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 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 documents(&self) -> impl Iterator<Item = Document>

Get all documents in this YAML file

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 document has no mapping yet, one is created automatically. This method always succeeds; it never returns an error.

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 · 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 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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl YamlAccept for YamlFile

Source§

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

Accept a visitor for traversal
Source§

impl Eq for YamlFile

Source§

impl StructuralPartialEq for YamlFile

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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.