Atex.Repo.Path (atex v0.9.1)

View Source

A validated AT Protocol repository path - a collection/rkey pair.

Repo paths identify individual records within a repository. They always have exactly two segments separated by a single /:

  • collection - a valid NSID string (e.g. "app.bsky.feed.post")
  • rkey - a record key string (e.g. "3jzfcijpj2z2a", "self", "example.com")

Character constraints

Collection segments follow NSID syntax: alphanumeric characters and periods (A-Za-z0-9.), at least two period-separated components.

Record keys allow: A-Za-z0-9 . - _ : ~ (per spec), with a minimum length of 1 and the values "." and ".." disallowed.

Usage

iex> {:ok, path} = Atex.Repo.Path.new("app.bsky.feed.post", "3jzfcijpj2z2a")
iex> to_string(path)
"app.bsky.feed.post/3jzfcijpj2z2a"

iex> {:ok, path} = Atex.Repo.Path.from_string("app.bsky.actor.profile/self")
iex> path.collection
"app.bsky.actor.profile"
iex> path.rkey
"self"

String.Chars and interpolation

Atex.Repo.Path implements String.Chars, so paths can be used directly in string interpolation and anywhere a string path is expected:

iex> path = Atex.Repo.Path.new!("app.bsky.feed.post", "3jzfcijpj2z2a")
iex> "Record at #{path}"
"Record at app.bsky.feed.post/3jzfcijpj2z2a"

ATProto spec: https://atproto.com/specs/repository#repository-paths

Summary

Types

t()

A validated AT Protocol repository path (collection + rkey).

Functions

Parses a "collection/rkey" string into a validated %Atex.Repo.Path{}.

Parses a "collection/rkey" string into a validated %Atex.Repo.Path{}, raising on invalid input.

Builds a validated %Atex.Repo.Path{} from a collection and record key.

Builds a validated %Atex.Repo.Path{}, raising on invalid input.

Sigil for constructing a validated %Atex.Repo.Path{} from a literal string.

Converts the path to its canonical "collection/rkey" string form.

Types

t()

@type t() :: %Atex.Repo.Path{collection: String.t(), rkey: String.t()}

A validated AT Protocol repository path (collection + rkey).

Functions

from_string(string)

@spec from_string(String.t()) ::
  {:ok, t()} | {:error, :invalid_path | :invalid_collection | :invalid_rkey}

Parses a "collection/rkey" string into a validated %Atex.Repo.Path{}.

Returns {:error, :invalid_path} if the string does not contain exactly one /, or if either segment is invalid.

Examples

iex> Atex.Repo.Path.from_string("app.bsky.feed.post/3jzfcijpj2z2a")
{:ok, %Atex.Repo.Path{collection: "app.bsky.feed.post", rkey: "3jzfcijpj2z2a"}}

iex> Atex.Repo.Path.from_string("no-slash")
{:error, :invalid_path}

iex> Atex.Repo.Path.from_string("a/b/c")
{:error, :invalid_path}

from_string!(string)

@spec from_string!(String.t()) :: t()

Parses a "collection/rkey" string into a validated %Atex.Repo.Path{}, raising on invalid input.

Examples

iex> Atex.Repo.Path.from_string!("app.bsky.feed.post/3jzfcijpj2z2a")
%Atex.Repo.Path{collection: "app.bsky.feed.post", rkey: "3jzfcijpj2z2a"}

new(collection, rkey)

@spec new(String.t(), String.t()) ::
  {:ok, t()} | {:error, :invalid_collection | :invalid_rkey}

Builds a validated %Atex.Repo.Path{} from a collection and record key.

Returns {:error, :invalid_collection} if the collection is not a valid NSID, or {:error, :invalid_rkey} if the record key contains disallowed characters or is a reserved value (. or ..).

Examples

iex> Atex.Repo.Path.new("app.bsky.feed.post", "3jzfcijpj2z2a")
{:ok, %Atex.Repo.Path{collection: "app.bsky.feed.post", rkey: "3jzfcijpj2z2a"}}

iex> Atex.Repo.Path.new("not-an-nsid", "self")
{:error, :invalid_collection}

iex> Atex.Repo.Path.new("app.bsky.feed.post", "..")
{:error, :invalid_rkey}

iex> Atex.Repo.Path.new("app.bsky.feed.post", "bad key!")
{:error, :invalid_rkey}

new!(collection, rkey)

@spec new!(String.t(), String.t()) :: t()

Builds a validated %Atex.Repo.Path{}, raising on invalid input.

Examples

iex> Atex.Repo.Path.new!("app.bsky.feed.post", "3jzfcijpj2z2a")
%Atex.Repo.Path{collection: "app.bsky.feed.post", rkey: "3jzfcijpj2z2a"}

sigil_PATH(string, _)

@spec sigil_PATH(String.t(), list()) :: t()

Sigil for constructing a validated %Atex.Repo.Path{} from a literal string.

Raises ArgumentError if the string is not a valid "collection/rkey" path. To use this sigil, import Atex.Repo.Path.

Examples

iex> import Atex.Repo.Path
iex> ~PATH"app.bsky.feed.post/3jzfcijpj2z2a"
%Atex.Repo.Path{collection: "app.bsky.feed.post", rkey: "3jzfcijpj2z2a"}

to_string(path)

@spec to_string(t()) :: String.t()

Converts the path to its canonical "collection/rkey" string form.

Examples

iex> path = Atex.Repo.Path.new!("app.bsky.feed.post", "3jzfcijpj2z2a")
iex> Atex.Repo.Path.to_string(path)
"app.bsky.feed.post/3jzfcijpj2z2a"