diff options
author | Peter Saxton <peterhsaxton@gmail.com> | 2020-06-12 09:11:46 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-06-16 10:21:40 +0100 |
commit | 803b340b31fd1c8cf4a84e4b50fae03f852f4b4b (patch) | |
tree | 72440d782b8d142bb0f576a09b9f95ead6599499 | |
parent | c28b8a505d44189824e97b0ba2680e5775cefc60 (diff) | |
download | gleam_stdlib-803b340b31fd1c8cf4a84e4b50fae03f852f4b4b.tar.gz gleam_stdlib-803b340b31fd1c8cf4a84e4b50fae03f852f4b4b.zip |
add decode bit_string to dynamic
-rw-r--r-- | src/gleam/dynamic.gleam | 15 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 6 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 24 |
3 files changed, 44 insertions, 1 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 6b54df1..f1c8f2d 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -1,3 +1,4 @@ +import gleam/bit_string.{BitString} import gleam/list as list_mod import gleam/atom import gleam/map.{Map} @@ -26,6 +27,20 @@ pub external fn from(a) -> Dynamic = pub external fn unsafe_coerce(Dynamic) -> a = "gleam_stdlib" "identity" +/// Check to see whether a Dynamic value is a bit_string, and return the bit_string if +/// it is. +/// +/// ## Examples +/// +/// > bit_string(from("Hello")) == bit_string.from_string("Hello") +/// True +/// +/// > bit_string(from(123)) +/// Error("Expected a BitString, got `123`") +/// +pub external fn bit_string(from: Dynamic) -> Result(BitString, String) = + "gleam_stdlib" "decode_bit_string" + /// Check to see whether a Dynamic value is a string, and return the string if /// it is. /// diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index a5dd804..fc53294 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -9,7 +9,8 @@ decode_element/2, parse_int/1, parse_float/1, compare_strings/2, string_pop_grapheme/1, string_starts_with/2, string_ends_with/2, string_pad/4, decode_tuple2/1, decode_map/1, bit_string_int_to_u32/1, - bit_string_int_from_u32/1, bit_string_append/2, bit_string_part_/3]). + bit_string_int_from_u32/1, bit_string_append/2, bit_string_part_/3, + decode_bit_string/1]). should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). @@ -60,6 +61,9 @@ 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_binary(Data) -> {ok, Data}; +decode_bit_string(Data) -> decode_error_msg("a bit_string", Data). + decode_string(Data) when is_binary(Data) -> {ok, Data}; decode_string(Data) -> decode_error_msg("a string", Data). diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index 410ae0e..8a43f69 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -1,3 +1,4 @@ +import gleam/bit_string import gleam/dynamic import gleam/atom import gleam/list @@ -5,6 +6,29 @@ import gleam/should import gleam/result import gleam/map + +pub fn bit_string_test() { + "" + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Ok(bit_string.from_string(""))) + + "Hello" + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Ok(bit_string.from_string("Hello"))) + + 1 + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Error("Expected a bit_string, got an int")) + + [] + |> dynamic.from + |> dynamic.bit_string + |> should.equal(Error("Expected a bit_string, got a list")) +} + pub fn string_test() { "" |> dynamic.from |