Skip to content

Commit

Permalink
List U8 -> input
Browse files Browse the repository at this point in the history
  • Loading branch information
roboteng committed Jun 28, 2024
1 parent ee03ace commit 62b0106
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
60 changes: 30 additions & 30 deletions crates/compiler/builtins/roc/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,43 @@ import Bool exposing [Bool]
##
## actual.result == expected
## ```
DecodeResult val err : { result : Result val err, rest : List U8 }
DecodeResult input val err : { result : Result val err, rest : input }

## Decodes a `List U8` of utf-8 bytes where `val` is the type of the decoded
## Decodes a `input` of utf-8 bytes where `val` is the type of the decoded
## value, and `fmt` is a [Decoder] which implements the [DecoderFormatting]
## ability
Decoder val fmt err := List U8, fmt -> DecodeResult val err where fmt implements DecoderFormatting
Decoder input val fmt err := input, fmt -> DecodeResult input val err where fmt implements DecoderFormatting

## Definition of the [Decoding] ability
Decoding implements
decoder : Decoder val fmt err where val implements Decoding, fmt implements DecoderFormatting
decoder : Decoder input val fmt err where val implements Decoding, fmt implements DecoderFormatting

## Definition of the [DecoderFormatting] ability
DecoderFormatting implements
u8 : Decoder U8 fmt err where fmt implements DecoderFormatting
u16 : Decoder U16 fmt err where fmt implements DecoderFormatting
u32 : Decoder U32 fmt err where fmt implements DecoderFormatting
u64 : Decoder U64 fmt err where fmt implements DecoderFormatting
u128 : Decoder U128 fmt err where fmt implements DecoderFormatting
i8 : Decoder I8 fmt err where fmt implements DecoderFormatting
i16 : Decoder I16 fmt err where fmt implements DecoderFormatting
i32 : Decoder I32 fmt err where fmt implements DecoderFormatting
i64 : Decoder I64 fmt err where fmt implements DecoderFormatting
i128 : Decoder I128 fmt err where fmt implements DecoderFormatting
f32 : Decoder F32 fmt err where fmt implements DecoderFormatting
f64 : Decoder F64 fmt err where fmt implements DecoderFormatting
dec : Decoder Dec fmt err where fmt implements DecoderFormatting
bool : Decoder Bool fmt err where fmt implements DecoderFormatting
string : Decoder Str fmt err where fmt implements DecoderFormatting
list : Decoder elem fmt err -> Decoder (List elem) fmt err where fmt implements DecoderFormatting
u8 : Decoder input U8 fmt err where fmt implements DecoderFormatting
u16 : Decoder input U16 fmt err where fmt implements DecoderFormatting
u32 : Decoder input U32 fmt err where fmt implements DecoderFormatting
u64 : Decoder input U64 fmt err where fmt implements DecoderFormatting
u128 : Decoder input U128 fmt err where fmt implements DecoderFormatting
i8 : Decoder input I8 fmt err where fmt implements DecoderFormatting
i16 : Decoder input I16 fmt err where fmt implements DecoderFormatting
i32 : Decoder input I32 fmt err where fmt implements DecoderFormatting
i64 : Decoder input I64 fmt err where fmt implements DecoderFormatting
i128 : Decoder input I128 fmt err where fmt implements DecoderFormatting
f32 : Decoder input F32 fmt err where fmt implements DecoderFormatting
f64 : Decoder input F64 fmt err where fmt implements DecoderFormatting
dec : Decoder input Dec fmt err where fmt implements DecoderFormatting
bool : Decoder input Bool fmt err where fmt implements DecoderFormatting
string : Decoder input Str fmt err where fmt implements DecoderFormatting
list : Decoder input elem fmt err -> Decoder input (List elem) fmt err where fmt implements DecoderFormatting

## `record state stepField finalizer` decodes a record field-by-field.
##
## `stepField` returns a decoder for the given field in the record, or
## `Skip` if the field is not a part of the decoded record.
##
## `finalizer` should produce the record value from the decoded `state`.
record : state, (state, Str -> [Keep (Decoder state fmt err), Skip]), (state, fmt -> Result val err) -> Decoder val fmt err where fmt implements DecoderFormatting
record : state, (state, Str -> [Keep (Decoder input state fmt err), Skip]), (state, fmt -> Result val err) -> Decoder input val fmt err where fmt implements DecoderFormatting

