module Protocol exposing (State, encodeState, decodeState) import Json.Decode as D import Json.Encode as E {- PROTOCOL -} -- for now, this is still very boring and just has one field: type alias State = { state : Int } encodeState : State -> String encodeState state = E.object [ ("state", E.int state.state) ] |> E.encode 0 stateDecoder : D.Decoder State stateDecoder = D.map State (D.field "state" D.int) decodeState : String -> Maybe State decodeState text = case D.decodeString (D.nullable stateDecoder) text of Err e -> Nothing Ok value -> value