Current section

Files

Jump to
possum src possum@nsid.erl
Raw

src/possum@nsid.erl

-module(possum@nsid).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/possum/nsid.gleam").
-export([to_string/1, parse/1]).
-export_type([parse_error/0, nsid/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(
" Namespaced Identifiers (NSIDs) are used to reference Lexicon schemas for\n"
" records, XRPC endpoints, and more. For example, `com.atproto.sync.getRecord`.\n"
"\n"
" The basic structure and semantics of an NSID are a fully-qualified hostname\n"
" in reverse domain-name order, followed by an additional name segment.\n"
" The hostname part is the **domain authority**, and the final segment is the **name**.\n"
"\n"
" Documentation: [Namespaced Identifiers](https://atproto.com/specs/nsid)\n"
).
-type parse_error() :: {invalid_regex_pattern, binary()} |
{invalid_syntax, binary()}.
-opaque nsid() :: {nsid, binary()}.
-file("src/possum/nsid.gleam", 40).
?DOC(
" Convert a NSID into a String\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/nsid\n"
"\n"
" let string = \"com.atproto.sync.getRecord\"\n"
"\n"
" let assert Ok(nsid) = nsid.parse(string)\n"
" assert nsid.to_string(nsid) == string\n"
" ```\n"
).
-spec to_string(nsid()) -> binary().
to_string(Nsid) ->
erlang:element(2, Nsid).
-file("src/possum/nsid.gleam", 55).
?DOC(
" Parse a string into a valid `Nsid` type\n"
" If the value is not a valid string then an error is returned.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import possum/nsid\n"
"\n"
" let string = \"com.atproto.sync.getRecord.\"\n"
" let assert Ok(nsid) = nsid.parse(string)\n"
" ```\n"
).
-spec parse(binary()) -> {ok, nsid()} | {error, parse_error()}.
parse(Content) ->
gleam@result:'try'(
begin
_pipe = gleam@regexp:from_string(
<<"^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$"/utf8>>
),
gleam@result:replace_error(
_pipe,
{invalid_regex_pattern,
<<"^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$"/utf8>>}
)
end,
fun(With) -> gleam@result:map(case gleam@regexp:check(With, Content) of
true ->
{ok, Content};
false ->
{error, {invalid_syntax, Content}}
end, fun(Value) -> {nsid, Value} end) end
).