Current section
Files
Jump to
Current section
Files
src/sprocket@internal@utils@unique.erl
-module(sprocket@internal@utils@unique).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([uuid/0, cuid/1, slug/2, from_string/1, to_string/1, equals/2]).
-export_type([unique/0]).
-opaque unique() :: {unique, binary()}.
-spec uuid() -> unique().
uuid() ->
_assert_subject = begin
_pipe = ids@uuid:generate_v4(),
gleam@result:map_error(
_pipe,
fun(Error) ->
sprocket@internal@logger:error(
<<"unique.uuid: failed to generate UUID"/utf8>>
),
Error
end
)
end,
{ok, Id} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/internal/utils/unique"/utf8>>,
function => <<"uuid"/utf8>>,
line => 12})
end,
{unique, Id}.
-spec cuid(gleam@erlang@process:subject(ids@cuid:message())) -> unique().
cuid(Channel) ->
Id = ids@cuid:generate(Channel),
{unique, Id}.
-spec slug(gleam@erlang@process:subject(ids@cuid:message()), binary()) -> unique().
slug(Channel, Label) ->
Id = <<<<Label/binary, "-"/utf8>>/binary, (ids@cuid:slug(Channel))/binary>>,
{unique, Id}.
-spec from_string(binary()) -> unique().
from_string(Str) ->
{unique, Str}.
-spec to_string(unique()) -> binary().
to_string(Unique) ->
erlang:element(2, Unique).
-spec equals(unique(), unique()) -> boolean().
equals(A, B) ->
erlang:element(2, A) =:= erlang:element(2, B).