defmodule URL.UUID do @moduledoc """ Parses a `geo` URL """ import NimbleParsec import URL.ParseHelpers.{Core, Params, Unwrap} @type t() :: %__MODULE__{ uuid: binary(), params: Map.t() } defstruct uuid: nil, params: %{} @doc """ Parse a URI with the `:scheme` of "uuid" This parser will parse the [RFC 4122](https://tools.ietf.org/html/rfc4122) standard and the [non-standard](https://tools.ietf.org/html/draft-kindel-uuid-uri-00) UUID uri. ## Example iex> uuid = URI.parse("uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6;a=b") iex> URL.UUID.parse(uuid) %URL.UUID{params: %{"a" => "b"}, uuid: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"} iex> uuid = URI.parse("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6") iex> URL.UUID.parse(uuid) %URL.UUID{params: %{}, uuid: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"} """ @spec parse(URI.t()) :: __MODULE__.t() | {:error, {module(), binary()}} def parse(%URI{scheme: "uuid", path: path}) do with {:ok, uuid} <- unwrap(parse_uuid(path)) do uuid |> structify(__MODULE__) end end def parse(%URI{scheme: "urn", path: path}) do case URL.parse(path) do %URL{parsed_path: parsed_path} -> parsed_path other -> other end end defparsecp :parse_uuid, uuid() |> concat(params()) end