diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-17 15:35:19 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-17 15:35:19 +0000 |
commit | fbd570295cd40802127913d2b308916eefb14066 (patch) | |
tree | 5a018e14b69d7748459091c97afa9e1cd7972d1d /gen | |
parent | 00ff9767dc61e698aac791b43704cfce1ab33600 (diff) | |
download | gleam_stdlib-fbd570295cd40802127913d2b308916eefb14066.tar.gz gleam_stdlib-fbd570295cd40802127913d2b308916eefb14066.zip |
Any decoders
Diffstat (limited to 'gen')
-rw-r--r-- | gen/any.erl | 62 | ||||
-rw-r--r-- | gen/result.erl | 17 |
2 files changed, 70 insertions, 9 deletions
diff --git a/gen/any.erl b/gen/any.erl index bfca689..a29ed3d 100644 --- a/gen/any.erl +++ b/gen/any.erl @@ -1,10 +1,70 @@ -module(any). -compile(no_auto_import). +-include_lib("eunit/include/eunit.hrl"). --export([from/1, unsafeCoerce/1]). +-export([from/1, unsafeCoerce/1, string/1, int/1, float/1, bool/1, thunk/1, tuple/1]). from(A) -> gleam__stdlib:identity(A). unsafeCoerce(A) -> gleam__stdlib:identity(A). + +string(A) -> + gleam__stdlib:decode_string(A). + +-ifdef(TEST). +string_test() -> + expect:equal(string(from(<<"">>)), {ok, <<"">>}), + expect:equal(string(from(<<"Hello">>)), {ok, <<"Hello">>}), + expect:equal(string(from(1)), {error, <<"Expected a String, got `1`">>}), + expect:equal(string(from([])), {error, <<"Expected a String, got `[]`">>}). +-endif. + +int(A) -> + gleam__stdlib:decode_int(A). + +-ifdef(TEST). +int_test() -> + expect:equal(int(from(1)), {ok, 1}), + expect:equal(int(from(2)), {ok, 2}), + expect:equal(int(from(1.0)), {error, <<"Expected an Int, got `1.0`">>}), + expect:equal(int(from([])), {error, <<"Expected an Int, got `[]`">>}). +-endif. + +float(A) -> + gleam__stdlib:decode_float(A). + +-ifdef(TEST). +float_test() -> + expect:equal(float(from(1.0)), {ok, 1.0}), + expect:equal(float(from(2.2)), {ok, 2.2}), + expect:equal(float(from(1)), {error, <<"Expected a Float, got `1`">>}), + expect:equal(float(from([])), {error, <<"Expected a Float, got `[]`">>}). +-endif. + +bool(A) -> + gleam__stdlib:decode_bool(A). + +-ifdef(TEST). +bool_test() -> + expect:equal(bool(from(true)), {ok, true}), + expect:equal(bool(from(false)), {ok, false}), + expect:equal(bool(from(1)), {error, <<"Expected a Bool, got `1`">>}), + expect:equal(bool(from([])), {error, <<"Expected a Bool, got `[]`">>}). +-endif. + +thunk(A) -> + gleam__stdlib:thunk(A). + +tuple(A) -> + gleam__stdlib:decode_tuple(A). + +-ifdef(TEST). +tuple_test() -> + expect:equal(tuple(from({1, []})), {ok, {from(1), from([])}}), + expect:equal(tuple(from({<<"ok">>, <<"ok">>})), + {ok, {from(<<"ok">>), from(<<"ok">>)}}), + expect:is_error(tuple(from({1}))), + expect:is_error(tuple(from({1, 2, 3}))). +-endif. diff --git a/gen/result.erl b/gen/result.erl index b52cadd..19d87ed 100644 --- a/gen/result.erl +++ b/gen/result.erl @@ -2,7 +2,7 @@ -compile(no_auto_import). -include_lib("eunit/include/eunit.hrl"). --export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, flat_map/2, unwrap/2]). +-export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, then/2, unwrap/2]). is_ok(Result) -> case Result of @@ -39,13 +39,14 @@ map(Result, Fun) -> {ok, X} -> {ok, Fun(X)}; - {error, _} -> - Result + {error, E} -> + {error, E} end. -ifdef(TEST). map_test() -> expect:equal(map({ok, 1}, fun(X) -> X + 1 end), {ok, 2}), + expect:equal(map({ok, 1}, fun(_) -> <<"2">> end), {ok, <<"2">>}), expect:equal(map({error, 1}, fun(X) -> X + 1 end), {error, 1}). -endif. @@ -81,7 +82,7 @@ flatten_test() -> expect:equal(flatten({error, {error, 1}}), {error, {error, 1}}). -endif. -flat_map(Result, Fun) -> +then(Result, Fun) -> case Result of {ok, X} -> case Fun(X) of @@ -97,10 +98,10 @@ flat_map(Result, Fun) -> end. -ifdef(TEST). -flat_map_test() -> - expect:equal(flat_map({error, 1}, fun(X) -> {ok, X + 1} end), {error, 1}), - expect:equal(flat_map({ok, 1}, fun(X) -> {ok, X + 1} end), {ok, 2}), - expect:equal(flat_map({ok, 1}, fun(_) -> {error, 1} end), {error, 1}). +then_test() -> + expect:equal(then({error, 1}, fun(X) -> {ok, X + 1} end), {error, 1}), + expect:equal(then({ok, 1}, fun(X) -> {ok, X + 1} end), {ok, 2}), + expect:equal(then({ok, 1}, fun(_) -> {error, 1} end), {error, 1}). -endif. unwrap(Result, Default) -> |