aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dynamic.gleam15
-rw-r--r--src/gleam_stdlib.erl6
2 files changed, 20 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).