aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-09-15 18:39:40 +0100
committerLouis Pilfold <louis@lpil.uk>2021-09-15 18:39:40 +0100
commitbc0768008492566626b8570766e0acaffcf3ed5d (patch)
tree72e36b51afa2f432367642e2b6dce2072f033a8e /src
parentd0890ff494b666f4d79f40b9d451f6f92402159e (diff)
downloadgleam_stdlib-bc0768008492566626b8570766e0acaffcf3ed5d.tar.gz
gleam_stdlib-bc0768008492566626b8570766e0acaffcf3ed5d.zip
uri.parse returns a result
Diffstat (limited to 'src')
-rw-r--r--src/gleam/uri.gleam35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam
index 6a931e4..caa02f9 100644
--- a/src/gleam/uri.gleam
+++ b/src/gleam/uri.gleam
@@ -33,8 +33,6 @@ pub type Uri {
)
}
-// Special thanks to Elixir for this algorithm
-//
/// Parses a compliant URI string into the `Uri` Type.
/// If the string is not a valid URI string then an error is returned.
///
@@ -48,7 +46,11 @@ pub type Uri {
/// Ok(Uri(scheme: Some("https"), ...))
/// ```
///
-pub fn parse(uri_string: String) -> Uri {
+pub fn parse(uri_string: String) -> Result(Uri, Nil) {
+ do_parse(uri_string)
+}
+
+fn do_parse(uri_string: String) -> Result(Uri, Nil) {
// From https://tools.ietf.org/html/rfc3986#appendix-B
let pattern =
// 12 3 4 5 6 7 8
@@ -89,23 +91,14 @@ pub fn parse(uri_string: String) -> Uri {
|> result.map(pair.second)
|> option.from_result
let port = case port {
- None ->
- case scheme {
- Some("ftp") -> Some(21)
- Some("sftp") -> Some(22)
- Some("tftp") -> Some(69)
- Some("http") -> Some(80)
- Some("https") -> Some(443)
- Some("ldap") -> Some(389)
- _ -> None
- }
+ None -> default_port(scheme)
_ -> port
}
let scheme =
scheme
|> noneify_empty_string
|> option.map(string.lowercase)
- Uri(
+ Ok(Uri(
scheme: scheme,
userinfo: userinfo,
host: host,
@@ -113,7 +106,19 @@ pub fn parse(uri_string: String) -> Uri {
path: path,
query: query,
fragment: fragment,
- )
+ ))
+}
+
+fn default_port(scheme: Option(String)) -> Option(Int) {
+ case scheme {
+ Some("ftp") -> Some(21)
+ Some("sftp") -> Some(22)
+ Some("tftp") -> Some(69)
+ Some("http") -> Some(80)
+ Some("https") -> Some(443)
+ Some("ldap") -> Some(389)
+ _ -> None
+ }
}
fn regex_submatches(pattern: String, string: String) -> List(Option(String)) {