pub struct ParserStack<'input, I = Empty<char>, T = StrInput<'input>>{ /* private fields */ }Expand description
A parser implementation that uses a stack for include-style parsing.
Note: ParserStack deliberately suppresses nested Event::StreamStart /
Event::DocumentStart events when more than one parser is stacked, and the tests assert
outputs where a nested parser starts directly with Event::MappingStart before the parent
stream/document wrapper appears.
That is exactly what we want for !include-style subtree injection.
Included parser events are replayed through the same event stream as parent events. With the
comments Cargo feature, this includes comment events unless emission is disabled through
ParserStack::with_options. Without the feature, comment events and their continuation state
are absent.
Anchor metadata is passed through from parsers that enable it. Each Parser emits
Event::Anchor immediately after its anchored node. Pushing another parser between
consuming that node and consuming its metadata can insert events between them in the
combined stream. If relying on adjacency, consume any pending anchor metadata before
calling Self::push_include or another push method.
Included Span values remain local to the included source. Use source-aware push methods
such as Self::push_str_parser_with_source_id and consume Self::next_sourced to associate
each result with an opaque caller-supplied key. This does not change events or spans, and the
existing iterator continues to return ordinary parse results. Errors also retain the nested
source names through ScanError::source_stack.
Implementations§
Source§impl<'input, I, T> ParserStack<'input, I, T>
impl<'input, I, T> ParserStack<'input, I, T>
Sourcepub fn with_options(options: Options) -> Self
pub fn with_options(options: Options) -> Self
Creates a new, empty parser stack with the supplied parsing options.
Options are applied to sources parsed internally by Self::push_include. Caller-supplied
parsers retain their own parsing options, including Options::emit_unresolved_aliases
and Options::emit_anchor_events. Unresolved alias and anchor metadata events from
caller-supplied parsers and replay streams are passed through.
With the comments Cargo feature, disabling comment emission also suppresses comment
events from parsers and replay streams supplied by the caller. Construct caller-supplied
parsers with emission disabled as well to avoid capturing comments before filtering them.
Without the feature, comment events are absent and Options::emit_comments has no effect.
Sourcepub fn set_resolver(
&mut self,
resolver: impl FnMut(&str) -> Result<String, ScanError> + 'input,
)
pub fn set_resolver( &mut self, resolver: impl FnMut(&str) -> Result<String, ScanError> + 'input, )
Set the resolver used by Self::push_include.
The resolver receives the include name and returns the included YAML source text.
Sourcepub fn set_borrowed_resolver(
&mut self,
resolver: impl FnMut(&str) -> Result<&'input str, ScanError> + 'input,
)
pub fn set_borrowed_resolver( &mut self, resolver: impl FnMut(&str) -> Result<&'input str, ScanError> + 'input, )
Set an include resolver whose source text can be borrowed for the stack’s input lifetime.
Unlike Self::set_resolver, this path lets scalar, anchor, and tag token text in included
documents borrow directly from the returned source, as well as comment text when the
comments Cargo feature is enabled. The included document is still validated eagerly so
resolution errors retain the same timing and source-stack context.
Sourcepub fn push_include(&mut self, include_str: &str) -> Result<(), ScanError>
pub fn push_include(&mut self, include_str: &str) -> Result<(), ScanError>
Resolve an include by name and push the resulting parser onto the stack.
With the comments Cargo feature, comment events from the included content follow the
setting passed to Self::with_options. Anchor metadata events likewise follow
Options::emit_anchor_events. All event spans are local to the included content returned
by the resolver.
§Errors
Returns ScanError if no resolver is configured, include resolution fails, or the
included content cannot be parsed.
Sourcepub fn push_include_with_source_id(
&mut self,
include_str: &str,
source_id: SourceId,
) -> Result<(), Sourced<ScanError>>
pub fn push_include_with_source_id( &mut self, include_str: &str, source_id: SourceId, ) -> Result<(), Sourced<ScanError>>
Resolve and push an include with a caller-supplied source key.
Eager resolution or parsing failures carry the supplied key. Successfully pushed content
carries it on every result returned by Self::next_sourced.
§Errors
Returns a sourced error under the same conditions as Self::push_include.
Sourcepub fn push_str_parser(
&mut self,
parser: Parser<'input, StrInput<'input>>,
name: String,
)
pub fn push_str_parser( &mut self, parser: Parser<'input, StrInput<'input>>, name: String, )
Push a string parser onto the stack.
The pushed parser inherits the current anchor offset so anchors remain unique across stacked
sources. name is returned by Self::stack for diagnostics.
Sourcepub fn push_iter_parser(
&mut self,
parser: Parser<'static, BufferedInput<I>>,
name: String,
)
pub fn push_iter_parser( &mut self, parser: Parser<'static, BufferedInput<I>>, name: String, )
Push an iterator-backed parser onto the stack.
The pushed parser inherits the current anchor offset so anchors remain unique across stacked
sources. name is returned by Self::stack for diagnostics.
Sourcepub fn push_custom_parser(&mut self, parser: Parser<'input, T>, name: String)
pub fn push_custom_parser(&mut self, parser: Parser<'input, T>, name: String)
Push a custom-input parser onto the stack.
The pushed parser inherits the current anchor offset so anchors remain unique across stacked
sources. name is returned by Self::stack for diagnostics.
Sourcepub fn push_replay_parser(&mut self, parser: ReplayParser<'input>, name: String)
pub fn push_replay_parser(&mut self, parser: ReplayParser<'input>, name: String)
Push a replay parser onto the stack.
Replay parsers are used for included content that has already been parsed into events.
name is returned by Self::stack for diagnostics.
Sourcepub fn push_custom_parser_with_current(
&mut self,
parser: Parser<'input, T>,
name: String,
current: (Event<'input>, Span),
)
pub fn push_custom_parser_with_current( &mut self, parser: Parser<'input, T>, name: String, current: (Event<'input>, Span), )
Push a custom parser and set the first event that should be returned from it.
This is used when the caller has already consumed the parser’s first event before deciding
to place it on the stack. The event follows the same filtering rules as subsequent events
from this source. Before reading anchored nodes, call
parser.set_anchor_offset(parser.anchor_offset().max(stack.current_anchor_offset())).
This preserves the parser’s starting ID when the stack’s offset is zero. For a replay
parent, reserve its unread anchor IDs as described in ReplayParser::new.
If Event::anchor_id or Event::alias_id yields an ID below the stack’s offset
at push time, reading that event returns ErrorKind::AnchorIdCollision. This can
happen if the child read an anchor before being seeded, or if the parent advanced
after seeding.
Sourcepub fn push_str_parser_with_source_id(
&mut self,
parser: Parser<'input, StrInput<'input>>,
name: String,
source_id: SourceId,
)
pub fn push_str_parser_with_source_id( &mut self, parser: Parser<'input, StrInput<'input>>, name: String, source_id: SourceId, )
Push a string parser with a source key returned by Self::next_sourced.
Sourcepub fn push_iter_parser_with_source_id(
&mut self,
parser: Parser<'static, BufferedInput<I>>,
name: String,
source_id: SourceId,
)
pub fn push_iter_parser_with_source_id( &mut self, parser: Parser<'static, BufferedInput<I>>, name: String, source_id: SourceId, )
Push an iterator parser with a source key returned by Self::next_sourced.
Sourcepub fn push_custom_parser_with_source_id(
&mut self,
parser: Parser<'input, T>,
name: String,
source_id: SourceId,
)
pub fn push_custom_parser_with_source_id( &mut self, parser: Parser<'input, T>, name: String, source_id: SourceId, )
Push a custom parser with a source key returned by Self::next_sourced.
Sourcepub fn push_replay_parser_with_source_id(
&mut self,
parser: ReplayParser<'input>,
name: String,
source_id: SourceId,
)
pub fn push_replay_parser_with_source_id( &mut self, parser: ReplayParser<'input>, name: String, source_id: SourceId, )
Push replayed events with a source key returned by Self::next_sourced.
Sourcepub fn push_custom_parser_with_current_and_source_id(
&mut self,
parser: Parser<'input, T>,
name: String,
current: (Event<'input>, Span),
source_id: SourceId,
)
pub fn push_custom_parser_with_current_and_source_id( &mut self, parser: Parser<'input, T>, name: String, current: (Event<'input>, Span), source_id: SourceId, )
Push a custom parser and its already consumed first event with a source key.
Both the supplied event and subsequent parser results retain source_id.
See Self::push_custom_parser_with_current for anchor preparation and event filtering.
Sourcepub fn current_anchor_offset(&self) -> usize
pub fn current_anchor_offset(&self) -> usize
Return the anchor offset that a newly pushed parser should inherit.
Sourcepub fn source_names(&self) -> SourceNames<'_, 'input, I, T> ⓘ
pub fn source_names(&self) -> SourceNames<'_, 'input, I, T> ⓘ
Borrow active source names from bottom to top without allocating.
Sourcepub fn source_ids(&self) -> SourceIds<'_, 'input, I, T> ⓘ
pub fn source_ids(&self) -> SourceIds<'_, 'input, I, T> ⓘ
Return active source keys from bottom to top without allocating.
Sourcepub fn current_source_id(&self) -> Option<SourceId>
pub fn current_source_id(&self) -> Option<SourceId>
Return the key of the currently active top parser, if one was supplied.
This describes the active stack, not the source of a previously emitted event or error.
Use Self::next_sourced for that association, which survives source removal.
Sourcepub fn stack(&self) -> Vec<String>
pub fn stack(&self) -> Vec<String>
Return owned names of active sources, from bottom to top.
Use Self::source_names to inspect names without cloning, or Self::depth when only
the number of active parsers is needed.
Sourcepub fn next_sourced(&mut self) -> Option<Sourced<ParseResult<'input>>>
pub fn next_sourced(&mut self) -> Option<Sourced<ParseResult<'input>>>
Consume the next parse result together with its caller-supplied source key.
This shares iteration and lookahead state with ParserTrait::peek and
Iterator::next. A result buffered by peek retains its original key even if another
parser is subsequently pushed. Pushing after a peeked stream end replaces that end with
the new source. An error retains the failing key after that source is popped. Sources
pushed through the ordinary methods have no key.
Trait Implementations§
Source§impl<'input, I, T> Default for ParserStack<'input, I, T>
impl<'input, I, T> Default for ParserStack<'input, I, T>
impl<'input, I, T> FusedIterator for ParserStack<'input, I, T>
Source§impl<'input, I, T> Iterator for ParserStack<'input, I, T>
impl<'input, I, T> Iterator for ParserStack<'input, I, T>
Source§type Item = Result<(Event<'input>, Span), ScanError>
type Item = Result<(Event<'input>, Span), ScanError>
Source§fn next(&mut self) -> Option<Self::Item>
fn next(&mut self) -> Option<Self::Item>
Source§fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
iter_next_chunk)N values. Read more1.0.0 (const: unstable) · Source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
1.0.0 (const: unstable) · Source§fn count(self) -> usizewhere
Self: Sized,
fn count(self) -> usizewhere
Self: Sized,
1.0.0 (const: unstable) · Source§fn last(self) -> Option<Self::Item>where
Self: Sized,
fn last(self) -> Option<Self::Item>where
Self: Sized,
Source§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
iter_advance_by)n elements. Read more1.0.0 (const: unstable) · Source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
nth element of the iterator. Read more1.28.0 (const: unstable) · Source§fn step_by(self, step: usize) -> StepBy<Self> ⓘwhere
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self> ⓘwhere
Self: Sized,
1.0.0 (const: unstable) · Source§fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> ⓘ
fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> ⓘ
1.0.0 (const: unstable) · Source§fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> ⓘwhere
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> ⓘwhere
Self: Sized,
U: IntoIterator,
Source§fn intersperse(self, separator: Self::Item) -> Intersperse<Self> ⓘ
fn intersperse(self, separator: Self::Item) -> Intersperse<Self> ⓘ
iter_intersperse)separator between items
of the original iterator. Read moreSource§fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> ⓘ
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> ⓘ
iter_intersperse)separator
between items of the original iterator. Read more1.0.0 (const: unstable) · Source§fn map<B, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<B, F>(self, f: F) -> Map<Self, F> ⓘ
1.21.0 (const: unstable) · Source§fn for_each<F>(self, f: F)
fn for_each<F>(self, f: F)
1.0.0 (const: unstable) · Source§fn filter<P>(self, predicate: P) -> Filter<Self, P> ⓘ
fn filter<P>(self, predicate: P) -> Filter<Self, P> ⓘ
1.0.0 (const: unstable) · Source§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> ⓘ
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> ⓘ
1.0.0 (const: unstable) · Source§fn enumerate(self) -> Enumerate<Self> ⓘwhere
Self: Sized,
fn enumerate(self) -> Enumerate<Self> ⓘwhere
Self: Sized,
1.0.0 (const: unstable) · Source§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> ⓘ
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> ⓘ
1.0.0 (const: unstable) · Source§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> ⓘ
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> ⓘ
1.57.0 (const: unstable) · Source§fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> ⓘ
fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> ⓘ
1.0.0 (const: unstable) · Source§fn skip(self, n: usize) -> Skip<Self> ⓘwhere
Self: Sized,
fn skip(self, n: usize) -> Skip<Self> ⓘwhere
Self: Sized,
n elements. Read more1.0.0 (const: unstable) · Source§fn take(self, n: usize) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, n: usize) -> Take<Self> ⓘwhere
Self: Sized,
n elements, or fewer
if the underlying iterator ends sooner. Read more1.0.0 (const: unstable) · Source§fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> ⓘ
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> ⓘ
1.0.0 (const: unstable) · Source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> ⓘ
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> ⓘ
1.29.0 (const: unstable) · Source§fn flatten(self) -> Flatten<Self> ⓘ
fn flatten(self) -> Flatten<Self> ⓘ
Source§fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N> ⓘ
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N> ⓘ
iter_map_windows)f for each contiguous window of size N over
self and returns an iterator over the outputs of f. Like slice::windows(),
the windows during mapping overlap as well. Read more1.0.0 (const: unstable) · Source§fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
1.0.0 (const: unstable) · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Iterator. Read more1.0.0 (const: unstable) · Source§fn collect<B>(self) -> B
fn collect<B>(self) -> B
Source§fn try_collect<B>(
&mut self,
) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType
fn try_collect<B>( &mut self, ) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType
iterator_try_collect)Source§fn collect_into<E>(self, collection: &mut E) -> &mut E
fn collect_into<E>(self, collection: &mut E) -> &mut E
iter_collect_into)1.0.0 (const: unstable) · Source§fn partition<B, F>(self, f: F) -> (B, B)
fn partition<B, F>(self, f: F) -> (B, B)
Source§fn is_partitioned<P>(self, predicate: P) -> bool
fn is_partitioned<P>(self, predicate: P) -> bool
iter_is_partitioned)true precede all those that return false. Read more1.27.0 (const: unstable) · Source§fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
1.27.0 (const: unstable) · Source§fn try_for_each<F, R>(&mut self, f: F) -> R
fn try_for_each<F, R>(&mut self, f: F) -> R
1.0.0 (const: unstable) · Source§fn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
1.51.0 (const: unstable) · Source§fn reduce<F>(self, f: F) -> Option<Self::Item>
fn reduce<F>(self, f: F) -> Option<Self::Item>
Source§fn try_reduce<R>(
&mut self,
f: impl FnMut(Self::Item, Self::Item) -> R,
) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
fn try_reduce<R>( &mut self, f: impl FnMut(Self::Item, Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
iterator_try_reduce)1.0.0 (const: unstable) · Source§fn all<F>(&mut self, f: F) -> bool
fn all<F>(&mut self, f: F) -> bool
1.0.0 (const: unstable) · Source§fn any<F>(&mut self, f: F) -> bool
fn any<F>(&mut self, f: F) -> bool
1.0.0 (const: unstable) · Source§fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
1.30.0 (const: unstable) · Source§fn find_map<B, F>(&mut self, f: F) -> Option<B>
fn find_map<B, F>(&mut self, f: F) -> Option<B>
Source§fn try_find<R>(
&mut self,
f: impl FnMut(&Self::Item) -> R,
) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
fn try_find<R>( &mut self, f: impl FnMut(&Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
try_find)1.0.0 (const: unstable) · Source§fn position<P>(&mut self, predicate: P) -> Option<usize>
fn position<P>(&mut self, predicate: P) -> Option<usize>
1.0.0 (const: unstable) · Source§fn max(self) -> Option<Self::Item>
fn max(self) -> Option<Self::Item>
1.0.0 (const: unstable) · Source§fn min(self) -> Option<Self::Item>
fn min(self) -> Option<Self::Item>
1.6.0 (const: unstable) · Source§fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 (const: unstable) · Source§fn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
1.6.0 (const: unstable) · Source§fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 (const: unstable) · Source§fn min_by<F>(self, compare: F) -> Option<Self::Item>
fn min_by<F>(self, compare: F) -> Option<Self::Item>
1.0.0 (const: unstable) · Source§fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
1.36.0 (const: unstable) · Source§fn copied<'a, T>(self) -> Copied<Self> ⓘ
fn copied<'a, T>(self) -> Copied<Self> ⓘ
Source§fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> ⓘwhere
Self: Sized,
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> ⓘwhere
Self: Sized,
iter_array_chunks)N elements of the iterator at a time. Read more1.11.0 (const: unstable) · Source§fn product<P>(self) -> P
fn product<P>(self) -> P
Source§fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
iter_order_by)Iterator with those
of another with respect to the specified comparison function. Read more1.5.0 (const: unstable) · Source§fn partial_cmp<I>(self, other: I) -> Option<Ordering>
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
PartialOrd elements of
this Iterator with those of another. The comparison works like short-circuit
evaluation, returning a result without comparing the remaining elements.
As soon as an order can be determined, the evaluation stops and a result is returned. Read moreSource§fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
iter_order_by)Iterator with those
of another with respect to the specified comparison function. Read moreSource§fn eq_by<I, F>(self, other: I, eq: F) -> bool
fn eq_by<I, F>(self, other: I, eq: F) -> bool
iter_order_by)1.5.0 (const: unstable) · Source§fn lt<I>(self, other: I) -> bool
fn lt<I>(self, other: I) -> bool
Iterator are lexicographically
less than those of another. Read more1.5.0 (const: unstable) · Source§fn le<I>(self, other: I) -> bool
fn le<I>(self, other: I) -> bool
Iterator are lexicographically
less or equal to those of another. Read more1.5.0 (const: unstable) · Source§fn gt<I>(self, other: I) -> bool
fn gt<I>(self, other: I) -> bool
Iterator are lexicographically
greater than those of another. Read more1.5.0 (const: unstable) · Source§fn ge<I>(self, other: I) -> bool
fn ge<I>(self, other: I) -> bool
Iterator are lexicographically
greater than or equal to those of another. Read more1.82.0 (const: unstable) · Source§fn is_sorted(self) -> bool
fn is_sorted(self) -> bool
1.82.0 (const: unstable) · Source§fn is_sorted_by<F>(self, compare: F) -> bool
fn is_sorted_by<F>(self, compare: F) -> bool
Source§impl<'input, I, T> ParserTrait<'input> for ParserStack<'input, I, T>
impl<'input, I, T> ParserTrait<'input> for ParserStack<'input, I, T>
Source§fn peek(&mut self) -> Option<Result<&(Event<'input>, Span), ScanError>>
fn peek(&mut self) -> Option<Result<&(Event<'input>, Span), ScanError>>
self.Source§fn next_event(&mut self) -> Option<ParseResult<'input>>
fn next_event(&mut self) -> Option<ParseResult<'input>>
self.