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
impl YamlFile
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<YamlFile, YamlError>
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<YamlFile, YamlError>
Parse YAML from a file path
Sourcepub fn document(&self) -> Option<Document>
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.
Sourcepub fn ensure_document(&self) -> Document
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.
Sourcepub fn directives(&self) -> impl Iterator<Item = Directive>
pub fn directives(&self) -> impl Iterator<Item = Directive>
Iterate over all YAML directives (e.g. %YAML 1.2) in this file.
Sourcepub fn add_directive(&self, directive_text: &str)
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.
Sourcepub fn push_document(&self, document: Document)
pub fn push_document(&self, document: Document)
Add a new document to the end of this YAML file
Sourcepub fn set(&self, key: impl AsYaml, value: impl AsYaml)
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).
Sourcepub fn insert_after(
&self,
after_key: impl AsYaml,
key: impl AsYaml,
value: impl AsYaml,
) -> bool
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).
Sourcepub fn insert_before(
&self,
before_key: impl AsYaml,
key: impl AsYaml,
value: impl AsYaml,
) -> bool
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).
Sourcepub fn move_after(
&self,
after_key: impl AsYaml,
key: impl AsYaml,
value: impl AsYaml,
) -> bool
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).
Sourcepub fn move_before(
&self,
before_key: impl AsYaml,
key: impl AsYaml,
value: impl AsYaml,
) -> bool
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).
Sourcepub fn insert_at_index(
&self,
index: usize,
key: impl AsYaml,
value: impl AsYaml,
)
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).