diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-15 21:21:55 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-15 21:21:55 +0100 |
commit | 103b76f91d4e6c4ef82a51c39895f0d025234e22 (patch) | |
tree | 7a5aaaee895a7a31b2ddafb7b9249f2efdb520ce /src/gleam_stdlib.erl | |
parent | bc0768008492566626b8570766e0acaffcf3ed5d (diff) | |
download | gleam_stdlib-103b76f91d4e6c4ef82a51c39895f0d025234e22.tar.gz gleam_stdlib-103b76f91d4e6c4ef82a51c39895f0d025234e22.zip |
Use Erlang uri parser
Diffstat (limited to 'src/gleam_stdlib.erl')
-rw-r--r-- | src/gleam_stdlib.erl | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index f607497..9a27c18 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -6,7 +6,7 @@ decode_float/1, decode_thunk/1, decode_list/1, decode_option/2, decode_field/2, parse_int/1, parse_float/1, less_than/2, string_pop_grapheme/1, string_starts_with/2, wrap_list/1, - string_ends_with/2, string_pad/4, decode_map/1, + string_ends_with/2, string_pad/4, decode_map/1, uri_parse/1, bit_string_int_to_u32/1, bit_string_int_from_u32/1, decode_result/1, bit_string_slice/3, decode_bit_string/1, compile_regex/2, regex_scan/2, percent_encode/1, percent_decode/1, regex_check/2, regex_split/2, @@ -298,3 +298,39 @@ check_utf8(Cs) -> {error, _, _} -> {error, nil}; _ -> {ok, Cs} end. + +uri_parse(String) -> + case uri_string:parse(String) of + {error, _, _} -> {error, nil}; + Uri -> + % #{ + % host := Host, path := Path, port := Port, query := Query, + % scheme := Scheme, userinfo := Userinfo + % } -> + % scheme: Option(String), + % userinfo: Option(String), + % host: Option(String), + % port: Option(Int), + % path: String, + % query: Option(String), + % fragment: Option(String), + {ok, {uri, + maps_get_optional(Uri, scheme), + maps_get_optional(Uri, userinfo), + maps_get_optional(Uri, host), + maps_get_optional(Uri, port), + maps_get_or(Uri, path, <<>>), + maps_get_optional(Uri, query), + maps_get_optional(Uri, fragment) + }} + end. + +maps_get_optional(Map, Key) -> + try {some, maps:get(Key, Map)} + catch _:_ -> none + end. + +maps_get_or(Map, Key, Default) -> + try maps:get(Key, Map) + catch _:_ -> Default + end. |