Implement decoding lists from env vars

This commit is contained in:
Ayaz Hafiz 2022-09-27 17:54:38 -05:00
parent 57231de0c1
commit a8b1cb2d90
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
2 changed files with 32 additions and 5 deletions

View File

@ -1,6 +1,5 @@
interface Env
exposes [cwd, dict, var, decode]
imports [Task.{ Task }, Path.{ Path }, InternalPath, Effect, InternalTask,
exposes [cwd, dict, var, decode] imports [Task.{ Task }, Path.{ Path }, InternalPath, Effect, InternalTask,
Decode.{ Decoder, DecodeError, Decoding, DecoderFormatting }
]
@ -121,9 +120,33 @@ envString = Decode.custom \bytes, @EnvFormatting {} ->
Ok s -> {result: Ok s, rest: []}
Err _ -> {result: Err TooShort, rest: bytes}
envList : Decoder _ _ -> _
envList = \_decodeElem -> Decode.custom \bytes, @EnvFormatting {} ->
{result: Err TooShort, rest: bytes}
envList = \decodeElem -> Decode.custom \bytes, @EnvFormatting {} ->
# Per our supported methods of decoding, this is either a list of strings or
# a list of numbers; in either case, the list of bytes must be Utf-8
# decodable. So just parse it as a list of strings and pass each chunk to
# the element decoder. By construction, our element decoders expect to parse
# a whole list of bytes anyway.
decodeElems = \allBytes, accum ->
{toParse, remainder} =
when List.splitFirst allBytes (Num.toU8 ',') is
Ok { before, after } ->
{ toParse: before, remainder: Some after }
Err NotFound ->
{ toParse: allBytes, remainder: None }
when Decode.decodeWith toParse decodeElem (@EnvFormatting {}) is
{result, rest} ->
when result is
Ok val ->
when remainder is
Some restBytes -> decodeElems restBytes (List.append accum val)
None -> Done (List.append accum val)
Err e -> Errored e rest
when decodeElems bytes [] is
Errored e rest -> { result: Err e, rest }
Done vals ->
{ result: Ok vals, rest: [] }
# TODO: we must currently annotate the arrows here so that the lambda sets are
# exercised, and the solver can find an ambient lambda set for the

View File

@ -14,4 +14,8 @@ main =
n ->
lvlStr = Num.toStr n
Stdout.line "Your current shell level is \(lvlStr)!")
|> Task.await (\{} -> Env.decode "LETTERS")
|> Task.await (\letters ->
joinedLetters = Str.joinWith letters " "
Stdout.line "Your favorite letters are: \(joinedLetters)")
|> Program.quick