diff options
-rw-r--r-- | src/gleam/gleam_uri_native.erl | 10 | ||||
-rw-r--r-- | src/gleam/uri.gleam | 29 |
2 files changed, 28 insertions, 11 deletions
diff --git a/src/gleam/gleam_uri_native.erl b/src/gleam/gleam_uri_native.erl index e0f9d2d..5d225cb 100644 --- a/src/gleam/gleam_uri_native.erl +++ b/src/gleam/gleam_uri_native.erl @@ -1,5 +1,5 @@ -module (gleam_uri_native). --export ([parse/1, to_string/1, parse_query/1, query_to_string/1]). +-export ([parse/1, to_string/1, parse_query/1]). find_key(Key, Map) -> case maps:find(Key, Map) of @@ -43,11 +43,3 @@ parse_query(String) -> Parts -> {ok, Parts} end. - -query_to_string(Parts) -> - case uri_string:compose_query(Parts, [{encoding, utf8}]) of - String when is_binary(String) -> - String; - % Return value when empty - [] -> <<"">> - end. diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 84b2360..21145df 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -10,9 +10,11 @@ import gleam/list import gleam/result.{Option} import gleam/string +import gleam/dynamic.{Dynamic} /// Type representing holding the parsed components of an URI. /// All components of a URI are optional, except the path. +/// pub type Uri { Uri( scheme: Option(String), @@ -29,6 +31,7 @@ pub type Uri { /// If the string is not a valid URI string then an error is returned. /// /// The opposite operation is `uri.to_string` +/// pub external fn parse(String) -> Result(Uri, Nil) = "gleam_uri_native" "parse" @@ -36,16 +39,36 @@ pub external fn parse(String) -> Result(Uri, Nil) = /// Returns an error for invalid encoding. /// /// The opposite operation is `uri.query_to_string`. +/// pub external fn parse_query( String, ) -> Result(List(tuple(String, String)), Nil) = "gleam_uri_native" "parse_query" +type Encoding { + Utf8 +} + +type ErlQueryToStringOption { + Encoding(Encoding) +} + +external fn erl_query_to_string( + List(tuple(String, String)), + List(ErlQueryToStringOption), +) -> Dynamic = + "uri_string" "compose_query" + /// Encode a list of key value pairs as a URI query string. /// /// The opposite operation is `uri.parse_query`. -pub external fn query_to_string(List(tuple(String, String))) -> String = - "gleam_uri_native" "query_to_string" +/// +pub fn query_to_string(query: List(tuple(String, String))) -> String { + query + |> erl_query_to_string([Encoding(Utf8)]) + |> dynamic.string + |> result.unwrap("") +} fn do_path_segments(input, accumulator) { case input { @@ -67,6 +90,7 @@ fn do_path_segments(input, accumulator) { /// /// Removes empty segments and resolves dot-segments as specified in /// [section 5.2](https://www.ietf.org/rfc/rfc3986.html#section-5.2) of the RFC. +/// pub fn path_segments(path) { do_path_segments(string.split(path, "/"), []) } @@ -74,5 +98,6 @@ pub fn path_segments(path) { /// Encode a `Uri` value as a URI string. /// /// The opposite operation is `uri.parse`. +/// pub external fn to_string(Uri) -> String = "gleam_uri_native" "to_string" |