Current section
Files
Jump to
Current section
Files
src/sprocket@internal@utils@cuid.erl
-module(sprocket@internal@utils@cuid).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/sprocket/internal/utils/cuid.gleam").
-export([generate/1, is_cuid/1, slug/1, is_slug/1, start/0]).
-export_type([message/0, state/0, char_list/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).
-opaque message() :: {generate, gleam@erlang@process:subject(binary())} |
{generate_slug, gleam@erlang@process:subject(binary())}.
-opaque state() :: {state, integer(), binary()}.
-type char_list() :: any().
-file("src/sprocket/internal/utils/cuid.gleam", 56).
?DOC(false).
-spec generate(gleam@erlang@process:subject(message())) -> binary().
generate(Channel) ->
gleam@otp@actor:call(Channel, 1000, fun(Field@0) -> {generate, Field@0} end).
-file("src/sprocket/internal/utils/cuid.gleam", 61).
?DOC(false).
-spec is_cuid(binary()) -> boolean().
is_cuid(Id) ->
gleam_stdlib:string_starts_with(Id, <<"c"/utf8>>).
-file("src/sprocket/internal/utils/cuid.gleam", 66).
?DOC(false).
-spec slug(gleam@erlang@process:subject(message())) -> binary().
slug(Channel) ->
gleam@otp@actor:call(
Channel,
1000,
fun(Field@0) -> {generate_slug, Field@0} end
).
-file("src/sprocket/internal/utils/cuid.gleam", 71).
?DOC(false).
-spec is_slug(binary()) -> boolean().
is_slug(Slug) ->
Slug_length = string:length(Slug),
(Slug_length >= 7) andalso (Slug_length =< 10).
-file("src/sprocket/internal/utils/cuid.gleam", 118).
?DOC(false).
-spec format_id(list(binary())) -> binary().
format_id(Id_data) ->
_pipe = Id_data,
_pipe@1 = erlang:list_to_binary(_pipe),
string:lowercase(_pipe@1).
-file("src/sprocket/internal/utils/cuid.gleam", 131).
?DOC(false).
-spec timestamp() -> binary().
timestamp() ->
Secs = os:system_time(millisecond),
_pipe = Secs,
gleam@int:to_base36(_pipe).
-file("src/sprocket/internal/utils/cuid.gleam", 155).
?DOC(false).
-spec get_fingerprint() -> binary().
get_fingerprint() ->
Operator = 36 * 36,
Pid@1 = case begin
_pipe = os:getpid(),
_pipe@1 = erlang:list_to_binary(_pipe),
gleam_stdlib:parse_int(_pipe@1)
end of
{ok, Pid} -> Pid;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"sprocket/internal/utils/cuid"/utf8>>,
function => <<"get_fingerprint"/utf8>>,
line => 157,
value => _assert_fail,
start => 3891,
'end' => 3975,
pattern_start => 3902,
pattern_end => 3909})
end,
Id = (case Operator of
0 -> 0;
Gleam@denominator -> Pid@1 rem Gleam@denominator
end) * Operator,
Localhost = net_adm:localhost(),
Sum = begin
_pipe@2 = Localhost,
gleam@list:fold(_pipe@2, 0, fun(Char, Acc) -> Char + Acc end)
end,
Hostid = case Operator of
0 -> 0;
Gleam@denominator@1 -> ((Sum + erlang:length(Localhost)) + 36) rem Gleam@denominator@1
end,
_pipe@3 = Id + Hostid,
gleam@int:to_base36(_pipe@3).
-file("src/sprocket/internal/utils/cuid.gleam", 138).
?DOC(false).
-spec format_count(integer()) -> binary().
format_count(Num) ->
_pipe = Num,
_pipe@1 = gleam@int:to_base36(_pipe),
gleam@string:pad_start(_pipe@1, 4, <<"0"/utf8>>).
-file("src/sprocket/internal/utils/cuid.gleam", 124).
?DOC(false).
-spec new_count(integer()) -> integer().
new_count(Count) ->
case Count < 1679616 of
true ->
Count + 1;
false ->
0
end.
-file("src/sprocket/internal/utils/cuid.gleam", 178).
?DOC(false).
-spec random_block() -> binary().
random_block() ->
_pipe = rand:uniform(1679616 - 1),
_pipe@1 = gleam@int:to_base36(_pipe),
gleam@string:pad_start(_pipe@1, 4, <<"0"/utf8>>).
-file("src/sprocket/internal/utils/cuid.gleam", 79).
?DOC(false).
-spec handle_msg(state(), message()) -> gleam@otp@actor:next(state(), message()).
handle_msg(State, Msg) ->
case Msg of
{generate, Reply_with} ->
Id = format_id(
[<<"c"/utf8>>,
timestamp(),
format_count(erlang:element(2, State)),
erlang:element(3, State),
random_block(),
random_block()]
),
gleam@otp@actor:send(Reply_with, Id),
gleam@otp@actor:continue(
begin
_record = State,
{state,
new_count(erlang:element(2, State)),
erlang:element(3, _record)}
end
);
{generate_slug, Reply_with@1} ->
Slug = format_id(
[begin
_pipe = timestamp(),
gleam@string:slice(_pipe, -2, 2)
end,
begin
_pipe@1 = format_count(erlang:element(2, State)),
gleam@string:slice(_pipe@1, -4, 4)
end,
erlang:list_to_binary(
[gleam@string:slice(erlang:element(3, State), 0, 1),
gleam@string:slice(erlang:element(3, State), -1, 1)]
),
begin
_pipe@2 = random_block(),
gleam@string:slice(_pipe@2, -2, 2)
end]
),
gleam@otp@actor:send(Reply_with@1, Slug),
gleam@otp@actor:continue(
begin
_record@1 = State,
{state,
new_count(erlang:element(2, State)),
erlang:element(3, _record@1)}
end
)
end.
-file("src/sprocket/internal/utils/cuid.gleam", 37).
?DOC(false).
-spec start() -> {ok, gleam@erlang@process:subject(message())} |
{error, gleam@otp@actor:start_error()}.
start() ->
_pipe = gleam@otp@actor:new({state, 0, get_fingerprint()}),
_pipe@1 = gleam@otp@actor:on_message(_pipe, fun handle_msg/2),
_pipe@2 = gleam@otp@actor:start(_pipe@1),
gleam@result:map(_pipe@2, fun(Started) -> erlang:element(3, Started) end).