DecodeResult val
Return type of a Decoder.
This can be useful when creating a custom decoder or when
using from_bytes_partial. For example writing unit tests,
such as;
expect
input = "\"hello\", " |> Str.to_utf8
actual = Decode.from_bytes_partial(input, Json.json)
expected = Ok("hello")
actual.result == expectedcustom : (List U8, fmt -> DecodeResult val) -> Decoder val fmt
where fmt implements DecoderFormatting
Build a custom Decoder function. For example the implementation of
decode_bool could be defined as follows;
decode_bool = Decode.custom \bytes, @Json({}) ->
when bytes is
['f', 'a', 'l', 's', 'e', ..] -> { result: Ok(Bool.false), rest: List.drop_first(bytes, 5) }
['t', 'r', 'u', 'e', ..] -> { result: Ok Bool.true, rest: List.drop_first(bytes, 4) }
_ -> { result: Err(TooShort), rest: bytes }from_bytes : List U8, fmt -> Result val [Leftover (List U8)]DecodeError
where val implements Decoding, fmt implements DecoderFormatting
Decode a List U8 utf-8 bytes and return a Result with no leftover bytes
expected. If successful returns Ok val, however, if there are bytes
remaining returns Err Leftover (List U8).
expect
input = "\"hello\", " |> Str.to_utf8
actual = Decode.from_bytes(input, Json.json)
expected = Ok("hello")
actual == expected