Skip to main content

FieldType

Enum FieldType 

Source
#[non_exhaustive]
pub enum FieldType { Null, Bool, Int, Float, Str, Ref, Extended(ExtKind), Map, Seq, }
Expand description

The type a field expects. Drives type-directed parsing and widget choice.

#[non_exhaustive]: fig gains ExtKinds and a schema gains field shapes in ordinary releases, so a match needs a _ arm. Constructing a variant is unaffected.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Null

§

Bool

§

Int

§

Float

§

Str

§

Ref

A link into the workspace (stored textually, like Str, but a reference).

§

Extended(ExtKind)

A format-specific scalar carried verbatim — a TOML datetime, a ZON enum or char literal. Coercing to one keeps the value’s native type instead of quoting it into a string, so a TOML date = 1979-05-27 survives an edit as a date rather than becoming date = "1979-05-27".

§

Map

§

Seq

Implementations§

Source§

impl FieldType

Source

pub fn of(value: &Value) -> FieldType

The type a parsed value has — the shape half of a check, and the counterpart of FieldType::coerce, which reads text into a type. Total: every Value has one. A Value::Uint is an Int, since that is only fig’s spelling for an integer past i64::MAX.

Never Ref: a reference is stored as text, and nothing in the value says it is one. FieldType::admits is where Ref accepts a string.

Source

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

Whether a parsed value already has this type — the question a whole-document check asks at every governed node, which FieldType::coerce does not: that reads an edit buffer into a type, and this asks whether what the parser produced fits one.

Mostly FieldType::of and equality, plus the widenings a reader expects, each of which is a convention the rest of the crate already follows rather than a choice made here:

  • A type names the item, and a list of items has the type too. Str admits public and [public, family] alike, one level deep — a list of strings and not a list of lists — because an embedder declares a field once and writes it as a scalar or a list as the document pleases, which is how validate_enum already checks one. An empty list is a list of items every one of which fits, so it fits.
  • Float admits an integer, and Ref admits a string, since a reference is stored as one.
  • Extended admits an extended value of the same kind, or a string shaped like one — the same cheap guard FieldType::coerce uses when writing one. YAML, JSON and markdown frontmatter have no date literal, so created: 1979-05-27 there is text, and a date field that refused it would fail every document in those formats against its own schema. The value stays a string; this says only that it could be the type it was declared.
Source

pub fn coerce(self, s: &str) -> Value

Coerce an edit-buffer string to this type — the schema-directed counterpart of shape-guessing. A value that doesn’t fit the type falls back to a string (the caller’s own reparse is the final backstop); container types are not scalar-edited, so they also pass through as text.

The numeric types go through Value::parse_number, so the text fig itself writes reads back unchanged — including the .inf/.nan spellings str::parse::<f64> rejects. Those still have no representation in JSON or TOML, so an embedder targeting those formats should reject them before they reach here.

Source§

impl FieldType

Source

pub fn from_name(name: &str) -> Option<FieldType>

The type a schema document’s type key names — int, str, date, seq — or None for a name the format does not define. The loader treats that None as an error rather than a dropped type; see FieldType::suggest_name for the sentence it adds.

use fig::ExtKind;
use fig_schema::FieldType;

assert_eq!(FieldType::from_name("str"), Some(FieldType::Str));
assert_eq!(FieldType::from_name("date"), Some(FieldType::Extended(ExtKind::LocalDate)));
assert_eq!(FieldType::from_name("string"), None);
Source

pub fn suggest_name(name: &str) -> Option<&'static str>

The name a person who wrote name most likely meant, for the error a loader reports: string is answered with str, since that is figl’s own annotation name and the one most likely reached for; boolean, integer, list, array, object, dict and text with the type each is another word for. None when nothing is close.

Trait Implementations§

Source§

impl Clone for FieldType

Source§

fn clone(&self) -> Self

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 Copy for FieldType

Source§

impl Debug for FieldType

Source§

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

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

impl Display for FieldType

Source§

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

The type’s name as the schema document format spells it — int, str, date, seq — so a message reads the way the rule was written, and FieldType::from_name reads it back. An extended kind the format cannot name is its fig name in kebab case (number-special), or extended for a kind newer than this crate.

Source§

impl Eq for FieldType

Source§

impl PartialEq for FieldType

Source§

fn eq(&self, other: &Self) -> 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 FieldType

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.