Current section

Files

Jump to
parrot src parrot@internal@lib.erl
Raw

src/parrot@internal@lib.erl

-module(parrot@internal@lib).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([try_nil/2, dedent/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/parrot/internal/lib.gleam", 9).
?DOC(false).
-spec try_nil(
{ok, IVO} | {error, any()},
fun((IVO) -> {ok, IVS} | {error, nil})
) -> {ok, IVS} | {error, nil}.
try_nil(Result, Do) ->
gleam@result:'try'(gleam@result:replace_error(Result, nil), Do).
-file("src/parrot/internal/lib.gleam", 36).
?DOC(false).
-spec indent_size(binary(), integer()) -> integer().
indent_size(Text, Size) ->
case Text of
<<" "/utf8, Rest/binary>> ->
indent_size(Rest, Size + 1);
<<"\t"/utf8, Rest/binary>> ->
indent_size(Rest, Size + 1);
_ ->
Size
end.
-file("src/parrot/internal/lib.gleam", 43).
?DOC(false).
-spec is_all_whitespace(binary()) -> boolean().
is_all_whitespace(Text) ->
_pipe = Text,
_pipe@1 = gleam@string:trim(_pipe),
gleam@string:is_empty(_pipe@1).
-file("src/parrot/internal/lib.gleam", 17).
?DOC(false).
-spec dedent(binary()) -> binary().
dedent(Text) ->
Lines = begin
_pipe = Text,
gleam@string:split(_pipe, <<"\n"/utf8>>)
end,
Min_indent = begin
_pipe@1 = Lines,
_pipe@2 = gleam@list:filter(
_pipe@1,
fun(Line) -> not is_all_whitespace(Line) end
),
_pipe@3 = gleam@list:map(
_pipe@2,
fun(_capture) -> indent_size(_capture, 0) end
),
_pipe@4 = gleam@list:sort(_pipe@3, fun gleam@int:compare/2),
_pipe@5 = gleam@list:first(_pipe@4),
gleam@result:unwrap(_pipe@5, 0)
end,
_pipe@6 = Lines,
_pipe@7 = gleam@list:map(
_pipe@6,
fun(_capture@1) -> gleam@string:drop_start(_capture@1, Min_indent) end
),
_pipe@8 = gleam@string:join(_pipe@7, <<"\n"/utf8>>),
gleam@string:trim(_pipe@8).