diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-22 19:15:04 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-23 16:08:19 +0100 |
commit | 5a6f1f93f87eb2e0ef2718dfdb8a7fd2e7d21346 (patch) | |
tree | 61fda8a5fef66b19604f403905c84518ccb37299 /src | |
parent | a5ca2d8bdefb677c4c4dfac2bfe58149ff195a7e (diff) | |
download | gleam_stdlib-5a6f1f93f87eb2e0ef2718dfdb8a7fd2e7d21346.tar.gz gleam_stdlib-5a6f1f93f87eb2e0ef2718dfdb8a7fd2e7d21346.zip |
Start URI printing
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/should.gleam | 5 | ||||
-rw-r--r-- | src/gleam/uri.gleam | 110 |
2 files changed, 58 insertions, 57 deletions
diff --git a/src/gleam/should.gleam b/src/gleam/should.gleam index 3566dc2..5a636d0 100644 --- a/src/gleam/should.gleam +++ b/src/gleam/should.gleam @@ -34,11 +34,10 @@ if javascript { True -> Nil _ -> crash(string.concat([ - "\n", + "\n\t", stringify(a), - "\nshould equal \n", + "\n\tshould equal \n\t", stringify(b), - "\n", ])) } } diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index b40b381..882f17e 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -7,31 +7,35 @@ //// Query encoding (Form encoding) is defined in the w3c specification. //// https://www.w3.org/TR/html52/sec-forms.html#urlencoded-form-data +import gleam/option.{None, Option, Some} +import gleam/string +import gleam/int + if erlang { import gleam/list import gleam/result - import gleam/option.{None, Option, Some} - import gleam/string import gleam/dynamic.{Dynamic} import gleam/map.{Map} import gleam/function import gleam/pair +} - /// 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), - userinfo: Option(String), - host: Option(String), - port: Option(Int), - path: String, - query: Option(String), - fragment: Option(String), - ) - } +/// 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), + userinfo: Option(String), + host: Option(String), + port: Option(Int), + path: String, + query: Option(String), + fragment: Option(String), + ) +} +if erlang { pub external fn erl_parse(String) -> Dynamic = "uri_string" "parse" @@ -220,47 +224,45 @@ if erlang { external fn erl_to_string(Map(UriKey, Dynamic)) -> Dynamic = "uri_string" "recompose" +} - /// Encodes a `Uri` value as a URI string. - /// - /// The opposite operation is `uri.parse`. - /// - /// ## Examples - /// - /// ``` - /// > let uri = Uri(Some("http"), None, Some("example.com"), ...) - /// > to_string(uri) - /// - /// "https://example.com" - /// ``` - /// - pub fn to_string(uri: Uri) -> String { - let field = fn(key: UriKey, value: Option(anything)) -> Result( - #(UriKey, Dynamic), - Nil, - ) { - case value { - Some(v) -> Ok(#(key, dynamic.from(v))) - None -> Error(Nil) - } - } - - [ - field(Scheme, uri.scheme), - field(Userinfo, uri.userinfo), - field(Host, uri.host), - field(Port, uri.port), - field(Path, option.Some(uri.path)), - field(Query, uri.query), - field(Fragment, uri.fragment), - ] - |> list.filter_map(fn(x) { x }) - |> map.from_list - |> erl_to_string - |> dynamic.string - |> result.unwrap("") +/// Encodes a `Uri` value as a URI string. +/// +/// The opposite operation is `uri.parse`. +/// +/// ## Examples +/// +/// ``` +/// > let uri = Uri(Some("http"), None, Some("example.com"), ...) +/// > to_string(uri) +/// +/// "https://example.com" +/// ``` +/// +pub fn to_string(uri: Uri) -> String { + // TODO: query + // TODO: fragment + let parts = [] + let parts = [uri.path, ..parts] + let parts = case uri.host, string.starts_with(uri.path, "/") { + Some(_), False -> ["/", ..parts] + _, _ -> parts + } + let parts = case uri.host, uri.port { + Some(_), Some(port) -> [":", int.to_string(port), ..parts] + _, _ -> parts } + let parts = case uri.scheme, uri.userinfo, uri.host { + Some(s), Some(u), Some(h) -> [s, ":", u, "@", h, ..parts] + Some(s), None, Some(h) -> [s, "://", h, ..parts] + Some(s), Some(_), None | Some(s), None, None -> [s, ":", ..parts] + None, None, Some(h) -> ["//", h, ..parts] + None, Some(_), None | None, None, None -> parts + } + string.concat(parts) +} +if erlang { /// Fetches the origin of a uri /// /// Return the origin of a uri as defined in |