Skip to content

Commit ee03ace

Browse files
committed
removed DecodeError
1 parent f8c6786 commit ee03ace

File tree

2 files changed

+55
-59
lines changed

2 files changed

+55
-59
lines changed

crates/compiler/builtins/roc/Decode.roc

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module [
2-
DecodeError,
32
DecodeResult,
43
Decoder,
54
Decoding,
@@ -49,9 +48,6 @@ import Num exposing [
4948
]
5049
import Bool exposing [Bool]
5150

52-
## Error types when decoding a `List U8` of utf-8 bytes using a [Decoder]
53-
DecodeError : [TooShort]
54-
5551
## Return type of a [Decoder].
5652
##
5753
## This can be useful when creating a [custom](#custom) decoder or when
@@ -65,43 +61,43 @@ DecodeError : [TooShort]
6561
##
6662
## actual.result == expected
6763
## ```
68-
DecodeResult val : { result : Result val DecodeError, rest : List U8 }
64+
DecodeResult val err : { result : Result val err, rest : List U8 }
6965

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

7571
## Definition of the [Decoding] ability
7672
Decoding implements
77-
decoder : Decoder val fmt where val implements Decoding, fmt implements DecoderFormatting
73+
decoder : Decoder val fmt err where val implements Decoding, fmt implements DecoderFormatting
7874

7975
## Definition of the [DecoderFormatting] ability
8076
DecoderFormatting implements
81-
u8 : Decoder U8 fmt where fmt implements DecoderFormatting
82-
u16 : Decoder U16 fmt where fmt implements DecoderFormatting
83-
u32 : Decoder U32 fmt where fmt implements DecoderFormatting
84-
u64 : Decoder U64 fmt where fmt implements DecoderFormatting
85-
u128 : Decoder U128 fmt where fmt implements DecoderFormatting
86-
i8 : Decoder I8 fmt where fmt implements DecoderFormatting
87-
i16 : Decoder I16 fmt where fmt implements DecoderFormatting
88-
i32 : Decoder I32 fmt where fmt implements DecoderFormatting
89-
i64 : Decoder I64 fmt where fmt implements DecoderFormatting
90-
i128 : Decoder I128 fmt where fmt implements DecoderFormatting
91-
f32 : Decoder F32 fmt where fmt implements DecoderFormatting
92-
f64 : Decoder F64 fmt where fmt implements DecoderFormatting
93-
dec : Decoder Dec fmt where fmt implements DecoderFormatting
94-
bool : Decoder Bool fmt where fmt implements DecoderFormatting
95-
string : Decoder Str fmt where fmt implements DecoderFormatting
96-
list : Decoder elem fmt -> Decoder (List elem) fmt where fmt implements DecoderFormatting
77+
u8 : Decoder U8 fmt err where fmt implements DecoderFormatting
78+
u16 : Decoder U16 fmt err where fmt implements DecoderFormatting
79+
u32 : Decoder U32 fmt err where fmt implements DecoderFormatting
80+
u64 : Decoder U64 fmt err where fmt implements DecoderFormatting
81+
u128 : Decoder U128 fmt err where fmt implements DecoderFormatting
82+
i8 : Decoder I8 fmt err where fmt implements DecoderFormatting
83+
i16 : Decoder I16 fmt err where fmt implements DecoderFormatting
84+
i32 : Decoder I32 fmt err where fmt implements DecoderFormatting
85+
i64 : Decoder I64 fmt err where fmt implements DecoderFormatting
86+
i128 : Decoder I128 fmt err where fmt implements DecoderFormatting
87+
f32 : Decoder F32 fmt err where fmt implements DecoderFormatting
88+
f64 : Decoder F64 fmt err where fmt implements DecoderFormatting
89+
dec : Decoder Dec fmt err where fmt implements DecoderFormatting
90+
bool : Decoder Bool fmt err where fmt implements DecoderFormatting
91+
string : Decoder Str fmt err where fmt implements DecoderFormatting
92+
list : Decoder elem fmt err -> Decoder (List elem) fmt err where fmt implements DecoderFormatting
9793

9894
## `record state stepField finalizer` decodes a record field-by-field.
9995
##
10096
## `stepField` returns a decoder for the given field in the record, or
10197
## `Skip` if the field is not a part of the decoded record.
10298
##
10399
## `finalizer` should produce the record value from the decoded `state`.
104-
record : state, (state, Str -> [Keep (Decoder state fmt), Skip]), (state, fmt -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting
100+
record : state, (state, Str -> [Keep (Decoder state fmt err), Skip]), (state, fmt -> Result val err) -> Decoder val fmt err where fmt implements DecoderFormatting
105101

106102
## `tuple state stepElem finalizer` decodes a tuple element-by-element.
107103
##
@@ -110,7 +106,7 @@ DecoderFormatting implements
110106
## index passed to `stepElem` is 0-indexed.
111107
##
112108
## `finalizer` should produce the tuple value from the decoded `state`.
113-
tuple : state, (state, U64 -> [Next (Decoder state fmt), TooLong]), (state -> Result val DecodeError) -> Decoder val fmt where fmt implements DecoderFormatting
109+
tuple : state, (state, U64 -> [Next (Decoder state fmt err), TooLong]), (state -> Result val err) -> Decoder val fmt err where fmt implements DecoderFormatting
114110

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

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

132128
## Decode a `List U8` utf-8 bytes and return a [DecodeResult](#DecodeResult)
@@ -138,7 +134,7 @@ decodeWith = \bytes, @Decoder decode, fmt -> decode bytes fmt
138134
##
139135
## actual.result == expected
140136
## ```
141-
fromBytesPartial : List U8, fmt -> DecodeResult val where val implements Decoding, fmt implements DecoderFormatting
137+
fromBytesPartial : List U8, fmt -> DecodeResult val err where val implements Decoding, fmt implements DecoderFormatting
142138
fromBytesPartial = \bytes, fmt -> decodeWith bytes decoder fmt
143139

144140
## Decode a `List U8` utf-8 bytes and return a [Result] with no leftover bytes
@@ -152,17 +148,17 @@ fromBytesPartial = \bytes, fmt -> decodeWith bytes decoder fmt
152148
##
153149
## actual == expected
154150
## ```
155-
fromBytes : List U8, fmt -> Result val [Leftover (List U8)]DecodeError where val implements Decoding, fmt implements DecoderFormatting
151+
fromBytes : List U8, fmt -> Result val [Leftover (List U8)] where val implements Decoding, fmt implements DecoderFormatting
156152
fromBytes = \bytes, fmt ->
157153
when fromBytesPartial bytes fmt is
158154
{ result, rest } ->
159155
if List.isEmpty rest then
160156
when result is
161157
Ok val -> Ok val
162-
Err TooShort -> Err TooShort
158+
Err e -> Err e
163159
else
164160
Err (Leftover rest)
165161

166162
## Transform the `val` of a [DecodeResult]
167-
mapResult : DecodeResult a, (a -> b) -> DecodeResult b
163+
mapResult : DecodeResult a err, (a -> b) -> DecodeResult b err
168164
mapResult = \{ result, rest }, mapper -> { result: Result.map result mapper, rest }

crates/compiler/builtins/roc/TotallyNotJson.roc

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ decodeF32 = Decode.custom \bytes, @Json {} ->
484484

485485
# Test decode of F32
486486
expect
487-
actual : DecodeResult F32
487+
actual : DecodeResult F32 [TooShort]
488488
actual = Str.toUtf8 "12.34e-5" |> Decode.fromBytesPartial json
489489
numStr = actual.result |> Result.map Num.toStr
490490

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

504504
# Test decode of F64
505505
expect
506-
actual : DecodeResult F64
506+
actual : DecodeResult F64 [TooShort]
507507
actual = Str.toUtf8 "12.34e-5" |> Decode.fromBytesPartial json
508508
numStr = actual.result |> Result.map Num.toStr
509509

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

523523
# Test decode of Dec
524524
expect
525-
actual : DecodeResult Dec
525+
actual : DecodeResult Dec [TooShort]
526526
actual = Str.toUtf8 "12.0034" |> Decode.fromBytesPartial json
527527

528528
actual.result == Ok 12.0034dec
@@ -582,7 +582,7 @@ expect
582582

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

585-
parseExactChar : List U8, U8 -> DecodeResult {}
585+
parseExactChar : List U8, U8 -> DecodeResult {} [TooShort]
586586
parseExactChar = \bytes, char ->
587587
when List.get bytes 0 is
588588
Ok c ->
@@ -595,19 +595,19 @@ parseExactChar = \bytes, char ->
595595

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

598-
openBracket : List U8 -> DecodeResult {}
598+
openBracket : List U8 -> DecodeResult {} [TooShort]
599599
openBracket = \bytes -> parseExactChar bytes '['
600600

601-
closingBracket : List U8 -> DecodeResult {}
601+
closingBracket : List U8 -> DecodeResult {} [TooShort]
602602
closingBracket = \bytes -> parseExactChar bytes ']'
603603

604-
anything : List U8 -> DecodeResult {}
604+
anything : List U8 -> DecodeResult {} [TooShort]
605605
anything = \bytes -> { result: Err TooShort, rest: bytes }
606606

607-
comma : List U8 -> DecodeResult {}
607+
comma : List U8 -> DecodeResult {} [TooShort]
608608
comma = \bytes -> parseExactChar bytes ','
609609

610-
tryDecode : DecodeResult a, ({ val : a, rest : List U8 } -> DecodeResult b) -> DecodeResult b
610+
tryDecode : DecodeResult a [TooShort], ({ val : a, rest : List U8 } -> DecodeResult b [TooShort]) -> DecodeResult b [TooShort]
611611
tryDecode = \{ result, rest }, mapper ->
612612
when result is
613613
Ok val -> mapper { val, rest }
@@ -723,96 +723,96 @@ expect
723723
actual == expected
724724

725725
expect
726-
actual : DecodeResult U16
726+
actual : DecodeResult U16 [TooShort]
727727
actual = "+1" |> Str.toUtf8 |> Decode.fromBytesPartial json
728728
expected = { result: Err TooShort, rest: ['+', '1'] }
729729
actual == expected
730730

731731
expect
732-
actual : DecodeResult U16
732+
actual : DecodeResult U16 [TooShort]
733733
actual = ".0" |> Str.toUtf8 |> Decode.fromBytesPartial json
734734
expected = { result: Err TooShort, rest: ['.', '0'] }
735735
actual == expected
736736

737737
expect
738-
actual : DecodeResult U64
738+
actual : DecodeResult U64 [TooShort]
739739
actual = "-.1" |> Str.toUtf8 |> Decode.fromBytesPartial json
740740
actual.result == Err TooShort
741741

742742
expect
743-
actual : DecodeResult Dec
743+
actual : DecodeResult Dec [TooShort]
744744
actual = "72" |> Str.toUtf8 |> Decode.fromBytesPartial json
745745
expected = Ok 72dec
746746
actual.result == expected
747747

748748
expect
749-
actual : DecodeResult Dec
749+
actual : DecodeResult Dec [TooShort]
750750
actual = "-0" |> Str.toUtf8 |> Decode.fromBytesPartial json
751751
expected = Ok 0dec
752752
actual.result == expected
753753

754754
expect
755-
actual : DecodeResult Dec
755+
actual : DecodeResult Dec [TooShort]
756756
actual = "-7" |> Str.toUtf8 |> Decode.fromBytesPartial json
757757
expected = Ok -7dec
758758
actual.result == expected
759759

760760
expect
761-
actual : DecodeResult Dec
761+
actual : DecodeResult Dec [TooShort]
762762
actual = "-0\n" |> Str.toUtf8 |> Decode.fromBytesPartial json
763763
expected = { result: Ok 0dec, rest: ['\n'] }
764764
actual == expected
765765

766766
expect
767-
actual : DecodeResult Dec
767+
actual : DecodeResult Dec [TooShort]
768768
actual = "123456789000 \n" |> Str.toUtf8 |> Decode.fromBytesPartial json
769769
expected = { result: Ok 123456789000dec, rest: [' ', '\n'] }
770770
actual == expected
771771

772772
expect
773-
actual : DecodeResult Dec
773+
actual : DecodeResult Dec [TooShort]
774774
actual = "-12.03" |> Str.toUtf8 |> Decode.fromBytesPartial json
775775
expected = Ok -12.03
776776
actual.result == expected
777777

778778
expect
779-
actual : DecodeResult U64
779+
actual : DecodeResult U64 [TooShort]
780780
actual = "-12." |> Str.toUtf8 |> Decode.fromBytesPartial json
781781
expected = Err TooShort
782782
actual.result == expected
783783

784784
expect
785-
actual : DecodeResult U64
785+
actual : DecodeResult U64 [TooShort]
786786
actual = "01.1" |> Str.toUtf8 |> Decode.fromBytesPartial json
787787
expected = Err TooShort
788788
actual.result == expected
789789

790790
expect
791-
actual : DecodeResult U64
791+
actual : DecodeResult U64 [TooShort]
792792
actual = ".0" |> Str.toUtf8 |> Decode.fromBytesPartial json
793793
expected = Err TooShort
794794
actual.result == expected
795795

796796
expect
797-
actual : DecodeResult U64
797+
actual : DecodeResult U64 [TooShort]
798798
actual = "1.e1" |> Str.toUtf8 |> Decode.fromBytesPartial json
799799
expected = Err TooShort
800800
actual.result == expected
801801

802802
expect
803-
actual : DecodeResult U64
803+
actual : DecodeResult U64 [TooShort]
804804
actual = "-1.2E" |> Str.toUtf8 |> Decode.fromBytesPartial json
805805
expected = Err TooShort
806806
actual.result == expected
807807

808808
expect
809-
actual : DecodeResult U64
809+
actual : DecodeResult U64 [TooShort]
810810
actual = "0.1e+" |> Str.toUtf8 |> Decode.fromBytesPartial json
811811
expected = Err TooShort
812812
actual.result == expected
813813

814814
expect
815-
actual : DecodeResult U64
815+
actual : DecodeResult U64 [TooShort]
816816
actual = "-03" |> Str.toUtf8 |> Decode.fromBytesPartial json
817817
expected = Err TooShort
818818
actual.result == expected
@@ -1180,7 +1180,7 @@ ArrayClosingState : [
11801180
expect
11811181
input = Str.toUtf8 "[ ]"
11821182

1183-
actual : DecodeResult (List U8)
1183+
actual : DecodeResult (List U8) [TooShort]
11841184
actual = Decode.fromBytesPartial input json
11851185

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

1192-
actual : DecodeResult (List U64)
1192+
actual : DecodeResult (List U64) [TooShort]
11931193
actual = Decode.fromBytesPartial input json
11941194

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

1203-
actual : DecodeResult (List Str)
1203+
actual : DecodeResult (List Str) [TooShort]
12041204
actual = Decode.fromBytesPartial input json
12051205
expected = Ok ["one", "two", "3"]
12061206

0 commit comments

Comments
 (0)