#[non_exhaustive]pub enum ScalarStyle {
#[non_exhaustive] Plain {
implicit: bool,
},
#[non_exhaustive] SingleQuoted,
#[non_exhaustive] DoubleQuoted,
#[non_exhaustive] Literal {
syntax: Option<BlockScalarSyntax>,
},
#[non_exhaustive] Folded {
syntax: Option<BlockScalarSyntax>,
},
}Expand description
The source notation and applicable syntax metadata of a YAML scalar.
Inspect styles with matches!, if let, or match, using { .. } even for variants
that currently have no fields. The rest pattern ignores metadata you do not need and lets
the library add fields later without changing your code. The enum and every variant are
non-exhaustive, so a complete match also needs a fallback arm.
Normal builds provide no equality, ordering, or hashing. Comparing every style field would
make classification depend on all present and future metadata. Match the variant instead,
then inspect the fields relevant to your use case. Self::kind returns a comparable,
fieldless category when a map key or ordering is needed. The opt-in test_equality feature
compares only the notation category for tests. It ignores the implicit flag and all
extended_details fields: source information may be absent on synthetic values, and enabling
optional capture through another dependency must not change equality. Compare implicit or syntax
explicitly when testing those details. Metadata defaults are not equality wildcards;
source metadata is always excluded so equality remains transitive.
Constructors create styles for synthetic events and alternative parser implementations.
They are independent of classification: use patterns to inspect the resulting values.
Their signatures are the same with and without extended_details. The feature adds
optional block syntax fields and builders; synthetic block styles start without known
source syntax. Omitted-node identity and Self::is_omitted are available in every build.
This keeps construction independent of future metadata additions.
use granit_parser::{Parser, ScalarStyle};
let (event, _) = Parser::new_from_str("value").nth(2).unwrap().unwrap();
let (_, style) = event.scalar().unwrap();
assert!(matches!(style, ScalarStyle::Plain { .. }));
assert!(!matches!(style, ScalarStyle::SingleQuoted { .. }));Whole-style equality is intentionally unavailable by default:
use granit_parser::ScalarStyle;
fn equal_styles(left: ScalarStyle, right: ScalarStyle) -> bool {
left == right
}Styles are not hash keys; use ScalarKind when only the notation category matters:
use granit_parser::ScalarStyle;
fn require_hash<T: core::hash::Hash>() {}
require_hash::<ScalarStyle>();Non-exhaustive variants cannot be constructed directly outside this crate:
use granit_parser::ScalarStyle;
let style = ScalarStyle::SingleQuoted;Use a constructor to create a synthetic style:
use granit_parser::ScalarStyle;
let style = ScalarStyle::literal();
assert!(matches!(style, ScalarStyle::Literal { .. }));Variant patterns must allow future fields:
use granit_parser::ScalarStyle;
fn inspect(style: ScalarStyle) {
if let ScalarStyle::Literal {} = style {}
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
#[non_exhaustive]Plain
A plain scalar or a synthesized empty node.
Fields
This variant is marked as non-exhaustive
implicit: boolWhether the parser synthesized an omitted node instead of reading scalar text.
Explicit tags and anchors can decorate an omitted node. Written empty quoted or
block scalars have their own styles and are never implicit plain scalars.
This distinction is available without extended_details.
#[non_exhaustive]SingleQuoted
A single-quoted scalar.
#[non_exhaustive]DoubleQuoted
A double-quoted scalar.
#[non_exhaustive]Literal
A literal block, with no escape processing or line folding.
Fields
This variant is marked as non-exhaustive
syntax: Option<BlockScalarSyntax>Header syntax relative to the accompanying scalar span, when known.
Parsed block styles contain Some when all details fit in u32;
synthetic styles and unrepresentable parsed details use None.
Available only with extended_details.
#[non_exhaustive]Folded
A folded block, whose line breaks are subject to YAML line folding.
Fields
This variant is marked as non-exhaustive
syntax: Option<BlockScalarSyntax>Header syntax relative to the accompanying scalar span, when known.
Parsed block styles contain Some when all details fit in u32;
synthetic styles and unrepresentable parsed details use None.
Available only with extended_details.
Implementations§
Source§impl ScalarStyle
impl ScalarStyle
Sourcepub const fn implicit_plain() -> Self
pub const fn implicit_plain() -> Self
Return the style of an omitted node synthesized by a parser.
Sourcepub const fn with_implicit(self, implicit: bool) -> Self
pub const fn with_implicit(self, implicit: bool) -> Self
Set whether a plain scalar represents an omitted node.
Other styles are returned unchanged because this detail applies only to plain scalars.
Sourcepub const fn single_quoted() -> Self
pub const fn single_quoted() -> Self
Return single-quoted notation.
Sourcepub const fn double_quoted() -> Self
pub const fn double_quoted() -> Self
Return double-quoted notation.
Sourcepub const fn with_block_syntax(self, syntax: BlockScalarSyntax) -> Self
pub const fn with_block_syntax(self, syntax: BlockScalarSyntax) -> Self
Attach known source-header syntax to a literal or folded style.
Other styles are returned unchanged because block-header syntax does not apply to them.
Available only with extended_details.
use granit_parser::{BlockScalarSyntax, ScalarStyle};
let style = ScalarStyle::literal();
assert!(style.block_syntax().is_none());
let style = style.with_block_syntax(BlockScalarSyntax::default());
assert!(style.block_syntax().is_some());Sourcepub const fn kind(&self) -> ScalarKind
pub const fn kind(&self) -> ScalarKind
Return the notation category, ignoring source positions and all other metadata.
Sourcepub const fn is_plain(&self) -> bool
pub const fn is_plain(&self) -> bool
Return whether this is plain notation, including synthesized empty nodes.
Equivalent to matches!(self, ScalarStyle::Plain { .. }).
Sourcepub const fn is_omitted(&self) -> bool
pub const fn is_omitted(&self) -> bool
Return whether the parser synthesized an omitted node rather than reading scalar text.
Written null spellings such as ~ and null, empty quoted strings, and empty block
scalars return false. An omitted node can still have an explicit tag or anchor.
Sourcepub const fn is_single_quoted(&self) -> bool
pub const fn is_single_quoted(&self) -> bool
Return whether this is single-quoted notation.
Sourcepub const fn is_double_quoted(&self) -> bool
pub const fn is_double_quoted(&self) -> bool
Return whether this is double-quoted notation.
Sourcepub const fn is_literal(&self) -> bool
pub const fn is_literal(&self) -> bool
Return whether this is literal block notation.
Sourcepub const fn block_syntax(&self) -> Option<&BlockScalarSyntax>
pub const fn block_syntax(&self) -> Option<&BlockScalarSyntax>
Return known block-header syntax for literal and folded scalars.
Returns None for other styles, synthetic block styles with no attached syntax, and
parsed block styles whose header distances or content indentation exceed u32::MAX.
Available only with extended_details.