## `tuple state stepElem finalizer` decodes a tuple element-by-element.
##
Expand All @@ -106,7 +106,7 @@ DecoderFormatting implements
## index passed to `stepElem` is 0-indexed.
##
## `finalizer` should produce the tuple value from the decoded `state`.
tuple : state, (state, U64 -> [Next (Decoder state fmt err), TooLong]), (state -> Result val err) -> Decoder val fmt err where fmt implements DecoderFormatting
tuple : state, (state, U64 -> [Next (Decoder input state fmt err), TooLong]), (state -> Result val err) -> Decoder input val fmt err where fmt implements DecoderFormatting

## Build a custom [Decoder] function. For example the implementation of
## `decodeBool` could be defined as follows;
Expand All @@ -118,14 +118,14 @@ DecoderFormatting implements
## ['t', 'r', 'u', 'e', ..] -> { result: Ok Bool.true, rest: List.dropFirst bytes 4 }
## _ -> { result: Err TooShort, rest: bytes }
## ```
custom : (List U8, fmt -> DecodeResult val err) -> Decoder val fmt err where fmt implements DecoderFormatting
custom : (input, fmt -> DecodeResult input val err) -> Decoder input val fmt err where fmt implements DecoderFormatting
custom = \decode -> @Decoder decode

## Decode a `List U8` utf-8 bytes using a specific [Decoder] function
decodeWith : List U8, Decoder val fmt err, fmt -> DecodeResult val err where fmt implements DecoderFormatting
## Decode a `input` utf-8 bytes using a specific [Decoder] function
decodeWith : input, Decoder input val fmt err, fmt -> DecodeResult input val err where fmt implements DecoderFormatting
decodeWith = \bytes, @Decoder decode, fmt -> decode bytes fmt

## Decode a `List U8` utf-8 bytes and return a [DecodeResult](#DecodeResult)
## Decode a `input` utf-8 bytes and return a [DecodeResult](#DecodeResult)
## ```roc
## expect
## input = "\"hello\", " |> Str.toUtf8
Expand All @@ -134,12 +134,12 @@ decodeWith = \bytes, @Decoder decode, fmt -> decode bytes fmt
##
## actual.result == expected
## ```
fromBytesPartial : List U8, fmt -> DecodeResult val err where val implements Decoding, fmt implements DecoderFormatting
fromBytesPartial : input, fmt -> DecodeResult input val err where val implements Decoding, fmt implements DecoderFormatting
fromBytesPartial = \bytes, fmt -> decodeWith bytes decoder fmt

## Decode a `List U8` utf-8 bytes and return a [Result] with no leftover bytes
## Decode a `input` 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)`.
## remaining returns `Err Leftover (input)`.
## ```roc
## expect
## input = "\"hello\", " |> Str.toUtf8
Expand All @@ -160,5 +160,5 @@ fromBytes = \bytes, fmt ->
Err (Leftover rest)

## Transform the `val` of a [DecodeResult]
mapResult : DecodeResult a err, (a -> b) -> DecodeResult b err
mapResult : DecodeResult input a err, (a -> b) -> DecodeResult input b err
mapResult = \{ result, rest }, mapper -> { result: Result.map result mapper, rest }
56 changes: 28 additions & 28 deletions crates/compiler/builtins/roc/TotallyNotJson.roc
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ decodeF32 = Decode.custom \bytes, @Json {} ->

# Test decode of F32
expect
actual : DecodeResult F32 [TooShort]
actual : DecodeResult (List U8) F32 [TooShort]
actual = Str.toUtf8 "12.34e-5" |> Decode.fromBytesPartial json
numStr = actual.result |> Result.map Num.toStr

Expand All @@ -503,7 +503,7 @@ decodeF64 = Decode.custom \bytes, @Json {} ->

# Test decode of F64
expect
actual : DecodeResult F64 [TooShort]
actual : DecodeResult (List U8) F64 [TooShort]
actual = Str.toUtf8 "12.34e-5" |> Decode.fromBytesPartial json
numStr = actual.result |> Result.map Num.toStr

Expand All @@ -522,7 +522,7 @@ decodeDec = Decode.custom \bytes, @Json {} ->

# Test decode of Dec
expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = Str.toUtf8 "12.0034" |> Decode.fromBytesPartial json

actual.result == Ok 12.0034dec
Expand Down Expand Up @@ -582,7 +582,7 @@ expect

