Skip to content

Commit be064d0

Browse files
CrowdHailerlpil
authored andcommitted
use bit_string fn
1 parent 7d1366b commit be064d0

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/gleam/dynamic.gleam

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import gleam/bit_string.{BitString}
1+
import gleam/bit_string.{BitString} as bit_string_mod
22
import gleam/list as list_mod
33
import gleam/atom
44
import gleam/map.{Map}
@@ -27,8 +27,19 @@ pub external fn from(a) -> Dynamic =
2727
pub external fn unsafe_coerce(Dynamic) -> a =
2828
"gleam_stdlib" "identity"
2929

30-
external fn erl_string(from: Dynamic) -> Result(BitString, String) =
31-
"gleam_stdlib" "decode_string"
30+
/// Check to see whether a Dynamic value is a bit_string, and return the bit_string if
31+
/// it is.
32+
///
33+
/// ## Examples
34+
///
35+
/// > bit_string(from("Hello")) == bit_string.from_string("Hello")
36+
/// True
37+
///
38+
/// > bit_string(from(123))
39+
/// Error("Expected a BitString, got `123`")
40+
///
41+
pub external fn bit_string(from: Dynamic) -> Result(BitString, String) =
42+
"gleam_stdlib" "decode_bit_string"
3243

3344
/// Check to see whether a Dynamic value is a string, and return the string if
3445
/// it is.
@@ -42,31 +53,17 @@ external fn erl_string(from: Dynamic) -> Result(BitString, String) =
4253
/// Error("Expected a String, got `123`")
4354
///
4455
pub fn string(from: Dynamic) -> Result(String, String) {
45-
erl_string(from)
56+
bit_string(from)
4657
|> result.then(
4758
fn(raw) {
48-
case bit_string.to_string(raw) {
59+
case bit_string_mod.to_string(raw) {
4960
Ok(string) -> Ok(string)
5061
Error(Nil) -> Error("Expected a string, got a bit_string")
5162
}
5263
},
5364
)
5465
}
5566

56-
/// Check to see whether a Dynamic value is a bit_string, and return the bit_string if
57-
/// it is.
58-
///
59-
/// ## Examples
60-
///
61-
/// > bit_string(from("Hello")) == bit_string.from_string("Hello")
62-
/// True
63-
///
64-
/// > bit_string(from(123))
65-
/// Error("Expected a BitString, got `123`")
66-
///
67-
pub external fn bit_string(from: Dynamic) -> Result(BitString, String) =
68-
"gleam_stdlib" "decode_bit_string"
69-
7067
/// Check to see whether a Dynamic value is an int, and return the int if it
7168
/// is.
7269
///

src/gleam_stdlib.erl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-export([should_equal/2, should_not_equal/2, should_be_ok/1, should_be_error/1,
55
atom_from_string/1, atom_create_from_string/1, atom_to_string/1,
66
map_get/2, iodata_append/2, iodata_prepend/2, identity/1,
7-
decode_int/1, decode_string/1, decode_bool/1, decode_float/1,
7+
decode_int/1, decode_bool/1, decode_float/1,
88
decode_thunk/1, decode_atom/1, decode_list/1, decode_field/2,
99
decode_element/2, parse_int/1, parse_float/1, compare_strings/2,
1010
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).
6565
decode_bit_string(Data) when is_bitstring(Data) -> {ok, Data};
6666
decode_bit_string(Data) -> decode_error_msg("a bit_string", Data).
6767

68-
decode_string(Data) when is_binary(Data) -> {ok, Data};
69-
decode_string(Data) -> decode_error_msg("a string", Data).
70-
7168
decode_int(Data) when is_integer(Data) -> {ok, Data};
7269
decode_int(Data) -> decode_error_msg("an int", Data).
7370

test/gleam/dynamic_test.gleam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ pub fn string_test() {
5252
1
5353
|> dynamic.from
5454
|> dynamic.string
55-
|> should.equal(Error("Expected a string, got an int"))
55+
|> should.equal(Error("Expected a bit_string, got an int"))
5656

5757
[]
5858
|> dynamic.from
5959
|> dynamic.string
60-
|> should.equal(Error("Expected a string, got a list"))
60+
|> should.equal(Error("Expected a bit_string, got a list"))
6161
}
6262

6363
pub fn int_test() {

0 commit comments

Comments
 (0)