Decode

DecodeError : [TooShort]

Error types when decoding a List U8 of utf-8 bytes using a Decoder

DecodeResult val

Return type of a Decoder.

This can be useful when creating a custom decoder or when using fromBytesPartial. For example writing unit tests, such as;

expect
    input = "\"hello\", " |> Str.toUtf8
    actual = Decode.fromBytesPartial input Json.json
    expected = Ok "hello"

    actual.result == expected

Decoder val fmt

Decodes a List U8 of utf-8 bytes where val is the type of the decoded value, and fmt is a Decoder which implements the DecoderFormatting ability

Decoding implements decoder : Decoder val fmt where val implements Decoding, fmt implements DecoderFormatting

Definition of the Decoding ability

DecoderFormatting implements u8 : Decoder U8 fmt where fmt implements DecoderFormatting u16 : Decoder U16 fmt where fmt implements DecoderFormatting u32 : Decoder U32 fmt where fmt implements DecoderFormatting u64 : Decoder U64 fmt where fmt implements DecoderFormatting u128 : Decoder U128 fmt where fmt implements DecoderFormatting i8 : Decoder I8 fmt where fmt implements DecoderFormatting i16 : Decoder I16 fmt where fmt implements DecoderFormatting i32 : Decoder I32 fmt where fmt implements DecoderFormatting i64 : Decoder I64 fmt where fmt implements DecoderFormatting i128 : Decoder I128 fmt where fmt implements DecoderFormatting f32 : Decoder F32 fmt where fmt implements DecoderFormatting f64 : Decoder F64 fmt where fmt implements DecoderFormatting dec : Decoder Dec fmt where fmt implements DecoderFormatting bool : Decoder Bool fmt where fmt implements DecoderFormatting string : Decoder Str fmt where fmt implements DecoderFormatting list : Decoder elem fmt -> Decoder (List elem) fmt where fmt implements DecoderFormatting record : state, (state, Str -> [ Keep (Decoder state fmt), Skip ]), (state, fmt -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting tuple : state, (state, U64 -> [ Next (Decoder state fmt), TooLong ]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting

Definition of the DecoderFormatting ability

custom : (List U8, fmt -> DecodeResult val) -> Decoder val fmt where fmt implements DecoderFormatting

Build a custom Decoder function. For example the implementation of decodeBool could be defined as follows;

decodeBool = Decode.custom \bytes, @Json {} ->
    when bytes is
        ['f', 'a', 'l', 's', 'e', ..] -> { result: Ok Bool.false, rest: List.dropFirst bytes 5 }
        ['t', 'r', 'u', 'e', ..] -> { result: Ok Bool.true, rest: List.dropFirst bytes 4 }
        _ -> { result: Err TooShort, rest: bytes }

decodeWith : List U8, Decoder val fmt, fmt -> DecodeResult val where fmt implements DecoderFormatting

Decode a List U8 utf-8 bytes using a specific Decoder function

fromBytesPartial : List U8, fmt -> DecodeResult val where val implements Decoding, fmt implements DecoderFormatting

Decode a List U8 utf-8 bytes and return a DecodeResult

expect
    input = "\"hello\", " |> Str.toUtf8
    actual = Decode.fromBytesPartial input Json.json
    expected = Ok "hello"

    actual.result == expected

fromBytes : 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.toUtf8
    actual = Decode.fromBytes input Json.json
    expected = Ok "hello"

    actual == expected

mapResult : DecodeResult a, (a -> b) -> DecodeResult b

Transform the val of a DecodeResult