You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.1 KiB

module WsMessage exposing (..)
import Json.Decode exposing (Decoder, andThen, decodeString, fail, field, map2, map3, string, succeed)
type Action
= Publish
strToAction : String -> Decoder Action
strToAction str =
if str == "Publish" then
succeed Publish
else
fail ("invalid action '" ++ str ++ "'")
type alias Publisher =
{ uuid : String
, name : String
}
type alias WsMessage =
{ action : Action
, publisher : Publisher
, payload : String
}
-- DECODERS
publisherDecoder : Decoder Publisher
publisherDecoder =
map2 Publisher
(field "uuid" string)
(field "name" string)
msgDecoder : Decoder WsMessage
msgDecoder =
map3 WsMessage
actionDecoder
(field "publisher" publisherDecoder)
(field "payload" string)
actionDecoder : Decoder Action
actionDecoder =
field "action" string |> andThen strToAction
decode : String -> Maybe WsMessage
decode str =
case decodeString msgDecoder str of
Ok msg ->
Just msg
Err _ ->
Nothing