Current section
Files
Jump to
Current section
Files
src/possum@did.erl
-module(possum@did).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/possum/did.gleam").
-export([method/1, indentifer/1, to_string/1, parse/1, decoder/0]).
-export_type([parse_error/0, did/0, method/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(
" ## DID (Decentralized ID)\n"
"\n"
" DIDs, or Decentralized IDentifiers, are universally-unique identifiers which\n"
" represent data repos. They are permanent and non-human-readable.\n"
" DIDs are a W3C specification. The AT Protocol currently supports\n"
" did:web and did:plc, two different DID methods.\n"
"\n"
" DIDs resolve to documents which contain metadata about a repo,\n"
" including the address of the repo's PDS, the repo's handles,\n"
" and the public signing keys.\n"
).
-type parse_error() :: {invalid_regex_pattern, binary()} |
{invalid_syntax, binary()} |
{invalid_did_method, binary()}.
-opaque did() :: {did, method(), binary()}.
-type method() :: plc | web.
-file("src/possum/did.gleam", 47).
?DOC(
" Return the DID method\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let assert Ok(did) = did.parse(\"did:plc:k5vecqzf4d5mdvkcu3mx6l5g\")\n"
" let method = possum.method(did)\n"
"\n"
" assert method == did.Plc\n"
" ```\n"
).
-spec method(did()) -> method().
method(Self) ->
erlang:element(2, Self).
-file("src/possum/did.gleam", 66).
?DOC(
" Return the DID identifier.\n"
"\n"
" This function **does not** convert the value into a valid DID format,\n"
" for that use `to_string`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/did\n"
"\n"
" let assert Ok(did) = did.parse(\"did:plc:k5vecqzf4d5mdvkcu3mx6l5g\")\n"
" let identifier = did.identifier(did)\n"
"\n"
" assert identifier == \"k5vecqzf4d5mdvkcu3mx6l5g\"\n"
" ```\n"
).
-spec indentifer(did()) -> binary().
indentifer(Self) ->
erlang:element(3, Self).
-file("src/possum/did.gleam", 82).
?DOC(
" Convert a DID to a string.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/did\n"
"\n"
" let assert Ok(did) = did.parse(\"did:plc:k5vecqzf4d5mdvkcu3mx6l5g\")\n"
" let string = did.to_string(did)\n"
"\n"
" assert string == \"did:plc:k5vecqzf4d5mdvkcu3mx6l5g\"\n"
" ```\n"
).
-spec to_string(did()) -> binary().
to_string(Self) ->
case erlang:element(2, Self) of
plc ->
<<"did:plc:"/utf8, (erlang:element(3, Self))/binary>>;
web ->
<<"did:web:"/utf8, (erlang:element(3, Self))/binary>>
end.
-file("src/possum/did.gleam", 131).
?DOC(
" Parse a string into a valid `Did` type\n"
" If the value is not a valid string then an error is returned.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/did\n"
"\n"
" let string = \"did:plc:k5vecqzf4d5mdvkcu3mx6l5g\"\n"
" let assert Ok(did) = did.parse(string)\n"
" ```\n"
).
-spec parse(binary()) -> {ok, did()} | {error, parse_error()}.
parse(Content) ->
gleam@result:'try'(
begin
_pipe = gleam@regexp:from_string(
<<"^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$"/utf8>>
),
gleam@result:replace_error(
_pipe,
{invalid_regex_pattern,
<<"^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$"/utf8>>}
)
end,
fun(With) ->
gleam@result:'try'(case gleam@regexp:check(With, Content) of
true ->
{ok, Content};
false ->
{error, {invalid_syntax, Content}}
end, fun(Value) -> case gleam@string:trim(Value) of
<<"did:plc:"/utf8, Identifier/binary>> ->
{ok, {did, plc, Identifier}};
<<"did:web:"/utf8, Identifier@1/binary>> ->
{ok, {did, web, Identifier@1}};
Value@1 ->
{error, {invalid_did_method, Value@1}}
end end)
end
).
-file("src/possum/did.gleam", 89).
-spec decoder() -> gleam@dynamic@decode:decoder(did()).
decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) -> case parse(Value) of
{ok, Did} ->
gleam@dynamic@decode:success(Did);
{error, _} ->
gleam@dynamic@decode:failure(
{did, plc, <<"possum"/utf8>>},
<<"DID"/utf8>>
)
end end
).