Skip to content

Commit 4724383

Browse files
authored
Merge pull request #7 from GearsDatapacks:master
Update to use new decoder API
2 parents c86d506 + 5ae7622 commit 4724383

6 files changed

Lines changed: 78 additions & 78 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: erlef/setup-beam@v1
1616
with:
1717
otp-version: "26.0.2"
18-
gleam-version: "1.5.1"
18+
gleam-version: "1.9.1"
1919
rebar3-version: "3"
2020
# elixir-version: "1.15.4"
2121
- run: gleam deps download

gleam.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ description = "Pretty print values with style!"
44
licences = ["Apache-2.0"]
55
repository = { type = "github", user = "MystPi", repo = "pprint" }
66

7-
internal_modules = [
8-
"pprint/decoder"
9-
]
7+
internal_modules = ["pprint/decoder"]
108

119
[dependencies]
12-
gleam_stdlib = "~> 0.34 or ~> 1.0"
10+
gleam_stdlib = ">= 0.50.0 and < 2.0.0"
1311
glam = "~> 2.0"
1412

1513
[dev-dependencies]

manifest.toml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

44
packages = [
55
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6-
{ name = "birdie", version = "1.1.4", build_tools = ["gleam"], requirements = ["argv", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "67E5D53B85540EDC6FAC738D7A4A2EEAA0C4920230AFFF4FA76C63670C057E5A" },
7-
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
8-
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
9-
{ name = "glance", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "8F3314D27773B7C3B9FB58D8C02C634290422CE531988C0394FA0DF8676B964D" },
10-
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
11-
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
12-
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
13-
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
14-
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
15-
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
16-
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" },
6+
{ name = "birdie", version = "1.2.6", build_tools = ["gleam"], requirements = ["argv", "edit_distance", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "term_size", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "1363F4C7E7433A4A8350CC682BCDDBA5BBC6F66C94EFC63BC43025F796C4F6D0" },
7+
{ name = "edit_distance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "edit_distance", source = "hex", outer_checksum = "A1E485C69A70210223E46E63985FA1008B8B2DDA9848B7897469171B29020C05" },
8+
{ name = "filepath", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "65F51013BCF78A603AFFD7992EF1CC6ECA96C74038EB48887F656DE44DBC1902" },
9+
{ name = "glam", version = "2.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "4932A2D139AB0389E149396407F89654928D7B815E212BB02F13C66F53B1BBA1" },
10+
{ name = "glance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "106111453AE9BA959184302B7DADF2E8CF322B27A7CB68EE78F3EE43FEACCE2C" },
11+
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
12+
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
13+
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
14+
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
15+
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
16+
{ name = "gleam_stdlib", version = "0.58.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "091F2D2C4A3A4E2047986C47E2C2C9D728A4E068ABB31FDA17B0D347E6248467" },
17+
{ name = "gleeunit", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "0E6C83834BA65EDCAAF4FE4FB94AC697D9262D83E6F58A750D63C9F6C8A9D9FF" },
18+
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
1719
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
1820
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
19-
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
20-
{ name = "thoas", version = "1.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "540C8CB7D9257F2AD0A14145DC23560F91ACDCA995F0CCBA779EB33AF5D859D1" },
21+
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
22+
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" },
2123
{ name = "trie_again", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "5B19176F52B1BD98831B57FDC97BD1F88C8A403D6D8C63471407E78598E27184" },
2224
]
2325

src/pprint/decoder.gleam

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gleam/dict.{type Dict}
22
import gleam/dynamic.{type Dynamic}
3+
import gleam/dynamic/decode
34
import gleam/result
45
import gleam/string
56

@@ -27,42 +28,54 @@ pub type Field {
2728
// ---- DECODERS ---------------------------------------------------------------
2829

2930
pub fn classify(value: Dynamic) -> Type {
30-
let assert Ok(t) = decode_type(value)
31+
let assert Ok(t) = decode.run(value, type_decoder())
3132
t
3233
}
3334

34-
/// This decoder will always be `Ok`. It returns `Result` so that it is compatible
35-
/// with other decoders.
36-
///
37-
fn decode_type(value: Dynamic) -> Result(Type, List(dynamic.DecodeError)) {
38-
use <- result.lazy_or(result.map(dynamic.int(value), TInt))
39-
use <- result.lazy_or(result.map(dynamic.float(value), TFloat))
40-
use <- result.lazy_or(result.map(dynamic.string(value), TString))
41-
use <- result.lazy_or(result.map(dynamic.bool(value), TBool))
42-
use <- result.lazy_or(result.map(decode_nil(value), fn(_) { TNil }))
43-
use <- result.lazy_or(result.map(dynamic.bit_array(value), TBitArray))
44-
use <- result.lazy_or(decode_custom_type(value))
45-
use <- result.lazy_or(result.map(decode_tuple(value), TTuple))
46-
use <- result.lazy_or(result.map(dynamic.shallow_list(value), TList))
47-
use <- result.lazy_or(result.map(
48-
dynamic.dict(decode_type, decode_type)(value),
49-
TDict,
50-
))
51-
// Anything else we just inspect. This could be a function or an external object
52-
// or type from the runtime.
53-
Ok(TForeign(string.inspect(value)))
35+
/// This decoder will always return `Ok`, as it ends with a catch-all
36+
/// pattern returning a `TForeign`.
37+
///
38+
fn type_decoder() -> decode.Decoder(Type) {
39+
use <- decode.recursive
40+
decode.one_of(decode.map(decode.int, TInt), [
41+
decode.map(decode.float, TFloat),
42+
decode.map(decode.float, TFloat),
43+
decode.map(decode.string, TString),
44+
decode.map(decode.bool, TBool),
45+
decode.map(nil(), fn(_) { TNil }),
46+
decode.map(decode.bit_array, TBitArray),
47+
custom_type(),
48+
decode.map(tuple(), TTuple),
49+
decode.map(decode.list(decode.dynamic), TList),
50+
decode.map(decode.dict(type_decoder(), type_decoder()), TDict),
51+
decode.map(decode.dynamic, fn(value) { TForeign(string.inspect(value)) }),
52+
])
53+
}
54+
55+
fn tuple() -> decode.Decoder(List(Dynamic)) {
56+
decode.new_primitive_decoder("Tuple", fn(dynamic) {
57+
result.replace_error(decode_tuple(dynamic), [])
58+
})
59+
}
60+
61+
fn custom_type() -> decode.Decoder(Type) {
62+
decode.new_primitive_decoder("CustomType", fn(dynamic) {
63+
result.replace_error(decode_custom_type(dynamic), TCustom("", []))
64+
})
65+
}
66+
67+
fn nil() -> decode.Decoder(Nil) {
68+
decode.new_primitive_decoder("Nil", decode_nil)
5469
}
5570

5671
@external(erlang, "pprint_ffi", "decode_custom_type")
5772
@external(javascript, "../pprint_ffi.mjs", "decode_custom_type")
58-
fn decode_custom_type(value: Dynamic) -> Result(Type, List(dynamic.DecodeError))
73+
fn decode_custom_type(value: Dynamic) -> Result(Type, Nil)
5974

6075
@external(erlang, "pprint_ffi", "decode_tuple")
6176
@external(javascript, "../pprint_ffi.mjs", "decode_tuple")
62-
fn decode_tuple(
63-
value: Dynamic,
64-
) -> Result(List(Dynamic), List(dynamic.DecodeError))
77+
fn decode_tuple(value: Dynamic) -> Result(List(Dynamic), Nil)
6578

6679
@external(erlang, "pprint_ffi", "decode_nil")
6780
@external(javascript, "../pprint_ffi.mjs", "decode_nil")
68-
fn decode_nil(value: Dynamic) -> Result(Nil, List(dynamic.DecodeError))
81+
fn decode_nil(value: Dynamic) -> Result(Nil, Nil)

src/pprint_ffi.erl

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
decode_nil(X) ->
99
case X of
1010
nil -> {ok, nil};
11-
_ -> decode_error("Nil", X)
11+
_ -> {error, nil}
1212
end.
1313

1414
decode_tuple(X) ->
1515
case X of
1616
Tuple when is_tuple(Tuple) -> {ok, tuple_to_list(Tuple)};
17-
_ -> decode_error("Tuple", X)
17+
_ -> {error, nil}
1818
end.
1919

2020
decode_custom_type(X) ->
@@ -23,28 +23,28 @@ decode_custom_type(X) ->
2323
Atom when is_atom(Atom) ->
2424
case inspect_maybe_gleam_atom(erlang:atom_to_binary(Atom), none, <<>>) of
2525
{ok, AtomName} -> {ok, {t_custom, AtomName, []}};
26-
{error, nil} -> decode_error("CustomType", X)
26+
{error, nil} -> {error, nil}
2727
end;
2828
% Variants with fields are encoded as tuples where the first items is an
2929
% atom with the variant's name.
3030
Tuple when is_tuple(Tuple) ->
3131
case tuple_to_list(Tuple) of
3232
[Atom | Elements] when is_atom(Atom) ->
3333
case inspect_maybe_gleam_atom(erlang:atom_to_binary(Atom), none, <<>>) of
34-
{ok, AtomName} -> {ok, {t_custom, AtomName, lists:map(fun(E) -> {positional, E} end, Elements)}};
35-
{error, nil} -> decode_error("CustomType", X)
34+
{ok, AtomName} ->
35+
{ok,
36+
{t_custom, AtomName,
37+
lists:map(fun(E) -> {positional, E} end, Elements)}};
38+
{error, nil} ->
39+
{error, nil}
3640
end;
37-
_ -> decode_error("CustomType", X)
41+
_ ->
42+
{error, nil}
3843
end;
39-
_ -> decode_error("CustomType", X)
44+
_ ->
45+
{error, nil}
4046
end.
4147

42-
decode_error(Expected, Got) ->
43-
ExpectedString = list_to_binary(Expected),
44-
GotString = gleam_stdlib:classify_dynamic(Got),
45-
DecodeError = {decode_error, ExpectedString, GotString, []},
46-
{error, [DecodeError]}.
47-
4848
% This is copy pasted from gleam's stdlib and performs some additional checks to
4949
% make sure the given atom is a gleam's custom type atom. Stdlib doesn't export
5050
% it so I had to copy it.
@@ -58,10 +58,11 @@ inspect_maybe_gleam_atom(<<"_", _Rest/binary>>, none, _) ->
5858
{error, nil};
5959
inspect_maybe_gleam_atom(<<"_">>, _PrevChar, _Acc) ->
6060
{error, nil};
61-
inspect_maybe_gleam_atom(<<"_", _Rest/binary>>, $_, _Acc) ->
61+
inspect_maybe_gleam_atom(<<"_", _Rest/binary>>, $_, _Acc) ->
6262
{error, nil};
63-
inspect_maybe_gleam_atom(<<First, _Rest/binary>>, _PrevChar, _Acc)
64-
when not (?is_lowercase_char(First) orelse ?is_underscore_char(First) orelse ?is_digit_char(First)) ->
63+
inspect_maybe_gleam_atom(<<First, _Rest/binary>>, _PrevChar, _Acc) when
64+
not (?is_lowercase_char(First) orelse ?is_underscore_char(First) orelse ?is_digit_char(First))
65+
->
6566
{error, nil};
6667
inspect_maybe_gleam_atom(<<First, Rest/binary>>, none, Acc) ->
6768
inspect_maybe_gleam_atom(Rest, First, <<Acc/binary, (uppercase(First))>>);

src/pprint_ffi.mjs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import * as $stdlib from '../gleam_stdlib/gleam_stdlib.mjs';
2-
import * as $dynamic from '../gleam_stdlib/gleam/dynamic.mjs';
31
import * as $gleam from './gleam.mjs';
42
import * as $decoder from './pprint/decoder.mjs';
53

6-
function decoder_error(expected, got) {
7-
return decoder_error_no_classify(expected, $stdlib.classify_dynamic(got));
8-
}
9-
10-
function decoder_error_no_classify(expected, got) {
11-
return new $gleam.Error(
12-
$gleam.List.fromArray([
13-
new $dynamic.DecodeError(expected, got, $gleam.toList([])),
14-
])
15-
);
16-
}
17-
184
export function decode_custom_type(value) {
195
if (value instanceof $gleam.CustomType) {
206
const name = value.constructor.name;
@@ -27,15 +13,15 @@ export function decode_custom_type(value) {
2713
return new $gleam.Ok(new $decoder.TCustom(name, $gleam.toList(fields)));
2814
}
2915

30-
return decoder_error('CustomType', value);
16+
return new $gleam.Error(undefined);
3117
}
3218

3319
export function decode_tuple(value) {
3420
if (Array.isArray(value)) return new $gleam.Ok($gleam.toList(value));
35-
return decoder_error('Tuple', value);
21+
return new $gleam.Error(undefined);
3622
}
3723

3824
export function decode_nil(value) {
39-
if (value === undefined) return new $gleam.Ok();
40-
return decoder_error('Nil', value);
25+
if (value === undefined) return new $gleam.Ok(undefined);
26+
return new $gleam.Error(undefined);
4127
}

0 commit comments

Comments
 (0)