Skip to main content

Consequence

Struct Consequence 

Source
#[non_exhaustive]
pub struct Consequence { pub when: Option<Value>, pub severity: Severity, pub message: String, }
Expand description

A cost of changing a field, declared on the rule that governs it.

A different fact from Presentation’s Tint. A tint says how loudly to draw a field; a consequence says what happens if the user goes through with the change — that switching a metadata format rewrites every document in the archive, that turning a recycle bin off makes deletion unrecoverable. A field can be drawn calmly and still be expensive to change, and a field can be drawn in red and cost nothing.

Three properties are deliberate, and a host that assumes otherwise will be subtly wrong:

  • A guard names the value being landed, not a transition. There is no from/to matrix: Consequence::when names a destination. The host knows the current value; this crate does not. A cost that only applies coming from a particular value is declared as a plain consequence on the destination, and suppressed by the host that can see the difference.
  • Deletion resolves to whatever the host’s default is. This crate has no concept of a field being absent, and Value::Null is not a spelling for it — a written null is a real value, which FieldType::Null coerces. A host removing a field should ask about the value the removal actually resolves to, or about nothing.
  • No-op detection is the caller’s. FieldRule::consequences_of has no current value to compare against, so re-setting a field to what it already holds answers exactly as setting it afresh does. Suppressing that is the host’s job — and it is what makes the first property work.

#[non_exhaustive]: built from Consequence::always or Consequence::when plus Consequence::severity. Reading the fields is unchanged.

use fig::Value;
use fig_schema::{Consequence, Severity};

let c = Consequence::when(false, "Deleted items will be gone for good.")
    .severity(Severity::ConfirmExplicitly);
assert_eq!(c.when, Some(Value::Bool(false)));

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.
§when: Option<Value>

The destination value this applies to, or None for any change to the field. Compared with Value::eq_canonical, so a guard written Value::Int(1) still matches a document that parsed 1 as a Value::Uint.

§severity: Severity

What the host should do about it.

§message: String

The sentence to show. Prose rather than a taxonomy: a consequence is specific to the field, and no closed set of effect kinds would spare the author from writing it.

Implementations§

Source§

impl Consequence

Source

pub fn always(message: impl Into<String>) -> Self

A consequence of changing the field at all, whatever the new value. Severity::Notice until Consequence::severity says otherwise.

Source

pub fn when(value: impl Into<Value>, message: impl Into<String>) -> Self

A consequence of changing the field to value — a destination, not a transition. See Consequence for why there is no from/to matrix.

Source

pub fn severity(self, severity: Severity) -> Self

Set what the host should do about this.

Source

pub fn applies_to(&self, value: &Value) -> bool

Whether this applies to landing value. An unguarded consequence applies to every value.

Trait Implementations§

Source§

impl Clone for Consequence

Source§

fn clone(&self) -> Consequence

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 Debug for Consequence

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Consequence

Source§

fn eq(&self, other: &Consequence) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Consequence

Auto Trait Implementations§

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.