From 4dc0d1854fa7dfc0229e27b6b69848564185e43b Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Tue, 17 Sep 2019 21:04:30 +0100 Subject: Rename stdlib native module --- src/gleam/any.gleam | 24 ++++++------- src/gleam/atom.gleam | 6 ++-- src/gleam/expect.gleam | 12 +++---- src/gleam/float.gleam | 2 +- src/gleam/int.gleam | 2 +- src/gleam/iodata.gleam | 14 ++++---- src/gleam/map.gleam | 2 +- src/gleam/string.gleam | 3 -- src/gleam__stdlib.erl | 92 -------------------------------------------------- src/gleam_stdlib.erl | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 123 insertions(+), 126 deletions(-) delete mode 100644 src/gleam__stdlib.erl create mode 100644 src/gleam_stdlib.erl (limited to 'src') diff --git a/src/gleam/any.gleam b/src/gleam/any.gleam index 902fb5c..a8358ac 100644 --- a/src/gleam/any.gleam +++ b/src/gleam/any.gleam @@ -11,35 +11,35 @@ pub external type Any; // Convert any Gleam data into `Any` data. // -pub external fn from(a) -> Any = "gleam__stdlib" "identity"; +pub external fn from(a) -> Any = "gleam_stdlib" "identity"; // Unsafely cast any type into any other type. // // This is an escape hatch for the type system that may be useful when wrapping // native Erlang APIs. It is to be used as a last measure only. // -pub external fn unsafe_coerce(a) -> b = "gleam__stdlib" "identity"; +pub external fn unsafe_coerce(a) -> b = "gleam_stdlib" "identity"; pub external fn string(Any) -> Result(String, String) - = "gleam__stdlib" "decode_string" + = "gleam_stdlib" "decode_string" pub external fn int(Any) -> Result(Int, String) - = "gleam__stdlib" "decode_int" + = "gleam_stdlib" "decode_int" pub external fn float(Any) -> Result(Float, String) - = "gleam__stdlib" "decode_float" + = "gleam_stdlib" "decode_float" pub external fn atom(Any) -> Result(atom.Atom, String) - = "gleam__stdlib" "decode_atom" + = "gleam_stdlib" "decode_atom" pub external fn bool(Any) -> Result(Bool, String) - = "gleam__stdlib" "decode_bool" + = "gleam_stdlib" "decode_bool" pub external fn thunk(Any) -> Result(fn() -> Any, String) - = "gleam__stdlib" "decode_thunk" + = "gleam_stdlib" "decode_thunk" -external fn list_any(Any) -> Result(List(Any), String) = - "gleam__stdlib" "decode_list" +external fn list_any(Any) -> Result(List(Any), String) + = "gleam_stdlib" "decode_list" pub fn list(any, decode) { any @@ -48,7 +48,7 @@ pub fn list(any, decode) { } pub external fn pair(Any) -> Result(pair.Pair(Any, Any), String) - = "gleam__stdlib" "decode_pair" + = "gleam_stdlib" "decode_pair" pub external fn field(Any, a) -> Result(Any, String) - = "gleam__stdlib" "decode_field" + = "gleam_stdlib" "decode_field" diff --git a/src/gleam/atom.gleam b/src/gleam/atom.gleam index 67d040d..24b82d5 100644 --- a/src/gleam/atom.gleam +++ b/src/gleam/atom.gleam @@ -4,14 +4,14 @@ pub enum AtomNotLoaded = | AtomNotLoaded pub external fn from_string(String) -> Result(Atom, AtomNotLoaded) = - "gleam__stdlib" "atom_from_string"; + "gleam_stdlib" "atom_from_string"; // This function can create a new atom if one does not already exist for // the given string. Atoms are not garbage collected so this can result // in a memory leak if called over time on new values // pub external fn create_from_string(String) -> Atom = - "gleam__stdlib" "atom_create_from_string"; + "gleam_stdlib" "atom_create_from_string"; pub external fn to_string(Atom) -> String = - "gleam__stdlib" "atom_to_string"; + "gleam_stdlib" "atom_to_string"; diff --git a/src/gleam/expect.gleam b/src/gleam/expect.gleam index 5ea6a93..a42a02e 100644 --- a/src/gleam/expect.gleam +++ b/src/gleam/expect.gleam @@ -3,17 +3,17 @@ pub external type Expectation; -pub external fn equal(a, a) -> Expectation = "gleam__stdlib" "expect_equal"; +pub external fn equal(a, a) -> Expectation = "gleam_stdlib" "expect_equal"; -pub external fn not_equal(a, a) -> Expectation = "gleam__stdlib" "expect_not_equal"; +pub external fn not_equal(a, a) -> Expectation = "gleam_stdlib" "expect_not_equal"; -pub external fn true(Bool) -> Expectation = "gleam__stdlib" "expect_true"; +pub external fn true(Bool) -> Expectation = "gleam_stdlib" "expect_true"; -pub external fn false(Bool) -> Expectation = "gleam__stdlib" "expect_false"; +pub external fn false(Bool) -> Expectation = "gleam_stdlib" "expect_false"; -pub external fn is_ok(Result(a, b)) -> Expectation = "gleam__stdlib" "expect_is_ok"; +pub external fn is_ok(Result(a, b)) -> Expectation = "gleam_stdlib" "expect_is_ok"; -pub external fn is_error(Result(a, b)) -> Expectation = "gleam__stdlib" "expect_is_error"; +pub external fn is_error(Result(a, b)) -> Expectation = "gleam_stdlib" "expect_is_error"; pub fn fail() { true(False) diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 4b6d589..9095ec4 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -5,7 +5,7 @@ pub enum NotAFloat = | NotAFloat pub external fn parse(String) -> Result(Float, NotAFloat) = - "gleam__stdlib" "parse_float"; + "gleam_stdlib" "parse_float"; pub fn to_string(f) { f diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 7a83fba..10731a3 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -1,6 +1,6 @@ import gleam/order -pub external fn parse(String) -> Result(Int, Nil) = "gleam__stdlib" "parse_int"; +pub external fn parse(String) -> Result(Int, Nil) = "gleam_stdlib" "parse_int"; pub external fn to_string(Int) -> String = "erlang" "integer_to_binary" diff --git a/src/gleam/iodata.gleam b/src/gleam/iodata.gleam index 56efc65..e1b017e 100644 --- a/src/gleam/iodata.gleam +++ b/src/gleam/iodata.gleam @@ -1,25 +1,25 @@ pub external type Iodata; pub external fn prepend(Iodata, String) -> Iodata = - "gleam__stdlib" "iodata_prepend"; + "gleam_stdlib" "iodata_prepend"; pub external fn append(Iodata, String) -> Iodata = - "gleam__stdlib" "iodata_append"; + "gleam_stdlib" "iodata_append"; pub external fn prepend_iodata(Iodata, Iodata) -> Iodata = - "gleam__stdlib" "iodata_prepend"; + "gleam_stdlib" "iodata_prepend"; pub external fn append_iodata(Iodata, Iodata) -> Iodata = - "gleam__stdlib" "iodata_append"; + "gleam_stdlib" "iodata_append"; pub external fn from_strings(List(String)) -> Iodata = - "gleam__stdlib" "identity"; + "gleam_stdlib" "identity"; pub external fn concat(List(Iodata)) -> Iodata = - "gleam__stdlib" "identity"; + "gleam_stdlib" "identity"; pub external fn new(String) -> Iodata = - "gleam__stdlib" "identity"; + "gleam_stdlib" "identity"; pub external fn to_string(Iodata) -> String = "erlang" "iolist_to_binary"; diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam index b46590d..af49d5e 100644 --- a/src/gleam/map.gleam +++ b/src/gleam/map.gleam @@ -25,7 +25,7 @@ pub external fn new() -> MapDict(key, value) = "maps" "new" pub external fn fetch(MapDict(key, value), key) -> Result(value, Nil) - = "gleam__stdlib" "map_fetch"; + = "gleam_stdlib" "map_fetch"; external fn erl_put(key, value, MapDict(key, value)) -> MapDict(key, value) = "maps" "put"; diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 10de2b7..9714220 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -3,9 +3,6 @@ import gleam/list pub external fn length(String) -> Int = "string" "length" -pub enum ParseError = - | ParseError - pub external fn lowercase(String) -> String = "string" "lowercase" pub external fn uppercase(String) -> String = "string" "uppercase" diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl deleted file mode 100644 index b9236ed..0000000 --- a/src/gleam__stdlib.erl +++ /dev/null @@ -1,92 +0,0 @@ --module(gleam__stdlib). --include_lib("eunit/include/eunit.hrl"). - --export([expect_equal/2, expect_not_equal/2, expect_true/1, expect_false/1, - expect_is_ok/1, expect_is_error/1, atom_from_string/1, - atom_create_from_string/1, atom_to_string/1, map_fetch/2, - iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, - decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, - decode_pair/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]). - -expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). -expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). -expect_true(A) -> ?assert(A). -expect_false(A) -> ?assertNot(A). -expect_is_ok(A) -> ?assertMatch({ok, _}, A). -expect_is_error(A) -> ?assertMatch({error, _}, A). - -map_fetch(Map, Key) -> - case maps:find(Key, Map) of - error -> {error, nil}; - 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]. -iodata_prepend(Iodata, String) -> [String, Iodata]. - -identity(X) -> X. - -decode_error_msg(Type, Data) -> - {error, iolist_to_binary(io_lib:format("Expected ~s, got `~p`", [Type, Data]))}. - -decode_atom(Data) when is_atom(Data) -> {ok, Data}; -decode_atom(Data) -> decode_error_msg("an Atom", Data). - -decode_string(Data) when is_binary(Data) -> {ok, Data}; -decode_string(Data) -> decode_error_msg("a String", Data). - -decode_int(Data) when is_integer(Data) -> {ok, Data}; -decode_int(Data) -> decode_error_msg("an Int", Data). - -decode_float(Data) when is_float(Data) -> {ok, Data}; -decode_float(Data) -> decode_error_msg("a Float", Data). - -decode_bool(Data) when is_boolean(Data) -> {ok, Data}; -decode_bool(Data) -> decode_error_msg("a Bool", Data). - -decode_thunk(Data) when is_function(Data, 0) -> {ok, Data}; -decode_thunk(Data) -> decode_error_msg("a zero arity function", Data). - -decode_pair(Data = {_, _}) -> {ok, Data}; -decode_pair(Data) -> decode_error_msg("a 2 element tuple", Data). - -decode_list(Data) when is_list(Data) -> {ok, Data}; -decode_list(Data) -> decode_error_msg("a List", Data). - -decode_field(Data, Key) -> - case Data of - #{Key := Value} -> - {ok, Value}; - - _ -> - decode_error_msg(io_lib:format("a map with key `~p`", [Key]), Data) - end. - -parse_int(String) -> - case string:to_integer(binary:bin_to_list(String)) of - {Integer, []} -> - {ok, Integer}; - - _ -> - {error, nil} - end. - -parse_float(String) -> - case string:to_float(binary:bin_to_list(String)) of - {Float, []} -> - {ok, Float}; - - _ -> - {error, parse_error} - end. diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl new file mode 100644 index 0000000..480518a --- /dev/null +++ b/src/gleam_stdlib.erl @@ -0,0 +1,92 @@ +-module(gleam_stdlib). +-include_lib("eunit/include/eunit.hrl"). + +-export([expect_equal/2, expect_not_equal/2, expect_true/1, expect_false/1, + expect_is_ok/1, expect_is_error/1, atom_from_string/1, + atom_create_from_string/1, atom_to_string/1, map_fetch/2, + iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, + decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, + decode_pair/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]). + +expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). +expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). +expect_true(A) -> ?assert(A). +expect_false(A) -> ?assertNot(A). +expect_is_ok(A) -> ?assertMatch({ok, _}, A). +expect_is_error(A) -> ?assertMatch({error, _}, A). + +map_fetch(Map, Key) -> + case maps:find(Key, Map) of + error -> {error, nil}; + 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]. +iodata_prepend(Iodata, String) -> [String, Iodata]. + +identity(X) -> X. + +decode_error_msg(Type, Data) -> + {error, iolist_to_binary(io_lib:format("Expected ~s, got `~p`", [Type, Data]))}. + +decode_atom(Data) when is_atom(Data) -> {ok, Data}; +decode_atom(Data) -> decode_error_msg("an Atom", Data). + +decode_string(Data) when is_binary(Data) -> {ok, Data}; +decode_string(Data) -> decode_error_msg("a String", Data). + +decode_int(Data) when is_integer(Data) -> {ok, Data}; +decode_int(Data) -> decode_error_msg("an Int", Data). + +decode_float(Data) when is_float(Data) -> {ok, Data}; +decode_float(Data) -> decode_error_msg("a Float", Data). + +decode_bool(Data) when is_boolean(Data) -> {ok, Data}; +decode_bool(Data) -> decode_error_msg("a Bool", Data). + +decode_thunk(Data) when is_function(Data, 0) -> {ok, Data}; +decode_thunk(Data) -> decode_error_msg("a zero arity function", Data). + +decode_pair(Data = {_, _}) -> {ok, Data}; +decode_pair(Data) -> decode_error_msg("a 2 element tuple", Data). + +decode_list(Data) when is_list(Data) -> {ok, Data}; +decode_list(Data) -> decode_error_msg("a List", Data). + +decode_field(Data, Key) -> + case Data of + #{Key := Value} -> + {ok, Value}; + + _ -> + decode_error_msg(io_lib:format("a map with key `~p`", [Key]), Data) + end. + +parse_int(String) -> + case string:to_integer(binary:bin_to_list(String)) of + {Integer, []} -> + {ok, Integer}; + + _ -> + {error, nil} + end. + +parse_float(String) -> + case string:to_float(binary:bin_to_list(String)) of + {Float, []} -> + {ok, Float}; + + _ -> + {error, parse_error} + end. -- cgit v1.2.3