-module(gleam@path). -compile(no_auto_import). -export([from_string/1, join/2, to_string/1, normalize/1]). -export_type([path/0, path_filter/0]). -type path() :: {absolute, list(binary())} | {relative, list(binary())}. -type path_filter() :: up | skip | include. -spec filter_segment_list(path()) -> path(). filter_segment_list(Path) -> Constructor = case Path of {absolute, _@1} -> fun(A) -> {absolute, A} end; {relative, _@2} -> fun(A) -> {relative, A} end end, Segments = erlang:element(2, Path), _pipe = Segments, _pipe@1 = gleam@list:fold( _pipe, [], fun(Segments@1, Segment) -> case filter_segment(Segment) of up -> case {Path, gleam@list:all( Segments@1, fun(Segment@1) -> Segment@1 =:= <<".."/utf8>> end )} of {{relative, _@3}, true} -> gleam@list:append(Segments@1, [<<".."/utf8>>]); {_@4, _@5} -> gleam@list:take( Segments@1, gleam@list:length(Segments@1) - 1 ) end; skip -> Segments@1; include -> gleam@list:append(Segments@1, [Segment]) end end ), Constructor(_pipe@1). -spec filter_segment(binary()) -> path_filter(). filter_segment(Segment) -> case Segment of <<""/utf8>> -> skip; <<"."/utf8>> -> skip; <<".."/utf8>> -> up; _@1 -> include end. -spec from_string(binary()) -> path(). from_string(Base) -> Constructor = case gleam@string:starts_with(Base, <<"/"/utf8>>) of true -> fun(A) -> {absolute, A} end; false -> fun(A) -> {relative, A} end end, _pipe = Base, _pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>), _pipe@2 = Constructor(_pipe@1), filter_segment_list(_pipe@2). -spec join(path(), binary()) -> path(). join(Path, Piece) -> Constructor = case Path of {absolute, _@1} -> fun(A) -> {absolute, A} end; {relative, _@2} -> fun(A) -> {relative, A} end end, Segments = erlang:element(2, Path), _pipe = Piece, _pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>), _pipe@2 = gleam@list:append(Segments, _pipe@1), _pipe@3 = Constructor(_pipe@2), filter_segment_list(_pipe@3). -spec to_string(path()) -> binary(). to_string(Path) -> Joined_segments = gleam@string:join(erlang:element(2, Path), <<"/"/utf8>>), case Path of {absolute, _@1} -> gleam@string:concat([<<"/"/utf8>>, Joined_segments]); {relative, _@2} -> Joined_segments end. -spec normalize(binary()) -> binary(). normalize(Messy) -> _pipe = Messy, _pipe@1 = from_string(_pipe), to_string(_pipe@1).