Current section
Files
Jump to
Current section
Files
src/acumen@internal@utils.erl
-module(acumen@internal@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/acumen/internal/utils.gleam").
-export([gose_error_to_string/1, json_parse_error_message/2, parse_absolute_uri/1, parse_http_date/1, request_from_url/1, timestamp_decoder/0, unexpected_status_message/1, uri_decoder/0]).
-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/acumen/internal/utils.gleam", 17).
?DOC(false).
-spec gose_error_to_string(gose:gose_error()) -> binary().
gose_error_to_string(Err) ->
case Err of
{parse_error, Msg} ->
<<"parse error: "/utf8, Msg/binary>>;
{crypto_error, Msg@1} ->
<<"crypto error: "/utf8, Msg@1/binary>>;
{invalid_state, Msg@2} ->
<<"invalid state: "/utf8, Msg@2/binary>>;
verification_failed ->
<<"verification failed"/utf8>>
end.
-file("src/acumen/internal/utils.gleam", 26).
?DOC(false).
-spec json_parse_error_message(binary(), gleam@json:decode_error()) -> binary().
json_parse_error_message(Entity, Error) ->
Detail = case Error of
unexpected_end_of_input ->
<<"unexpected end of input"/utf8>>;
{unexpected_byte, B} ->
<<"unexpected byte: "/utf8, B/binary>>;
{unexpected_sequence, S} ->
<<"unexpected sequence: "/utf8, S/binary>>;
{unable_to_decode, [{decode_error, Expected, Found, _} | _]} ->
<<<<<<"unable to decode: expected "/utf8, Expected/binary>>/binary,
", found "/utf8>>/binary,
Found/binary>>;
{unable_to_decode, _} ->
<<"unable to decode"/utf8>>
end,
<<<<<<"failed to parse "/utf8, Entity/binary>>/binary, ": "/utf8>>/binary,
Detail/binary>>.
-file("src/acumen/internal/utils.gleam", 41).
?DOC(false).
-spec parse_absolute_uri(binary()) -> {ok, gleam@uri:uri()} | {error, nil}.
parse_absolute_uri(String) ->
case gleam_stdlib:uri_parse(String) of
{ok, {uri, {some, _}, _, _, _, _, _, _} = Parsed} ->
{ok, Parsed};
_ ->
{error, nil}
end.
-file("src/acumen/internal/utils.gleam", 69).
?DOC(false).
-spec parse_time(binary()) -> {ok, {integer(), integer(), integer()}} |
{error, nil}.
parse_time(Time) ->
case gleam@string:split(Time, <<":"/utf8>>) of
[Hours_str, Minutes_str, Seconds_str] ->
gleam@result:'try'(
gleam_stdlib:parse_int(Hours_str),
fun(Hours) ->
gleam@result:'try'(
gleam_stdlib:parse_int(Minutes_str),
fun(Minutes) ->
gleam@result:'try'(
gleam_stdlib:parse_int(Seconds_str),
fun(Seconds) ->
{ok, {Hours, Minutes, Seconds}}
end
)
end
)
end
);
_ ->
{error, nil}
end.
-file("src/acumen/internal/utils.gleam", 81).
?DOC(false).
-spec parse_month(binary()) -> {ok, gleam@time@calendar:month()} | {error, nil}.
parse_month(Month) ->
case Month of
<<"Jan"/utf8>> ->
{ok, january};
<<"Feb"/utf8>> ->
{ok, february};
<<"Mar"/utf8>> ->
{ok, march};
<<"Apr"/utf8>> ->
{ok, april};
<<"May"/utf8>> ->
{ok, may};
<<"Jun"/utf8>> ->
{ok, june};
<<"Jul"/utf8>> ->
{ok, july};
<<"Aug"/utf8>> ->
{ok, august};
<<"Sep"/utf8>> ->
{ok, september};
<<"Oct"/utf8>> ->
{ok, october};
<<"Nov"/utf8>> ->
{ok, november};
<<"Dec"/utf8>> ->
{ok, december};
_ ->
{error, nil}
end.
-file("src/acumen/internal/utils.gleam", 48).
?DOC(false).
-spec parse_http_date(binary()) -> {ok, gleam@time@timestamp:timestamp()} |
{error, nil}.
parse_http_date(Value) ->
case gleam@string:split(Value, <<" "/utf8>>) of
[_, Day_str, Month_str, Year_str, Time_str | _] ->
gleam@result:'try'(
gleam_stdlib:parse_int(Day_str),
fun(Day) ->
gleam@bool:guard(
(Day < 1) orelse (Day > 31),
{error, nil},
fun() ->
gleam@result:'try'(
parse_month(Month_str),
fun(Month) ->
gleam@result:'try'(
gleam_stdlib:parse_int(Year_str),
fun(Year) ->
gleam@result:'try'(
parse_time(Time_str),
fun(_use0) ->
{Hours, Minutes, Seconds} = _use0,
gleam@bool:guard(
(Hours < 0) orelse (Hours
> 23),
{error, nil},
fun() ->
gleam@bool:guard(
(Minutes < 0)
orelse (Minutes
> 59),
{error, nil},
fun() ->
gleam@bool:guard(
(Seconds
< 0)
orelse (Seconds
> 59),
{error,
nil},
fun() ->
{ok,
gleam@time@timestamp:from_calendar(
{date,
Year,
Month,
Day},
{time_of_day,
Hours,
Minutes,
Seconds,
0},
gleam@time@duration:seconds(
0
)
)}
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
);
_ ->
{error, nil}
end.
-file("src/acumen/internal/utils.gleam", 99).
?DOC(false).
-spec request_from_url(acumen@url:url()) -> gleam@http@request:request(binary()).
request_from_url(Url) ->
Req@1 = case begin
_pipe = Url,
_pipe@1 = acumen@url:to_uri(_pipe),
gleam@http@request:from_uri(_pipe@1)
end of
{ok, Req} -> Req;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"acumen/internal/utils"/utf8>>,
function => <<"request_from_url"/utf8>>,
line => 100,
value => _assert_fail,
start => 3226,
'end' => 3296,
pattern_start => 3237,
pattern_end => 3244})
end,
_pipe@2 = Req@1,
_pipe@3 = gleam@http@request:set_header(
_pipe@2,
<<"user-agent"/utf8>>,
<<"acumen/"/utf8, (<<"1.1.1"/utf8>>)/binary>>
),
gleam@http@request:set_header(
_pipe@3,
<<"accept-language"/utf8>>,
<<"en"/utf8>>
).
-file("src/acumen/internal/utils.gleam", 110).
?DOC(false).
-spec timestamp_decoder() -> gleam@dynamic@decode:decoder(gleam@time@timestamp:timestamp()).
timestamp_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(String) -> case gleam@time@timestamp:parse_rfc3339(String) of
{ok, Ts} ->
gleam@dynamic@decode:success(Ts);
{error, _} ->
gleam@dynamic@decode:failure(
gleam@time@timestamp:from_unix_seconds(0),
<<"Timestamp"/utf8>>
)
end end
).
-file("src/acumen/internal/utils.gleam", 118).
?DOC(false).
-spec unexpected_status_message(integer()) -> binary().
unexpected_status_message(Status) ->
<<"unexpected status: "/utf8, (erlang:integer_to_binary(Status))/binary>>.
-file("src/acumen/internal/utils.gleam", 122).
?DOC(false).
-spec uri_decoder() -> gleam@dynamic@decode:decoder(gleam@uri:uri()).
uri_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(String) -> case parse_absolute_uri(String) of
{ok, Parsed} ->
gleam@dynamic@decode:success(Parsed);
{error, _} ->
gleam@dynamic@decode:failure(
{uri, none, none, none, none, <<""/utf8>>, none, none},
<<"URI"/utf8>>
)
end end
).