GoUI/Go/Decoders.elm

35 lines
1020 B
Elm

module Go.Decoders exposing (..)
import Go.Types exposing (..)
import Go.Util exposing (pair)
import Json.Decode as Decode
import Json.Decode.Pipeline exposing (decode, required)
decodeIndex : Decode.Decoder Index
decodeIndex = decode pair
|> required "x" Decode.int
|> required "y" Decode.int
decodeColor : Decode.Decoder Color
decodeColor =
let
tryDecode : String -> Decode.Decoder Color
tryDecode s = case s of
"White" -> Decode.succeed White
"Black" -> Decode.succeed Black
_ -> Decode.fail "Invalid color"
in
Decode.andThen tryDecode Decode.string
decodeCell : Decode.Decoder Cell
decodeCell = decode pair
|> required "index" decodeIndex
|> required "color" decodeColor
decodeUpdate : Decode.Decoder Update
decodeUpdate = decode pair
|> required "turn" decodeColor
|> required "board" (Decode.list decodeCell)
decodeUpdatestring : String -> Result String Update
decodeUpdatestring = Decode.decodeString decodeUpdate