aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.erl
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-07-31 11:26:38 +0100
committerLouis Pilfold <louis@lpil.uk>2021-07-31 11:26:38 +0100
commitf78920f014e2e2b3d512bd5703902bbdba68eb4f (patch)
treea8dbc823a3223fb67f2d8e34ef4287f4de3f161d /src/gleam_stdlib.erl
parent942521b3016af2f24bff4eb1237c01a7856ffea5 (diff)
downloadgleam_stdlib-f78920f014e2e2b3d512bd5703902bbdba68eb4f.tar.gz
gleam_stdlib-f78920f014e2e2b3d512bd5703902bbdba68eb4f.zip
Move atom module to gleam_erlang library
Diffstat (limited to 'src/gleam_stdlib.erl')
-rw-r--r--src/gleam_stdlib.erl45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 49a3b0c..ff8b97b 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -2,14 +2,13 @@
-include_lib("eunit/include/eunit.hrl").
-export([should_equal/2, should_not_equal/2, should_be_ok/1, should_be_error/1,
- atom_from_string/1, atom_create_from_string/1, atom_to_string/1,
map_get/2, iodata_append/2, identity/1, decode_int/1, decode_bool/1,
- decode_float/1, decode_thunk/1, decode_atom/1, decode_list/1,
+ decode_float/1, decode_thunk/1, decode_list/1, decode_optional/2,
decode_field/2, decode_element/2, parse_int/1, parse_float/1,
less_than/2, string_pop_grapheme/1, string_starts_with/2,
string_ends_with/2, string_pad/4, decode_tuple2/1, decode_tuple3/1,
decode_tuple4/1, decode_tuple5/1, decode_tuple6/1, decode_map/1,
- bit_string_int_to_u32/1, bit_string_int_from_u32/1,
+ bit_string_int_to_u32/1, bit_string_int_from_u32/1, decode_result/1,
bit_string_append/2, bit_string_part_/3, decode_bit_string/1,
compile_regex/2, regex_match/2, regex_split/2, regex_scan/2,
base_decode64/1, wrap_list/1, get_line/1]).
@@ -33,17 +32,6 @@ map_get(Map, Key) ->
OkFound -> OkFound
end.
-atom_create_from_string(S) ->
- binary_to_atom(S, utf8).
-
-atom_to_string(S) ->
- atom_to_binary(S, utf8).
-
-atom_from_string(S) ->
- try {ok, binary_to_existing_atom(S, utf8)}
- catch error:badarg -> {error, atom_not_loaded}
- end.
-
iodata_append(Iodata, String) -> [Iodata, String].
identity(X) -> X.
@@ -79,9 +67,6 @@ decode_tuple6(Data) -> decode_error_msg("a 6 element tuple", Data).
decode_map(Data) when is_map(Data) -> {ok, Data};
decode_map(Data) -> decode_error_msg("a map", Data).
-decode_atom(Data) when is_atom(Data) -> {ok, Data};
-decode_atom(Data) -> decode_error_msg("an atom", Data).
-
decode_bit_string(Data) when is_bitstring(Data) -> {ok, Data};
decode_bit_string(Data) -> decode_error_msg("a bit_string", Data).
@@ -119,6 +104,32 @@ decode_element(Data, Position) when is_tuple(Data) ->
end;
decode_element(Data, _Position) -> decode_error_msg("a tuple", Data).
+decode_optional(Term, F) ->
+ Decode = fun(Inner) ->
+ case F(Inner) of
+ {ok, Decoded} -> {ok, {some, Decoded}};
+ Error -> Error
+ end
+ end,
+ case Term of
+ undefined -> {ok, none};
+ error -> {ok, none};
+ null -> {ok, none};
+ none -> {ok, none};
+ nil -> {ok, none};
+ {some, Inner} -> Decode(Inner);
+ _ -> Decode(Term)
+ end.
+
+decode_result(Term) ->
+ case Term of
+ {ok, Inner} -> {ok, {ok, Inner}};
+ ok -> {ok, {ok, nil}};
+ {error, Inner} -> {ok, {error, Inner}};
+ error -> {ok, {error, nil}};
+ _ -> decode_error_msg("a result tuple", Term)
+ end.
+
parse_int(String) ->
case catch binary_to_integer(String) of
Int when is_integer(Int) -> {ok, Int};