Add distill helpers for codec packages.

This commit is contained in:
Dillon Kearns 2021-06-18 10:29:23 -07:00
parent c2599426bc
commit 09bd2e982e
5 changed files with 45 additions and 12 deletions

View File

@ -26,6 +26,7 @@
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"MartinSStewart/elm-serialize": "1.2.5 <= v < 2.0.0",
"avh4/elm-color": "1.0.0 <= v < 2.0.0",
"danyx23/elm-mimetype": "4.0.1 <= v < 5.0.0",
"dillonkearns/elm-bcp47-language-tag": "1.0.1 <= v < 2.0.0",

View File

@ -244,9 +244,8 @@ data route =
|> DataSource.fromResult
)
)
|> DataSource.distill ("markdown-blocks-" ++ route.slug)
(S.encodeToJson (S.list MarkdownCodec.codec))
(S.decodeFromJson (S.list MarkdownCodec.codec) >> Result.mapError (\_ -> "Error"))
|> DataSource.distillSerializeCodec ("markdown-blocks-" ++ route.slug)
(S.list MarkdownCodec.codec)
|> DataSource.andThen
(\blocks ->
blocks

View File

@ -157,12 +157,7 @@ titleForSection section =
|> Result.fromMaybe "Expected to find an H1 heading in this markdown."
|> DataSource.fromResult
)
|> DataSource.distill ("next-previous-" ++ section.slug)
(Serialize.encodeToJson NextPrevious.serialize)
(Serialize.decodeFromJson
NextPrevious.serialize
>> Result.mapError (\_ -> "Error")
)
|> DataSource.distillSerializeCodec ("next-previous-" ++ section.slug) NextPrevious.serialize
head :

View File

@ -32,9 +32,7 @@ dataSource docFiles =
)
|> DataSource.resolve
|> DataSource.map List.reverse
|> DataSource.distill "table-of-contents"
(S.encodeToJson serialize)
(S.decodeFromJson serialize >> Result.mapError (\_ -> "Error"))
|> DataSource.distillSerializeCodec "table-of-contents" serialize
codec : Codec (TableOfContents Data)

View File

@ -6,6 +6,7 @@ module DataSource exposing
, andThen, resolve, combine
, map2, map3, map4, map5, map6, map7, map8, map9
, distill, validate
, distillCodec, distillSerializeCodec
)
{-| StaticHttp requests are an alternative to doing Elm HTTP requests the traditional way using the `elm/http` package.
@ -66,6 +67,7 @@ and describe your use case!
-}
import Codec
import Dict exposing (Dict)
import Dict.Extra
import Json.Decode as Decode
@ -77,6 +79,7 @@ import Pages.Secrets
import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (RawRequest(..), WhatToDo)
import RequestsAndPending exposing (RequestsAndPending)
import Serialize
{-| Build an empty body for a StaticHttp request. See [elm/http's `Http.emptyBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#emptyBody).
@ -237,6 +240,43 @@ distill uniqueKey encode decode dataSource =
)
{-| -}
distillSerializeCodec :
String
-> Serialize.Codec error value
-> DataSource value
-> DataSource value
distillSerializeCodec uniqueKey serializeCodec =
distill uniqueKey
(Serialize.encodeToJson serializeCodec)
(Serialize.decodeFromJson serializeCodec
>> Result.mapError
(\error ->
case error of
Serialize.DataCorrupted ->
"DataCorrupted"
Serialize.CustomError errorMessage ->
"CustomError"
Serialize.SerializerOutOfDate ->
"SerializerOutOfDate"
)
)
{-| -}
distillCodec :
String
-> Codec.Codec value
-> DataSource value
-> DataSource value
distillCodec uniqueKey codec =
distill uniqueKey
(Codec.encodeToValue codec)
(Codec.decodeValue codec >> Result.mapError Decode.errorToString)
toResult : Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, b ) -> RawRequest b
toResult result =
case result of