pub struct Sequence(/* private fields */);Expand description
A YAML sequence (list)
Note: This type uses interior mutability through the rowan library.
Mutation methods work even when called through &self. See the crate-level
documentation for details on the mutability model.
Implementations§
Source§impl Sequence
impl Sequence
Sourcepub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
pub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
Iterate over items in this sequence as YamlNodes.
Items that cannot be wrapped as a YamlNode are silently skipped.
Source§impl Sequence
impl Sequence
Sourcepub fn new_flow() -> Self
pub fn new_flow() -> Self
An empty sequence that renders as [] and stays flow when filled.
Use this for a sequence that is meant to be flow-style in the output, including one that is left empty.
Sourcepub fn new_pending_block() -> Self
pub fn new_pending_block() -> Self
An empty sequence that becomes a block sequence once an item is added.
YAML has no block syntax for an empty sequence: block collections are
written as their entries, so with none left there is nothing to write,
and a bare key: reads back as null. An empty sequence is therefore
[] whatever its eventual style, and block-vs-flow only becomes a
real choice once there is an item.
This renders as exactly [], plus a zero-width token that marks it as
destined for block style. Sequence::push rewrites it into block
form when the first item arrives; a [] that came from the source has
no such marker and keeps its flow style.
The marker lives only in the tree. Serializing and re-parsing yields
an ordinary [], which stays flow, since YAML has nowhere to record
the intent.
Sourcepub fn push(&self, value: impl AsYaml)
pub fn push(&self, value: impl AsYaml)
Add an item to the end of the sequence.
Mutates in place despite &self (see crate docs on interior mutability).
§Performance
Runs in time linear in the sequence’s current length, so building a sequence one push at a time is quadratic overall. The cost is rowan rebuilding the green node for every edit, which an immutable tree has to do; this method already avoids the avoidable part by reading only the tail of the child list and holding no child handles across the edit. As a rough guide, 1000 pushes take about 70ms and 8000 about 4.5s (release build).
For a large sequence, build it with
SequenceBuilder or parse the YAML text in
one go, rather than pushing in a loop: the builder is linear, and
assembles those 8000 entries in under 2ms.
insert and remove are likewise
linear per call, and rather more costly than push.
Sourcepub fn insert(&self, index: usize, value: impl AsYaml)
pub fn insert(&self, index: usize, value: impl AsYaml)
Insert an item at a specific position.
If index is out of bounds, the item is appended at the end.
This method always succeeds; it never returns an error.
Mutates in place despite &self (see crate docs on interior mutability).
Sourcepub fn set(&self, index: usize, value: impl AsYaml) -> bool
pub fn set(&self, index: usize, value: impl AsYaml) -> bool
Replace the item at index with a new value.
Returns true if the index was in bounds and the item was replaced,
false if index >= len().
Mutates in place despite &self (see crate docs on interior mutability).
Sourcepub fn remove(&self, index: usize) -> Option<YamlNode>
pub fn remove(&self, index: usize) -> Option<YamlNode>
Remove the item at index, returning its value.
Returns Some(value) if the index was in bounds, None otherwise.
Mutates in place despite &self (see crate docs on interior mutability).
Sourcepub fn is_flow_style(&self) -> bool
pub fn is_flow_style(&self) -> bool
Check if this sequence is in flow style [item1, item2]
Sourcepub fn pop(&self) -> Option<YamlNode>
pub fn pop(&self) -> Option<YamlNode>
Remove and return the last item in this sequence.
Returns None if the sequence is empty.
Mutates in place despite &self (see crate docs on interior mutability).
Sourcepub fn clear(&self)
pub fn clear(&self)
Remove all items from this sequence.
Mutates in place despite &self (see crate docs on interior mutability).
Sourcepub fn byte_range(&self) -> TextPosition
pub fn byte_range(&self) -> TextPosition
Get the byte offset range of this sequence in the source text.
Returns the start and end byte offsets as a TextPosition.
Sourcepub fn start_position(&self, source_text: &str) -> LineColumn
pub fn start_position(&self, source_text: &str) -> LineColumn
Get the line and column where this sequence starts.
Requires the original source text to calculate line/column from byte offsets. Line and column numbers are 1-indexed.
§Arguments
source_text- The original YAML source text
Sourcepub fn end_position(&self, source_text: &str) -> LineColumn
pub fn end_position(&self, source_text: &str) -> LineColumn
Get the line and column where this sequence ends.
Requires the original source text to calculate line/column from byte offsets. Line and column numbers are 1-indexed.
§Arguments
source_text- The original YAML source text
Trait Implementations§
Source§impl AsYaml for Sequence
impl AsYaml for Sequence
Source§fn as_node(&self) -> Option<&SyntaxNode<Lang>>
fn as_node(&self) -> Option<&SyntaxNode<Lang>>
SyntaxNode if one exists. Read moreSource§fn build_content(
&self,
builder: &mut GreenNodeBuilder<'_>,
indent: usize,
_flow_context: bool,
) -> bool
fn build_content( &self, builder: &mut GreenNodeBuilder<'_>, indent: usize, _flow_context: bool, ) -> bool
GreenNodeBuilder. Read more