#[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
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
impl FieldType
Sourcepub fn of(value: &Value) -> FieldType
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.
Sourcepub fn admits(self, value: &Value) -> bool
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.
Stradmitspublicand[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 howvalidate_enumalready checks one. An empty list is a list of items every one of which fits, so it fits. Floatadmits an integer, andRefadmits a string, since a reference is stored as one.Extendedadmits an extended value of the same kind, or a string shaped like one — the same cheap guardFieldType::coerceuses when writing one. YAML, JSON and markdown frontmatter have no date literal, socreated: 1979-05-27there 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.
Sourcepub fn coerce(self, s: &str) -> Value
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
impl FieldType
Sourcepub fn from_name(name: &str) -> Option<FieldType>
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);Sourcepub fn suggest_name(name: &str) -> Option<&'static str>
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§
impl Copy for FieldType
Source§impl Display for FieldType
impl Display for FieldType
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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.