diff options
author | Jakub Hampl <kopomir@gmail.com> | 2019-02-14 15:23:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-14 15:23:49 +0000 |
commit | 6bd5f8ccbd8c44c3311ef36b0e2de9ede4fa71ed (patch) | |
tree | de40a36d34cb734c2765a705506436f8b38e28a9 /style-generator/src/Decoder/Generic.elm | |
parent | f0c36a3d49fad46e0fb6cafeb7a021dd5d775993 (diff) |
New Style Generator (#8)
Diffstat (limited to 'style-generator/src/Decoder/Generic.elm')
-rw-r--r-- | style-generator/src/Decoder/Generic.elm | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/style-generator/src/Decoder/Generic.elm b/style-generator/src/Decoder/Generic.elm new file mode 100644 index 0000000..b81167e --- /dev/null +++ b/style-generator/src/Decoder/Generic.elm @@ -0,0 +1,52 @@ +module Decoder.Generic exposing (combine, pair, resultToDecoder, subdecode, tail, withDefault) + +import Json.Decode as D exposing (Decoder) + + +withDefault : a -> Decoder a -> Decoder a +withDefault fallback decoder = + D.oneOf + [ decoder + , D.succeed fallback + ] + + +combine : List (Decoder a) -> Decoder (List a) +combine = + List.foldr (D.map2 (::)) (D.succeed []) + + +subdecode : Decoder a -> D.Value -> Decoder a +subdecode d v = + D.decodeValue d v |> resultToDecoder + + +tail : Decoder a -> Decoder (List a) +tail itemDecoder = + D.list D.value + |> D.andThen + (\l -> + case l of + [] -> + D.fail "Can't get tail of empty" + + head :: t -> + List.map (subdecode itemDecoder) t |> combine + ) + + +pair : Decoder a -> Decoder b -> Decoder ( a, b ) +pair aDecoder bDecoder = + D.map2 Tuple.pair + (D.index 0 aDecoder) + (D.index 1 bDecoder) + + +resultToDecoder : Result D.Error a -> Decoder a +resultToDecoder res = + case res of + Ok a -> + D.succeed a + + Err e -> + D.fail (D.errorToString e) |