Skip to main content

fig_schema/
lib.rs

1//! `fig-schema` — the schema layer: what a field *expects* — its type, its
2//! allowed values, and how to present it — layered over [`fig`]'s schema-free
3//! value tree.
4//!
5//! fig parses bytes → [`fig::Value`] and edits losslessly; it has no notion of
6//! "what is valid here". This crate adds that knowledge as a **generic,
7//! embedder-agnostic** engine. It never learns the word "prov" or "flower": a
8//! consumer (prov, for frontmatter fields; flower, for a metadata editor)
9//! defines its own constraint type — an enum covering whatever kinds of
10//! constraint it needs (a controlled vocabulary, a reference into a
11//! workspace, …) — and implements [`Validate`] on it. [`FieldRule`]/[`Schema`]
12//! are generic over that type, so the path-matching and commit-time
13//! validation plumbing is written once, here, and reused by every embedder.
14//!
15//! What's genuinely reusable, and lives here as concrete types rather than
16//! being left to the embedder:
17//!
18//! - [`PathPat`] / [`SegPat`] — pattern-matching a fig path, including "every
19//!   item of this list" ([`SegPat::EachItem`]) and "this subtree"
20//!   ([`SegPat::AnyDepth`]).
21//! - [`FieldType`] — the expected type, and type-directed coercion of an edit
22//!   buffer ([`FieldType::coerce`]).
23//! - [`Term`] / [`Cardinality`] / [`validate_enum`] — a controlled vocabulary
24//!   and the logic to check a value against one (closed-vocabulary rejection,
25//!   open-vocabulary near-miss warnings). Cardinality (one vs. many) is pure
26//!   data shape, useful even to a constraint this crate doesn't otherwise model
27//!   (a relation/reference field, for instance).
28//! - [`VocabularyDoc`] / [`parse_vocabulary`] — load a term set from the
29//!   shared `vocabulary: { field, values }` / `terms:` document convention, so
30//!   independent embedders can point at the same vocabulary document without
31//!   either depending on the other.
32//! - [`Presentation`] / [`Icon`] / [`Tint`] — renderer-neutral display hints,
33//!   carried on every rule but never interpreted here.
34//!
35//! The public structs are `#[non_exhaustive]`, so they are built from a
36//! constructor plus chainable setters ([`FieldRule::new`], [`Term::value`],
37//! [`Presentation::default`]) rather than a struct literal. That is what lets a
38//! later release add a hint without costing every embedder a major version.
39//! - [`Issue`] / [`IssueKind`] — why a value failed, as data rather than
40//!   prose, so the embedder owns the wording. [`Issue`]'s `Display` renders a
41//!   reasonable English default for embedders that don't care.
42//!
43//! # Example
44//!
45//! ```
46//! use fig::Value;
47//! use fig_schema::{
48//!     FieldRule, FieldType, PathPat, Presentation, Schema, Seg, Term, Validate,
49//!     Validation, validate_enum,
50//! };
51//!
52//! // The embedder's own constraint type — the seam this crate is built around.
53//! struct Vocabulary { values: Vec<Term>, closed: bool }
54//!
55//! impl Validate for Vocabulary {
56//!     fn validate(&self, value: &Value) -> Validation {
57//!         validate_enum(&self.values, self.closed, value)
58//!     }
59//! }
60//!
61//! let schema = Schema::new(vec![
62//!     FieldRule::new(PathPat::each_item_of("audience"))
63//!         .ty(FieldType::Str)
64//!         .constraint(Vocabulary {
65//!             values: vec![Term::value("public"), Term::value("family")],
66//!             closed: true,
67//!         })
68//!         .present(Presentation::default().title("Audience")),
69//! ]);
70//!
71//! // Find the rule governing `audience[0]`, then check a candidate against it.
72//! let path = [Seg::Key("audience".into()), Seg::Index(0)];
73//! let rule = schema.rule_for(&path).expect("a rule governs this path");
74//! assert!(rule.validate(&Value::Str("public".into())).is_ok());
75//!
76//! let rejected = rule.validate(&Value::Str("familly".into()));
77//! assert!(rejected.is_reject());
78//! assert_eq!(
79//!     rejected.issue().unwrap().suggestion.as_deref(),
80//!     Some("family"),
81//! );
82//! ```
83
84mod field;
85mod path;
86mod present;
87mod vocab;
88
89pub use field::{FieldRule, FieldType, Schema};
90pub use path::{PathPat, Seg, SegPat};
91pub use present::{Icon, Presentation, Tint};
92pub use vocab::{
93    Cardinality, Issue, IssueKind, Term, Validate, Validation, VocabularyDoc, parse_vocabulary,
94    validate_enum,
95};