diff --git a/src/at/did.gleam b/src/at/did.gleam index b7635f9..c03679e 100644 --- a/src/at/did.gleam +++ b/src/at/did.gleam @@ -9,7 +9,6 @@ import gleam/int import gleam/json.{type Json} import gleam/list import gleam/option.{type Option, None, Some} -import gleam/result import gleam/string import gleam/uri.{type Uri, Uri} @@ -124,11 +123,7 @@ fn parse_web_host(host: String) -> Result(Did, Nil) { /// /// pub fn url_from_string(value: String) -> Result(Url, Nil) { - let #(authority, path) = - string.split_once(value, "/") - |> result.lazy_or(fn() { string.split_once(value, "?") }) - |> result.lazy_or(fn() { string.split_once(value, "#") }) - |> result.unwrap(#(value, "")) + let #(authority, path) = split_authority(value) case from_string(authority) { Ok(did) -> @@ -144,6 +139,21 @@ pub fn url_from_string(value: String) -> Result(Url, Nil) { } } +fn split_authority(value: String) -> #(String, String) { + case string.split_once(value, "/") { + Ok(#(authority, path)) -> #(authority, path) + Error(_) -> + case string.split_once(value, "?") { + Ok(#(authority, query)) -> #(authority, "?" <> query) + Error(_) -> + case string.split_once(value, "#") { + Ok(#(authority, fragment)) -> #(authority, "#" <> fragment) + Error(_) -> #(value, "") + } + } + } +} + fn parse_path(uri_string: String, url: Url) -> Result(Url, Nil) { do_parse_path(uri_string, uri_string, url, 0) }