37 lines
1.0 KiB
Elm
37 lines
1.0 KiB
Elm
|
module Go.Decoders exposing (..)
|
||
|
import Go.Types exposing (..)
|
||
|
import Json.Decode as Decode
|
||
|
import Json.Decode.Pipeline exposing (decode, required)
|
||
|
|
||
|
pair : a -> b -> (a, b)
|
||
|
pair a1 a2 = (a1, a2)
|
||
|
|
||
|
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
|