Current section
Files
Jump to
Current section
Files
src/possum@at_uri.erl
-module(possum@at_uri).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/possum/at_uri.gleam").
-export([authority/1, collection/1, rkey/1, parse/1, to_string/1]).
-export_type([parse_error/0, authority/0, at_uri/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(
" The AT URI scheme (at://) makes it easy to reference individual records in\n"
" a specific repository, identified by either DID or handle.\n"
"\n"
" AT URIs are not content-addressed, so the contents of the record they\n"
" refer to may also change over time.\n"
"\n"
" Documentation: [AT URI Scheme](https://atproto.com/specs/at-uri-scheme)\n"
).
-type parse_error() :: {invalid_scheme, binary()} |
missing_scheme |
{invalid_handle_authority, possum@handle:parse_error()} |
{invalid_did_authority, possum@did:parse_error()} |
missing_authority.
-type authority() :: {handle, possum@handle:handle()} | {did, possum@did:did()}.
-opaque at_uri() :: {at_uri, authority(), binary(), binary()}.
-file("src/possum/at_uri.gleam", 66).
?DOC(
" Return the authority part of the Uri\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/at_uri\n"
"\n"
" let string = \"at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g\"\n"
" let assert Ok(at_uri) = at_uri.parse(string)\n"
"\n"
" assert at_uri.authority(at_uri) == \"did:plc:vwzwgnygau7ed7b7wt5ux7y2\"\n"
" ```\n"
).
-spec authority(at_uri()) -> authority().
authority(Self) ->
erlang:element(2, Self).
-file("src/possum/at_uri.gleam", 82).
?DOC(
" Return the collecion part of the Uri\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/at_uri\n"
"\n"
" let string = \"at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g\"\n"
" let assert Ok(at_uri) = at_uri.parse(string)\n"
"\n"
" assert at_uri.collection(at_uri) == \"app.bsky.feed.post\"\n"
" ```\n"
).
-spec collection(at_uri()) -> binary().
collection(Self) ->
erlang:element(3, Self).
-file("src/possum/at_uri.gleam", 98).
?DOC(
" Return the rkey part of the Uri\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/at_uri\n"
"\n"
" let string = \"at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g\"\n"
" let assert Ok(at_uri) = at_uri.parse(string)\n"
"\n"
" assert at_uri.rkey(at_uri) == \"3k5nobkf2w72g\"\n"
" ```\n"
).
-spec rkey(at_uri()) -> binary().
rkey(Self) ->
erlang:element(4, Self).
-file("src/possum/at_uri.gleam", 157).
-spec parse_authority(binary()) -> {ok, authority()} | {error, parse_error()}.
parse_authority(String) ->
case String of
<<"did:"/utf8, _/binary>> ->
case possum@did:parse(String) of
{ok, Value} ->
{ok, {did, Value}};
{error, Reason} ->
{error, {invalid_did_authority, Reason}}
end;
String@1 ->
case possum@handle:parse(String@1) of
{ok, Value@1} ->
{ok, {handle, Value@1}};
{error, Reason@1} ->
{error, {invalid_handle_authority, Reason@1}}
end
end.
-file("src/possum/at_uri.gleam", 115).
?DOC(
" Parse a String into a valid At-Uri type\n"
" If the value is not a valid string then an error is returned.\n"
"\n"
" Handles are not supported, use a DID instead.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/at_uri\n"
"\n"
" let string = \"at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g\"\n"
" let assert Ok(at_uri) = at_uri.parse(string)\n"
" ```\n"
).
-spec parse(binary()) -> {ok, at_uri()} | {error, parse_error()}.
parse(Content) ->
gleam@result:'try'(case Content of
<<"at://"/utf8, Rest/binary>> ->
{ok, Rest};
_ ->
case gleam@string:split_once(Content, <<"://"/utf8>>) of
{ok, Scheme} ->
{error, {invalid_scheme, erlang:element(1, Scheme)}};
{error, _} ->
{error, missing_scheme}
end
end, fun(Value) -> case gleam@string:split(Value, <<"/"/utf8>>) of
[] ->
{error, missing_authority};
[Authority] ->
gleam@result:map(
parse_authority(Authority),
fun(Authority@1) ->
{at_uri, Authority@1, <<""/utf8>>, <<""/utf8>>}
end
);
[Authority@2, Collection] ->
gleam@result:map(
parse_authority(Authority@2),
fun(Authority@3) ->
{at_uri, Authority@3, Collection, <<""/utf8>>}
end
);
[Authority@4, Collection@1, Rkey | _] ->
gleam@result:map(
parse_authority(Authority@4),
fun(Authority@5) ->
{at_uri, Authority@5, Collection@1, Rkey}
end
)
end end).
-file("src/possum/at_uri.gleam", 186).
?DOC(
" Convert a At-URI into a String\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/at_uri\n"
"\n"
" let string =\n"
" \"at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g\"\n"
"\n"
" let assert Ok(at_uri) = at_uri.parse(string)\n"
" assert at_uri.to_string(at_uri) == string\n"
" ```\n"
).
-spec to_string(at_uri()) -> binary().
to_string(Self) ->
Host = case erlang:element(2, Self) of
{handle, Value} ->
possum@handle:to_string(Value);
{did, Value@1} ->
possum@did:to_string(Value@1)
end,
Path = case {erlang:element(3, Self), erlang:element(4, Self)} of
{<<""/utf8>>, _} ->
<<""/utf8>>;
{Collection, <<""/utf8>>} ->
<<"/"/utf8, Collection/binary>>;
{Collection@1, Rkey} ->
<<<<<<"/"/utf8, Collection@1/binary>>/binary, "/"/utf8>>/binary,
Rkey/binary>>
end,
<<<<"at://"/utf8, Host/binary>>/binary, Path/binary>>.