Anubis.Server.Component.URITemplate (anubis_mcp v1.6.1)

Copy Markdown

RFC 6570 URI Template parser and matcher (Levels 1 and 2).

Supported expressions:

FormLevelDescription
{var}1Simple expansion (excludes /?#)
{+var}2Reserved expansion (allows reserved)
{#var}2Fragment expansion (literal #)

Level 3 (multi-var, label, path-segment, query) and Level 4 (prefix, explode) are not supported.

Examples

iex> {:ok, t} = URITemplate.parse("file:///{path}")
iex> URITemplate.match(t, "file:///docs/readme.md")
{:ok, %{"path" => "docs/readme.md"}}

iex> {:ok, t} = URITemplate.parse("db:///{table}/{id}")
iex> URITemplate.match(t, "db:///users/42")
{:ok, %{"table" => "users", "id" => "42"}}

iex> {:ok, t} = URITemplate.parse("file:///{+path}")
iex> URITemplate.match(t, "file:///deep/nested/file.md")
{:ok, %{"path" => "deep/nested/file.md"}}

iex> {:ok, t} = URITemplate.parse("/page{#section}")
iex> URITemplate.match(t, "/page#intro")
{:ok, %{"section" => "intro"}}

Summary

Functions

Matches a URI against a parsed template (or template string).

Parses an RFC 6570 (Level 1 + Level 2) URI template string.

Same as parse/1 but raises ArgumentError on failure.

Types

t()

@type t() :: %Anubis.Server.Component.URITemplate{
  raw: String.t(),
  regex: Regex.t(),
  vars: [String.t()]
}

Functions

match(t, uri)

@spec match(t() | String.t(), String.t()) :: {:ok, map()} | :error

Matches a URI against a parsed template (or template string).

Returns {:ok, vars_map} on a match where keys are variable names and values are the percent-decoded substrings, or :error if the URI does not match the template.

parse(template)

@spec parse(String.t()) :: {:ok, t()} | {:error, String.t()}

Parses an RFC 6570 (Level 1 + Level 2) URI template string.

Returns {:ok, %URITemplate{}} on success, {:error, reason} otherwise.

parse!(template)

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

Same as parse/1 but raises ArgumentError on failure.