Skip to main content

FieldRule

Struct FieldRule 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§at: PathPat

Which 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: Presentation

Renderer-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>

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn present(self, present: Presentation) -> Self

Set the presentation hints.

Source

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.

Source

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.

Source§

impl<C: Validate> FieldRule<C>

Source

pub fn validate(&self, value: &Value) -> Validation

Validate a candidate value against this rule’s constraint. A rule with no constraint (or a type-only rule) imposes nothing here.

Trait Implementations§

Source§

impl<C: Clone> Clone for FieldRule<C>

Source§

fn clone(&self) -> FieldRule<C>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug> Debug for FieldRule<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> Freeze for FieldRule<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for FieldRule<C>
where C: RefUnwindSafe,

§

impl<C> Send for FieldRule<C>
where C: Send,

§

impl<C> Sync for FieldRule<C>
where C: Sync,

§

impl<C> Unpin for FieldRule<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for FieldRule<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for FieldRule<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.