#[non_exhaustive]pub struct FieldRule<C> {
pub at: PathPat,
pub ty: Option<FieldType>,
pub constraint: Option<C>,
pub present: Presentation,
pub on_change: Vec<Consequence>,
}Expand description
One field rule: which node(s) it governs, the type it expects, an optional
constraint of the embedder’s own type C, and how to present it.
#[non_exhaustive]: a rule gains ways to describe a field over time, so it
is built from FieldRule::new and the chainable setters rather than a
struct literal. Reading the fields is unchanged.
use fig_schema::{FieldRule, FieldType, PathPat, Presentation, Validate, Validation};
let rule = FieldRule::new(PathPat::each_item_of("audience"))
.ty(FieldType::Str)
.constraint(Vocab)
.present(Presentation::default().title("Audience"));
assert_eq!(rule.ty, Some(FieldType::Str));Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.at: PathPatWhich node(s) this governs (reaches list elements, not only scalars).
ty: Option<FieldType>The expected type — drives type-directed parsing and widget choice.
constraint: Option<C>A value constraint, in whatever shape the embedder defines.
present: PresentationRenderer-neutral presentation hints.
on_change: Vec<Consequence>What changing this field costs, if anything — see Consequence.
Beside the presentation hints rather than inside them: a cost is a fact about the field, not a way of drawing it, and a host that ignores every other hint must still honour these.
Implementations§
Source§impl<C> FieldRule<C>
impl<C> FieldRule<C>
Sourcepub fn consequences_of(&self, value: &Value) -> Vec<&Consequence>
pub fn consequences_of(&self, value: &Value) -> Vec<&Consequence>
Every consequence of landing value on this field, in declaration
order — the unguarded ones and the guards that match.
All of them, not the worst: two consequences can be true at once (a
blanket “this rewrites the archive” and a value-specific “and none
cannot be undone”), and dropping either loses a sentence the user needed.
For picking one interaction, use FieldRule::severity_of.
Sourcepub fn severity_of(&self, value: &Value) -> Option<Severity>
pub fn severity_of(&self, value: &Value) -> Option<Severity>
The most severe consequence of landing value, or None if there is
none. Allocates nothing — this runs per keystroke in an editor deciding
whether to arm a confirm.
Source§impl<C> FieldRule<C>
impl<C> FieldRule<C>
Sourcepub fn new(at: PathPat) -> Self
pub fn new(at: PathPat) -> Self
A rule governing at, with no type, no constraint and no presentation
hints — the parts a caller adds with the setters below.
Sourcepub fn ty(self, ty: impl Into<Option<FieldType>>) -> Self
pub fn ty(self, ty: impl Into<Option<FieldType>>) -> Self
Set the expected type. Takes a FieldType or an Option<FieldType>,
so a caller reading a config that may not declare one can pass it
straight through.
Sourcepub fn constraint(self, constraint: C) -> Self
pub fn constraint(self, constraint: C) -> Self
Set the value constraint.
This one takes a C rather than an impl Into<Option<C>> the way
FieldRule::ty does: with C otherwise unconstrained, Into cannot
tell C from Option<C> and the call fails to infer. Use
FieldRule::constraint_opt for a constraint that may be absent.
Sourcepub fn constraint_opt(self, constraint: Option<C>) -> Self
pub fn constraint_opt(self, constraint: Option<C>) -> Self
Set the value constraint from an optional one. None leaves the rule
imposing nothing, which is what a type-only rule wants.
Sourcepub fn present(self, present: Presentation) -> Self
pub fn present(self, present: Presentation) -> Self
Set the presentation hints.
Sourcepub fn on_change(self, consequence: Consequence) -> Self
pub fn on_change(self, consequence: Consequence) -> Self
Declare one more consequence of changing this field. Appends, so a rule
can carry a blanket cost and a value-specific one by calling this twice
— see FieldRule::consequences_of.
Sourcepub fn on_change_all(self, consequences: Vec<Consequence>) -> Self
pub fn on_change_all(self, consequences: Vec<Consequence>) -> Self
Set the whole consequence list at once, for a caller building it from a config rather than declaring it inline. Replaces rather than appends.