From 70123112675a57eb6777a286e469b903cd47653f Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Thu, 10 Oct 2024 22:32:17 -0400 Subject: [PATCH] feat: atom schematic --- README.md | 1 + lib/schematic.ex | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index d0689fe..a5b4fb0 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ There are 12 builtin schematics that you can use to build new schematics that fi - `bool/0` - `str/0` +- `atom/0` - `int/0` - `float/0` - `list/1` diff --git a/lib/schematic.ex b/lib/schematic.ex index 4f91d42..bf2077c 100644 --- a/lib/schematic.ex +++ b/lib/schematic.ex @@ -168,6 +168,37 @@ defmodule Schematic do } end + @doc """ + Specifies that the data is an atom. + + ## Usage + + Any string. + + ```elixir + iex> schematic = atom() + iex> {:ok, :hi} = unify(schematic, :hi) + iex> {:error, "expected an atom"} = unify(schematic, "boom") + ``` + """ + @spec atom() :: t() + def atom() do + message = fn -> "an atom" end + + %Schematic{ + kind: "atom", + message: message, + unify: + telemetry_wrap(:str, %{}, fn input, _dir -> + if is_atom(input) do + {:ok, input} + else + {:error, "expected #{message.()}"} + end + end) + } + end + @doc """ Specifies that the data is an integer or a specific integer.