actual.result == Ok ("The Answer is", 42)

parseExactChar : List U8, U8 -> DecodeResult {} [TooShort]
parseExactChar : List U8, U8 -> DecodeResult (List U8) {} [TooShort]
parseExactChar = \bytes, char ->
when List.get bytes 0 is
Ok c ->
Expand All @@ -595,19 +595,19 @@ parseExactChar = \bytes, char ->

Err _ -> { result: Err TooShort, rest: bytes }

openBracket : List U8 -> DecodeResult {} [TooShort]
openBracket : List U8 -> DecodeResult (List U8) {} [TooShort]
openBracket = \bytes -> parseExactChar bytes '['

closingBracket : List U8 -> DecodeResult {} [TooShort]
closingBracket : List U8 -> DecodeResult (List U8) {} [TooShort]
closingBracket = \bytes -> parseExactChar bytes ']'

anything : List U8 -> DecodeResult {} [TooShort]
anything : List U8 -> DecodeResult (List U8) {} [TooShort]
anything = \bytes -> { result: Err TooShort, rest: bytes }

comma : List U8 -> DecodeResult {} [TooShort]
comma : List U8 -> DecodeResult (List U8) {} [TooShort]
comma = \bytes -> parseExactChar bytes ','

tryDecode : DecodeResult a [TooShort], ({ val : a, rest : List U8 } -> DecodeResult b [TooShort]) -> DecodeResult b [TooShort]
tryDecode : DecodeResult (List U8) a [TooShort], ({ val : a, rest : List U8 } -> DecodeResult (List U8) b [TooShort]) -> DecodeResult (List U8) b [TooShort]
tryDecode = \{ result, rest }, mapper ->
when result is
Ok val -> mapper { val, rest }
Expand Down Expand Up @@ -723,96 +723,96 @@ expect
actual == expected

expect
actual : DecodeResult U16 [TooShort]
actual : DecodeResult (List U8) U16 [TooShort]
actual = "+1" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = { result: Err TooShort, rest: ['+', '1'] }
actual == expected

expect
actual : DecodeResult U16 [TooShort]
actual : DecodeResult (List U8) U16 [TooShort]
actual = ".0" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = { result: Err TooShort, rest: ['.', '0'] }
actual == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "-.1" |> Str.toUtf8 |> Decode.fromBytesPartial json
actual.result == Err TooShort

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "72" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Ok 72dec
actual.result == expected

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "-0" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Ok 0dec
actual.result == expected

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "-7" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Ok -7dec
actual.result == expected

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "-0\n" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = { result: Ok 0dec, rest: ['\n'] }
actual == expected

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "123456789000 \n" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = { result: Ok 123456789000dec, rest: [' ', '\n'] }
actual == expected

expect
actual : DecodeResult Dec [TooShort]
actual : DecodeResult (List U8) Dec [TooShort]
actual = "-12.03" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Ok -12.03
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "-12." |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "01.1" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = ".0" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "1.e1" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "-1.2E" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "0.1e+" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected

expect
actual : DecodeResult U64 [TooShort]
actual : DecodeResult (List U8) U64 [TooShort]
actual = "-03" |> Str.toUtf8 |> Decode.fromBytesPartial json
expected = Err TooShort
actual.result == expected
Expand Down Expand Up @@ -1180,7 +1180,7 @@ ArrayClosingState : [
expect
input = Str.toUtf8 "[ ]"

actual : DecodeResult (List U8) [TooShort]
actual : DecodeResult (List U8) (List U8) [TooShort]
actual = Decode.fromBytesPartial input json

actual.result == Ok []
Expand All @@ -1189,7 +1189,7 @@ expect
expect
input = Str.toUtf8 "\n[\t 1 , 2 , 3]"

actual : DecodeResult (List U64) [TooShort]
actual : DecodeResult (List U8) (List U64) [TooShort]
actual = Decode.fromBytesPartial input json

expected = Ok [1, 2, 3]
Expand All @@ -1200,7 +1200,7 @@ expect
expect
input = Str.toUtf8 "\n\t [\n \"one\"\r , \"two\" , \n\"3\"\t]"

actual : DecodeResult (List Str) [TooShort]
actual : DecodeResult (List U8) (List Str) [TooShort]
actual = Decode.fromBytesPartial input json
expected = Ok ["one", "two", "3"]

Expand Down

0 comments on commit 62b0106

Please sign in to comment.