mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
Import Decode by default in all modules
This commit is contained in:
parent
f10c47ec20
commit
06bef34829
4 changed files with 81 additions and 57 deletions
|
@ -124,6 +124,14 @@ const MODULE_ENCODE_TYPES: &[(&str, Symbol)] = &[
|
|||
("EncoderFormatting", Symbol::ENCODE_ENCODERFORMATTING),
|
||||
];
|
||||
|
||||
const MODULE_DECODE_TYPES: &[(&str, Symbol)] = &[
|
||||
("DecodeError", Symbol::DECODE_DECODE_ERROR),
|
||||
("DecodeResult", Symbol::DECODE_DECODE_RESULT),
|
||||
("Decoder", Symbol::DECODE_DECODER_OPAQUE),
|
||||
("Decoding", Symbol::DECODE_DECODING),
|
||||
("DecoderFormatting", Symbol::DECODE_DECODERFORMATTING),
|
||||
];
|
||||
|
||||
macro_rules! log {
|
||||
($($arg:tt)*) => (dbg_do!(ROC_PRINT_LOAD_LOG, println!($($arg)*)))
|
||||
}
|
||||
|
@ -2249,6 +2257,7 @@ fn update<'a>(
|
|||
.imported_modules
|
||||
.insert(ModuleId::LIST, Region::zero());
|
||||
|
||||
// ENCODE
|
||||
header
|
||||
.package_qualified_imported_modules
|
||||
.insert(PackageQualified::Unqualified(ModuleId::ENCODE));
|
||||
|
@ -2262,6 +2271,21 @@ fn update<'a>(
|
|||
.exposed_imports
|
||||
.insert(Ident::from(*type_name), (*symbol, Region::zero()));
|
||||
}
|
||||
|
||||
// DECODE
|
||||
header
|
||||
.package_qualified_imported_modules
|
||||
.insert(PackageQualified::Unqualified(ModuleId::DECODE));
|
||||
|
||||
header
|
||||
.imported_modules
|
||||
.insert(ModuleId::DECODE, Region::zero());
|
||||
|
||||
for (type_name, symbol) in MODULE_DECODE_TYPES {
|
||||
header
|
||||
.exposed_imports
|
||||
.insert(Ident::from(*type_name), (*symbol, Region::zero()));
|
||||
}
|
||||
}
|
||||
|
||||
state
|
||||
|
|
|
@ -6392,21 +6392,21 @@ mod solve_expr {
|
|||
r#"
|
||||
app "test" provides [myU8] to "./platform"
|
||||
|
||||
DecodeError : [TooShort, Leftover (List U8)]
|
||||
MDecodeError : [TooShort, Leftover (List U8)]
|
||||
|
||||
Decoder val fmt := List U8, fmt -> { result: Result val DecodeError, rest: List U8 } | fmt has DecoderFormatting
|
||||
MDecoder val fmt := List U8, fmt -> { result: Result val MDecodeError, rest: List U8 } | fmt has MDecoderFormatting
|
||||
|
||||
Decoding has
|
||||
decoder : Decoder val fmt | val has Decoding, fmt has DecoderFormatting
|
||||
MDecoding has
|
||||
decoder : MDecoder val fmt | val has MDecoding, fmt has MDecoderFormatting
|
||||
|
||||
DecoderFormatting has
|
||||
u8 : Decoder U8 fmt | fmt has DecoderFormatting
|
||||
MDecoderFormatting has
|
||||
u8 : MDecoder U8 fmt | fmt has MDecoderFormatting
|
||||
|
||||
decodeWith : List U8, Decoder val fmt, fmt -> { result: Result val DecodeError, rest: List U8 } | fmt has DecoderFormatting
|
||||
decodeWith = \lst, (@Decoder doDecode), fmt -> doDecode lst fmt
|
||||
decodeWith : List U8, MDecoder val fmt, fmt -> { result: Result val MDecodeError, rest: List U8 } | fmt has MDecoderFormatting
|
||||
decodeWith = \lst, (@MDecoder doDecode), fmt -> doDecode lst fmt
|
||||
|
||||
fromBytes : List U8, fmt -> Result val DecodeError
|
||||
| fmt has DecoderFormatting, val has Decoding
|
||||
fromBytes : List U8, fmt -> Result val MDecodeError
|
||||
| fmt has MDecoderFormatting, val has MDecoding
|
||||
fromBytes = \lst, fmt ->
|
||||
when decodeWith lst decoder fmt is
|
||||
{ result, rest } ->
|
||||
|
@ -6415,17 +6415,17 @@ mod solve_expr {
|
|||
Err e -> Err e
|
||||
|
||||
|
||||
Linear := {} has [DecoderFormatting {u8}]
|
||||
Linear := {} has [MDecoderFormatting {u8}]
|
||||
|
||||
u8 = @Decoder \lst, @Linear {} ->
|
||||
u8 = @MDecoder \lst, @Linear {} ->
|
||||
#^^{-1}
|
||||
when List.first lst is
|
||||
Ok n -> { result: Ok n, rest: List.dropFirst lst }
|
||||
Err _ -> { result: Err TooShort, rest: [] }
|
||||
|
||||
MyU8 := U8 has [Decoding {decoder}]
|
||||
MyU8 := U8 has [MDecoding {decoder}]
|
||||
|
||||
decoder = @Decoder \lst, fmt ->
|
||||
decoder = @MDecoder \lst, fmt ->
|
||||
#^^^^^^^{-1}
|
||||
when decodeWith lst u8 fmt is
|
||||
{ result, rest } ->
|
||||
|
@ -6436,11 +6436,11 @@ mod solve_expr {
|
|||
#^^^^{-1}
|
||||
"#
|
||||
),
|
||||
@r#"
|
||||
Linear#u8(11) : Decoder U8 Linear
|
||||
MyU8#decoder(12) : Decoder MyU8 fmt | fmt has DecoderFormatting
|
||||
myU8 : Result MyU8 DecodeError
|
||||
"#
|
||||
@r###"
|
||||
Linear#u8(11) : MDecoder U8 Linear
|
||||
MyU8#decoder(12) : MDecoder MyU8 fmt | fmt has MDecoderFormatting
|
||||
myU8 : Result MyU8 MDecodeError
|
||||
"###
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -272,21 +272,21 @@ fn decode() {
|
|||
r#"
|
||||
app "test" provides [myU8] to "./platform"
|
||||
|
||||
DecodeError : [TooShort, Leftover (List U8)]
|
||||
MDecodeError : [TooShort, Leftover (List U8)]
|
||||
|
||||
Decoder val fmt := List U8, fmt -> { result: Result val DecodeError, rest: List U8 } | fmt has DecoderFormatting
|
||||
MDecoder val fmt := List U8, fmt -> { result: Result val MDecodeError, rest: List U8 } | fmt has MDecoderFormatting
|
||||
|
||||
Decoding has
|
||||
decoder : Decoder val fmt | val has Decoding, fmt has DecoderFormatting
|
||||
MDecoding has
|
||||
decoder : MDecoder val fmt | val has MDecoding, fmt has MDecoderFormatting
|
||||
|
||||
DecoderFormatting has
|
||||
u8 : Decoder U8 fmt | fmt has DecoderFormatting
|
||||
MDecoderFormatting has
|
||||
u8 : MDecoder U8 fmt | fmt has MDecoderFormatting
|
||||
|
||||
decodeWith : List U8, Decoder val fmt, fmt -> { result: Result val DecodeError, rest: List U8 } | fmt has DecoderFormatting
|
||||
decodeWith = \lst, (@Decoder doDecode), fmt -> doDecode lst fmt
|
||||
decodeWith : List U8, MDecoder val fmt, fmt -> { result: Result val MDecodeError, rest: List U8 } | fmt has MDecoderFormatting
|
||||
decodeWith = \lst, (@MDecoder doDecode), fmt -> doDecode lst fmt
|
||||
|
||||
fromBytes : List U8, fmt -> Result val DecodeError
|
||||
| fmt has DecoderFormatting, val has Decoding
|
||||
fromBytes : List U8, fmt -> Result val MDecodeError
|
||||
| fmt has MDecoderFormatting, val has MDecoding
|
||||
fromBytes = \lst, fmt ->
|
||||
when decodeWith lst decoder fmt is
|
||||
{ result, rest } ->
|
||||
|
@ -296,17 +296,17 @@ fn decode() {
|
|||
else Err (Leftover rest)
|
||||
|
||||
|
||||
Linear := {} has [DecoderFormatting {u8}]
|
||||
Linear := {} has [MDecoderFormatting {u8}]
|
||||
|
||||
u8 = @Decoder \lst, @Linear {} ->
|
||||
u8 = @MDecoder \lst, @Linear {} ->
|
||||
when List.first lst is
|
||||
Ok n -> { result: Ok n, rest: List.dropFirst lst }
|
||||
Err _ -> { result: Err TooShort, rest: [] }
|
||||
|
||||
MyU8 := U8 has [Decoding {decoder}]
|
||||
MyU8 := U8 has [MDecoding {decoder}]
|
||||
|
||||
# impl Decoding for MyU8
|
||||
decoder = @Decoder \lst, fmt ->
|
||||
# impl MDecoding for MyU8
|
||||
decoder = @MDecoder \lst, fmt ->
|
||||
{ result, rest } = decodeWith lst u8 fmt
|
||||
{ result: Result.map result (\n -> @MyU8 n), rest }
|
||||
|
||||
|
@ -743,7 +743,7 @@ fn decode_use_stdlib() {
|
|||
indoc!(
|
||||
r#"
|
||||
app "test"
|
||||
imports [Decode.{ Decoding }, Json]
|
||||
imports [Json]
|
||||
provides [main] to "./platform"
|
||||
|
||||
MyNum := U8 has [Decoding {decoder: myDecoder}]
|
||||
|
@ -774,7 +774,7 @@ fn decode_use_stdlib_json_list() {
|
|||
indoc!(
|
||||
r#"
|
||||
app "test"
|
||||
imports [Decode.{ Decoding }, Json]
|
||||
imports [Json]
|
||||
provides [main] to "./platform"
|
||||
|
||||
MyNumList := List U8 has [Decoding {decoder: myDecoder}]
|
||||
|
@ -817,7 +817,7 @@ mod decode_immediate {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "\"foo\"" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -838,7 +838,7 @@ mod decode_immediate {
|
|||
assert_evals_to!(
|
||||
&format!(indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Num.toStr {}{} |> Str.toUtf8 |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -876,7 +876,7 @@ mod decode_immediate {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Num.toStr 17.23dec |> Str.toUtf8 |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -896,7 +896,7 @@ fn decode_list_of_strings() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "[\"a\",\"b\",\"c\"]" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -915,7 +915,7 @@ fn encode_then_decode_list_of_strings() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Encode.toBytes ["a", "b", "c"] Json.fromUtf8 |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -935,7 +935,7 @@ fn encode_then_decode_list_of_lists_of_strings() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Encode.toBytes [["a", "b"], ["c", "d", "e"], ["f"]] Json.fromUtf8 |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -957,7 +957,7 @@ fn decode_record_two_fields() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{\"first\":\"ab\",\"second\":\"cd\"}" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -979,7 +979,7 @@ fn decode_record_two_fields_string_and_int() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{\"first\":\"ab\",\"second\":10}" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -1001,7 +1001,7 @@ fn decode_record_two_fields_string_and_string_infer() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{\"first\":\"ab\",\"second\":\"cd\"}" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -1023,7 +1023,7 @@ fn decode_record_two_fields_string_and_string_infer_local_var() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
decoded = Str.toUtf8 "{\"first\":\"ab\",\"second\":\"cd\"}" |> Decode.fromBytes Json.fromUtf8
|
||||
|
@ -1046,7 +1046,7 @@ fn decode_record_two_fields_string_and_string_infer_local_var_destructured() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
decoded = Str.toUtf8 "{\"first\":\"ab\",\"second\":\"cd\"}" |> Decode.fromBytes Json.fromUtf8
|
||||
|
@ -1067,7 +1067,7 @@ fn decode_empty_record() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{}" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
@ -1090,7 +1090,7 @@ fn decode_record_of_record() {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
when Str.toUtf8 "{\"outer\":{\"inner\":\"a\"},\"other\":{\"one\":\"b\",\"two\":10}}" |> Decode.fromBytes Json.fromUtf8 is
|
||||
|
|
|
@ -10186,7 +10186,7 @@ All branches in an `if` must have the same type!
|
|||
derive_decoding_for_function,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode] provides [A] to "./platform"
|
||||
app "test" imports [] provides [A] to "./platform"
|
||||
|
||||
A a := a -> a has [Decode.Decoding]
|
||||
"#
|
||||
|
@ -10209,7 +10209,7 @@ All branches in an `if` must have the same type!
|
|||
derive_decoding_for_non_decoding_opaque,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode] provides [A] to "./platform"
|
||||
app "test" imports [] provides [A] to "./platform"
|
||||
|
||||
A := B has [Decode.Decoding]
|
||||
|
||||
|
@ -10235,7 +10235,7 @@ All branches in an `if` must have the same type!
|
|||
derive_decoding_for_other_has_decoding,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode] provides [A] to "./platform"
|
||||
app "test" imports [] provides [A] to "./platform"
|
||||
|
||||
A := B has [Decode.Decoding]
|
||||
|
||||
|
@ -10249,7 +10249,7 @@ All branches in an `if` must have the same type!
|
|||
derive_decoding_for_recursive_deriving,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode] provides [MyNat] to "./platform"
|
||||
app "test" imports [] provides [MyNat] to "./platform"
|
||||
|
||||
MyNat := [S MyNat, Z] has [Decode.Decoding]
|
||||
"#
|
||||
|
@ -10261,7 +10261,7 @@ All branches in an `if` must have the same type!
|
|||
function_cannot_derive_encoding,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode.{Decoder, DecoderFormatting, decoder}] provides [main] to "./platform"
|
||||
app "test" imports [Decode.{decoder}] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
myDecoder : Decoder (a -> a) fmt | fmt has DecoderFormatting
|
||||
|
@ -10291,7 +10291,7 @@ All branches in an `if` must have the same type!
|
|||
nested_opaque_cannot_derive_encoding,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode.{Decoder, DecoderFormatting, decoder}] provides [main] to "./platform"
|
||||
app "test" imports [Decode.{decoder}] provides [main] to "./platform"
|
||||
|
||||
A := {}
|
||||
|
||||
|
@ -10475,7 +10475,7 @@ All branches in an `if` must have the same type!
|
|||
infer_decoded_record_error_with_function_field,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode, Json] provides [main] to "./platform"
|
||||
app "test" imports [Json] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
decoded = Str.toUtf8 "{\"first\":\"ab\",\"second\":\"cd\"}" |> Decode.fromBytes Json.fromUtf8
|
||||
|
@ -10505,7 +10505,7 @@ All branches in an `if` must have the same type!
|
|||
record_with_optional_field_types_cannot_derive_decoding,
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" imports [Decode.{Decoder, DecoderFormatting, decoder}] provides [main] to "./platform"
|
||||
app "test" imports [Decode.{decoder}] provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
myDecoder : Decoder {x : Str, y ? Str} fmt | fmt has DecoderFormatting
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue