Packages
gleam_stdlib
0.16.0
1.0.3
1.0.2
1.0.1
1.0.0
0.71.0
0.70.0
0.69.0
0.68.1
0.68.0
0.67.1
0.67.0
0.65.0
0.64.0
0.63.2
0.63.1
0.63.0
0.62.1
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.2
0.28.1
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.0
0.23.0
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc1
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.4.0-rc1
0.3.1
0.3.0
0.2.0
retired
A standard library for the Gleam programming language
Current section
Files
Jump to
Current section
Files
gen/src/gleam@uri.erl
-module(gleam@uri).
-compile(no_auto_import).
-export([erl_parse/1, parse/1, parse_query/1, query_to_string/1, percent_encode/1, percent_decode/1, path_segments/1, to_string/1, origin/1, merge/2]).
-export_type([uri/0, uri_key/0, encoding/0, erl_query_to_string_option/0]).
-type uri() :: {uri,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(integer()),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type uri_key() :: scheme | userinfo | host | port | path | 'query' | fragment.
-type encoding() :: utf8.
-type erl_query_to_string_option() :: {encoding, encoding()}.
-spec erl_parse(binary()) -> gleam@dynamic:dynamic().
erl_parse(A) ->
uri_string:parse(A).
-spec parse(binary()) -> {ok, uri()} | {error, nil}.
parse(String) ->
case gleam@result:nil_error(gleam@dynamic:map(uri_string:parse(String))) of
{error, Gleam@try_error} -> {error, Gleam@try_error};
{ok, Uri_map} ->
Get = fun(K, Decode_type) ->
gleam@option:from_result(
gleam@result:then(
gleam@map:get(Uri_map, gleam@dynamic:from(K)),
gleam@function:compose(
Decode_type,
fun gleam@result:nil_error/1
)
)
)
end,
Uri = {uri,
Get(scheme, fun gleam@dynamic:string/1),
Get(userinfo, fun gleam@dynamic:string/1),
Get(host, fun gleam@dynamic:string/1),
Get(port, fun gleam@dynamic:int/1),
gleam@option:unwrap(
Get(path, fun gleam@dynamic:string/1),
<<""/utf8>>
),
Get('query', fun gleam@dynamic:string/1),
Get(fragment, fun gleam@dynamic:string/1)},
{ok, Uri}
end.
-spec parse_query(binary()) -> {ok, list({binary(), binary()})} | {error, nil}.
parse_query(Query) ->
Bool_value = fun(X) ->
gleam@result:map(gleam@dynamic:bool(X), fun(_) -> <<""/utf8>> end)
end,
Query_param = fun(_gleam_capture) ->
gleam@dynamic:typed_tuple2(
_gleam_capture,
fun gleam@dynamic:string/1,
fun(_gleam_capture@1) ->
gleam@dynamic:any(
_gleam_capture@1,
[fun gleam@dynamic:string/1, Bool_value]
)
end
)
end,
gleam@result:nil_error(
gleam@dynamic:typed_list(uri_string:dissect_query(Query), Query_param)
).
-spec query_to_string(list({binary(), binary()})) -> binary().
query_to_string(Query) ->
gleam@result:unwrap(
gleam@dynamic:string(
uri_string:compose_query(Query, [{encoding, utf8}])
),
<<""/utf8>>
).
-spec percent_encode(binary()) -> binary().
percent_encode(Value) ->
gleam@string:replace(
query_to_string([{<<"k"/utf8>>, Value}]),
<<"k="/utf8>>,
<<""/utf8>>
).
-spec percent_decode(binary()) -> {ok, binary()} | {error, nil}.
percent_decode(Value) ->
gleam@result:map(
gleam@result:then(
parse_query(gleam@string:concat([<<"k="/utf8>>, Value])),
fun gleam@list:head/1
),
fun gleam@pair:second/1
).
-spec do_remove_dot_segments(list(binary()), list(binary())) -> list(binary()).
do_remove_dot_segments(Input, Accumulator) ->
case Input of
[] ->
gleam@list:reverse(Accumulator);
[Segment | Rest] ->
Accumulator@5 = case {Segment, Accumulator} of
{<<""/utf8>>, Accumulator@1} ->
Accumulator@1;
{<<"."/utf8>>, Accumulator@2} ->
Accumulator@2;
{<<".."/utf8>>, []} ->
[];
{<<".."/utf8>>, [_ | Accumulator@3]} ->
Accumulator@3;
{Segment@1, Accumulator@4} ->
[Segment@1 | Accumulator@4]
end,
do_remove_dot_segments(Rest, Accumulator@5)
end.
-spec remove_dot_segments(list(binary())) -> list(binary()).
remove_dot_segments(Input) ->
do_remove_dot_segments(Input, []).
-spec path_segments(binary()) -> list(binary()).
path_segments(Path) ->
remove_dot_segments(gleam@string:split(Path, <<"/"/utf8>>)).
-spec to_string(uri()) -> binary().
to_string(Uri) ->
Field = fun(Key, Value) -> case Value of
{some, V} ->
{ok, {Key, gleam@dynamic:from(V)}};
none ->
{error, nil}
end end,
gleam@result:unwrap(
gleam@dynamic:string(
uri_string:recompose(
gleam@map:from_list(
gleam@list:filter_map(
[Field(scheme, erlang:element(2, Uri)),
Field(userinfo, erlang:element(3, Uri)),
Field(host, erlang:element(4, Uri)),
Field(port, erlang:element(5, Uri)),
Field(path, {some, erlang:element(6, Uri)}),
Field('query', erlang:element(7, Uri)),
Field(fragment, erlang:element(8, Uri))],
fun(X) -> X end
)
)
)
),
<<""/utf8>>
).
-spec origin(uri()) -> {ok, binary()} | {error, nil}.
origin(Uri) ->
{uri, Scheme, _, Host, Port, _, _, _} = Uri,
case Scheme of
{some, <<"https"/utf8>>} ->
Origin = {uri, Scheme, none, Host, Port, <<""/utf8>>, none, none},
{ok, to_string(Origin)};
{some, <<"http"/utf8>>} ->
Origin = {uri, Scheme, none, Host, Port, <<""/utf8>>, none, none},
{ok, to_string(Origin)};
_ ->
{error, nil}
end.
-spec drop_last(list(HYD)) -> list(HYD).
drop_last(Elements) ->
gleam@list:take(Elements, gleam@list:length(Elements) - 1).
-spec join_segments(list(binary())) -> binary().
join_segments(Segments) ->
gleam@string:join([<<""/utf8>> | Segments], <<"/"/utf8>>).
-spec merge(uri(), uri()) -> {ok, uri()} | {error, nil}.
merge(Base, Relative) ->
case Base of
{uri, {some, _}, _, {some, _}, _, _, _, _} ->
case Relative of
{uri, _, _, {some, _}, _, _, _, _} ->
Path = join_segments(
remove_dot_segments(
gleam@string:split(
erlang:element(6, Relative),
<<"/"/utf8>>
)
)
),
Resolved = {uri,
gleam@option:'or'(
erlang:element(2, Relative),
erlang:element(2, Base)
),
none,
erlang:element(4, Relative),
erlang:element(5, Relative),
Path,
erlang:element(7, Relative),
erlang:element(8, Relative)},
{ok, Resolved};
{uri, none, _, none, _, _, _, _} ->
{New_path, New_query} = case erlang:element(6, Relative) of
<<""/utf8>> ->
{erlang:element(6, Base),
gleam@option:'or'(
erlang:element(7, Relative),
erlang:element(7, Base)
)};
_ ->
Path_segments = case gleam@string:starts_with(
erlang:element(6, Relative),
<<"/"/utf8>>
) of
true ->
gleam@string:split(
erlang:element(6, Relative),
<<"/"/utf8>>
);
false ->
gleam@list:append(
drop_last(
gleam@string:split(
erlang:element(6, Base),
<<"/"/utf8>>
)
),
gleam@string:split(
erlang:element(6, Relative),
<<"/"/utf8>>
)
)
end,
Path@1 = join_segments(
remove_dot_segments(Path_segments)
),
{Path@1, erlang:element(7, Relative)}
end,
Resolved@1 = {uri,
erlang:element(2, Base),
none,
erlang:element(4, Base),
erlang:element(5, Base),
New_path,
New_query,
erlang:element(8, Relative)},
{ok, Resolved@1}
end;
_ ->
{error, nil}
end.