@@ -27,33 +27,45 @@ pub external fn from(a) -> Dynamic =
27
27
pub external fn unsafe_coerce ( Dynamic ) -> a =
28
28
"gleam_stdlib" "identity"
29
29
30
- /// Check to see whether a Dynamic value is a bit_string, and return the bit_string if
30
+ external fn erl_string ( from : Dynamic ) -> Result ( BitString , String ) =
31
+ "gleam_stdlib" "decode_string"
32
+
33
+ /// Check to see whether a Dynamic value is a string, and return the string if
31
34
/// it is.
32
35
///
33
36
/// ## Examples
34
37
///
35
- /// > bit_string (from("Hello")) == bit_string.from_string("Hello" )
36
- /// True
38
+ /// > string (from("Hello"))
39
+ /// Ok("Hello")
37
40
///
38
- /// > bit_string (from(123))
39
- /// Error("Expected a BitString , got `123`")
41
+ /// > string (from(123))
42
+ /// Error("Expected a String , got `123`")
40
43
///
41
- pub external fn bit_string ( from : Dynamic ) -> Result ( BitString , String ) =
42
- "gleam_stdlib" "decode_bit_string"
44
+ pub fn string ( from : Dynamic ) -> Result ( String , String ) {
45
+ erl_string ( from )
46
+ |> result . then (
47
+ fn ( raw ) {
48
+ case bit_string . is_utf8 ( raw ) {
49
+ True -> Ok ( raw )
50
+ False -> Error ( "Expected a string, got a bit_string" )
51
+ }
52
+ } ,
53
+ )
54
+ }
43
55
44
- /// Check to see whether a Dynamic value is a string , and return the string if
56
+ /// Check to see whether a Dynamic value is a bit_string , and return the bit_string if
45
57
/// it is.
46
58
///
47
59
/// ## Examples
48
60
///
49
- /// > string (from("Hello"))
50
- /// Ok("Hello")
61
+ /// > bit_string (from("Hello")) == bit_string.from_string("Hello" )
62
+ /// True
51
63
///
52
- /// > string (from(123))
53
- /// Error("Expected a String , got `123`")
64
+ /// > bit_string (from(123))
65
+ /// Error("Expected a BitString , got `123`")
54
66
///
55
- pub external fn string ( from : Dynamic ) -> Result ( String , String ) =
56
- "gleam_stdlib" "decode_string "
67
+ pub external fn bit_string ( from : Dynamic ) -> Result ( BitString , String ) =
68
+ "gleam_stdlib" "decode_bit_string "
57
69
58
70
/// Check to see whether a Dynamic value is an int, and return the int if it
59
71
/// is.
0 commit comments