@@ -27,33 +27,45 @@ pub external fn from(a) -> Dynamic =
2727pub external fn unsafe_coerce ( Dynamic ) -> a =
2828 "gleam_stdlib" "identity"
2929
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
3134/// it is.
3235///
3336/// ## Examples
3437///
35- /// > bit_string (from("Hello")) == bit_string.from_string("Hello" )
36- /// True
38+ /// > string (from("Hello"))
39+ /// Ok("Hello")
3740///
38- /// > bit_string (from(123))
39- /// Error("Expected a BitString , got `123`")
41+ /// > string (from(123))
42+ /// Error("Expected a String , got `123`")
4043///
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+ }
4355
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
4557/// it is.
4658///
4759/// ## Examples
4860///
49- /// > string (from("Hello"))
50- /// Ok("Hello")
61+ /// > bit_string (from("Hello")) == bit_string.from_string("Hello" )
62+ /// True
5163///
52- /// > string (from(123))
53- /// Error("Expected a String , got `123`")
64+ /// > bit_string (from(123))
65+ /// Error("Expected a BitString , got `123`")
5466///
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 "
5769
5870/// Check to see whether a Dynamic value is an int, and return the int if it
5971/// is.
0 commit comments