diff options
author | Peter Saxton <peterhsaxton@gmail.com> | 2020-07-01 14:51:10 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-07-01 18:52:36 +0100 |
commit | be064d04b749e206fcd0b69bc6778500a42d70d8 (patch) | |
tree | 410c2216f737546b5383cb9e7bc0b23b88208a44 | |
parent | 7d1366b6787b869a78cf8874e521634d2d0aaa95 (diff) | |
download | gleam_stdlib-be064d04b749e206fcd0b69bc6778500a42d70d8.tar.gz gleam_stdlib-be064d04b749e206fcd0b69bc6778500a42d70d8.zip |
use bit_string fn
-rw-r--r-- | src/gleam/dynamic.gleam | 35 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 5 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 4 |
3 files changed, 19 insertions, 25 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 4e8e3e8..648ad0a 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -1,4 +1,4 @@ -import gleam/bit_string.{BitString} +import gleam/bit_string.{BitString} as bit_string_mod import gleam/list as list_mod import gleam/atom import gleam/map.{Map} @@ -27,8 +27,19 @@ pub external fn from(a) -> Dynamic = pub external fn unsafe_coerce(Dynamic) -> a = "gleam_stdlib" "identity" -external fn erl_string(from: Dynamic) -> Result(BitString, String) = - "gleam_stdlib" "decode_string" +/// 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. @@ -42,10 +53,10 @@ external fn erl_string(from: Dynamic) -> Result(BitString, String) = /// Error("Expected a String, got `123`") /// pub fn string(from: Dynamic) -> Result(String, String) { - erl_string(from) + bit_string(from) |> result.then( fn(raw) { - case bit_string.to_string(raw) { + case bit_string_mod.to_string(raw) { Ok(string) -> Ok(string) Error(Nil) -> Error("Expected a string, got a bit_string") } @@ -53,20 +64,6 @@ pub fn string(from: Dynamic) -> Result(String, String) { ) } -/// 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 an int, and return the int if it /// is. /// diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index fa5f8fd..fa7f683 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -4,7 +4,7 @@ -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, iodata_prepend/2, identity/1, - decode_int/1, decode_string/1, decode_bool/1, decode_float/1, + decode_int/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2, string_pop_grapheme/1, string_starts_with/2, string_ends_with/2, @@ -65,9 +65,6 @@ 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). -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). diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index 9416e2f..2b272d4 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -52,12 +52,12 @@ pub fn string_test() { 1 |> dynamic.from |> dynamic.string - |> should.equal(Error("Expected a string, got an int")) + |> should.equal(Error("Expected a bit_string, got an int")) [] |> dynamic.from |> dynamic.string - |> should.equal(Error("Expected a string, got a list")) + |> should.equal(Error("Expected a bit_string, got a list")) } pub fn int_test() { |