Skip to main content

YamlFile

Struct YamlFile 

Source
pub struct YamlFile(/* private fields */);
Expand description

A YAML file containing one or more documents

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 YamlFile

Source

pub fn snapshot(&self) -> Self

Capture an independent snapshot of this YAML file.

The returned value shares the underlying immutable green-node data with self at the time of the call, but lives in its own mutable tree: subsequent mutations to self do not propagate to the snapshot. Pair with Self::tree_eq to detect later mutations.

Source

pub fn tree_eq(&self, other: &Self) -> bool

Returns true iff the syntax trees of self and other are value-equal. An O(1) pointer-identity fast path makes this free for trees that still share state with a recent snapshot().

Source§

impl YamlFile

Source

pub fn new() -> YamlFile

Create a new empty YAML document.

Source

pub fn parse(text: &str) -> Parse<YamlFile>

Parse YAML text, returning a Parse result

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<YamlFile, YamlError>

Parse YAML from a file path

Source

pub fn from_str_relaxed(s: &str) -> (YamlFile, Vec<String>)

Parse YAML text, allowing syntax errors.

Returns the parsed tree even if there are parse errors, along with a list of error messages. This allows for error-resilient tooling that can work with partial or invalid input.

§Example
use yaml_edit::YamlFile;

let (yaml_file, errors) = YamlFile::from_str_relaxed("key: [unclosed");
// Tree is usable even with errors
assert!(!errors.is_empty());
assert!(yaml_file.document().is_some());
Source

pub fn from_path_relaxed<P: AsRef<Path>>( path: P, ) -> Result<(YamlFile, Vec<String>), Error>

Parse YAML from a file path, allowing syntax errors.

Returns the parsed tree even if there are parse errors, along with a list of error messages. I/O errors are still returned as Err.

Source

pub fn read_relaxed<R: Read>(r: R) -> Result<(YamlFile, Vec<String>), Error>

Read YAML from a Read object, allowing syntax errors.

Returns the parsed tree even if there are parse errors, along with a list of error messages. I/O errors are still returned as Err.

Source

pub fn documents(&self) -> impl Iterator<Item = Document>

Get all documents in this YAML file

Source

pub fn comments(&self) -> impl Iterator<Item = Comment>

Iterate over the comments in this file, in source order.

This includes comments inside documents as well as any comments between or outside documents.

§Example
use yaml_edit::YamlFile;
use std::str::FromStr;

let file = YamlFile::from_str("# header\nkey: value # trailing\n").unwrap();
let comments: Vec<_> = file.comments().map(|c| c.content().to_string()).collect();
assert_eq!(comments, vec!["header", "trailing"]);
Source

pub fn document(&self) -> Option<Document>

Get the first document in this YAML file, or None if there are none.

Most YAML files have exactly one document. Use documents to iterate over all documents in a multi-document file.

Source

pub fn ensure_document(&self) -> Document

Ensure this YamlFile contains at least one document, creating an empty mapping document if needed.

Returns the first document.

Source

pub fn directives(&self) -> impl Iterator<Item = Directive>

Iterate over all YAML directives (e.g. %YAML 1.2) in this file.

Source

pub fn add_directive(&self, directive_text: &str)

Prepend a YAML directive to this file.

directive_text should be the full directive line without a trailing newline, e.g. "%YAML 1.2" or "%TAG ! tag:example.com,2000:app/". The directive is inserted before all existing content.

Note: the parser does not currently enforce that directives appear before any document node; callers are responsible for ordering.

Source

pub fn push_document(&self, document: Document)

Add a new document to the end of this YAML file

Source

pub fn set(&self, key: impl AsYaml, value: impl AsYaml)

Set a key-value pair in the first document’s mapping.

If the key exists its value is replaced; if not, a new entry is appended. Does nothing if the YamlFile contains no documents or the first document is not a mapping. Use ensure_document first if you need to guarantee a document exists.

Mutates in place despite &self (see crate docs on interior mutability).

Source

pub fn insert_after( &self, after_key: impl AsYaml, key: impl AsYaml, value: impl AsYaml, ) -> bool

Insert a key-value pair immediately after after_key in the first document.

Delegates to Document::insert_after, which in turn calls Mapping::insert_after. If key already exists it is updated in-place rather than moved. Returns false if after_key is not found or the document is not a mapping.

Mutates in place despite &self (see crate docs on interior mutability).

Source

pub fn insert_before( &self, before_key: impl AsYaml, key: impl AsYaml, value: impl AsYaml, ) -> bool

Insert a key-value pair immediately before before_key in the first document.

Delegates to Document::insert_before, which in turn calls Mapping::insert_before. If key already exists it is updated in-place rather than moved. Returns false if before_key is not found or the document is not a mapping.

Mutates in place despite &self (see crate docs on interior mutability).

Source

pub fn move_after( &self, after_key: impl AsYaml, key: impl AsYaml, value: impl AsYaml, ) -> bool

Move a key-value pair to immediately after after_key in the first document.

Delegates to Document::move_after. If key already exists it is removed from its current position and re-inserted after after_key. Returns false if after_key is not found or the document is not a mapping.

Use insert_after if you want an existing entry to be updated in-place rather than moved.

Mutates in place despite &self (see crate docs on interior mutability).

Source

pub fn move_before( &self, before_key: impl AsYaml, key: impl AsYaml, value: impl AsYaml, ) -> bool

Move a key-value pair to immediately before before_key in the first document.

Delegates to Document::move_before. If key already exists it is removed from its current position and re-inserted before before_key. Returns false if before_key is not found or the document is not a mapping.

Use insert_before if you want an existing entry to be updated in-place rather than moved.

Mutates in place despite &self (see crate docs on interior mutability).

Source

pub fn insert_at_index( &self, index: usize, key: impl AsYaml, value: impl AsYaml, )

Insert a key-value pair at a specific index (0-based) in the first document.

Delegates to Document::insert_at_index. If key already exists it is updated in-place rather than moved. If index is out of bounds the entry is appended at the end. If the first document has no root node, a mapping is created. A sequence or other non-mapping root is left unchanged.

Mutates in place despite &self (see crate docs on interior mutability).

Trait Implementations§

Source§

impl AstNode for YamlFile

Source§

type Language = Lang

Source§

fn can_cast(kind: SyntaxKind) -> bool

Source§

fn cast(syntax: SyntaxNode<Lang>) -> Option<Self>

Source§

fn syntax(&self) -> &SyntaxNode<Lang>

Source§

fn clone_for_update(&self) -> Self
where Self: Sized,

Source§

fn clone_subtree(&self) -> Self
where Self: Sized,

Source§

impl Clone for YamlFile

Source§

fn clone(&self) -> YamlFile

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 Debug for YamlFile

Source§

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

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

impl Default for YamlFile

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for YamlFile

Source§

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

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

impl Eq for YamlFile

Source§

impl FromStr for YamlFile

Source§

type Err = YamlError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for YamlFile

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for YamlFile

Source§

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

Source§

impl YamlAccept for YamlFile

Source§

fn accept<V: YamlVisitor>(&self, visitor: &mut V)

Accept a visitor for traversal

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.