Struct kdl::KdlDocument
source · [−]pub struct KdlDocument { /* private fields */ }
Expand description
Represents a KDL
Document
.
This type is also used to manage a KdlNode
’s Children Block
,
when present.
Examples
The easiest way to create a KdlDocument
is to parse it:
let kdl: KdlDocument = "foo 1 2 3\nbar 4 5 6".parse().expect("parse failed");
Implementations
sourceimpl KdlDocument
impl KdlDocument
sourcepub fn span(&self) -> &SourceSpan
pub fn span(&self) -> &SourceSpan
Gets this document’s span.
This value will be properly initialized when created via [KdlDocument::parse
]
but may become invalidated if the document is mutated. We do not currently
guarantee this to yield any particularly consistent results at that point.
sourcepub fn span_mut(&mut self) -> &mut SourceSpan
pub fn span_mut(&mut self) -> &mut SourceSpan
Gets a mutable reference to this document’s span.
sourcepub fn set_span(&mut self, span: impl Into<SourceSpan>)
pub fn set_span(&mut self, span: impl Into<SourceSpan>)
Sets this document’s span.
sourcepub fn get(&self, name: &str) -> Option<&KdlNode>
pub fn get(&self, name: &str) -> Option<&KdlNode>
Gets the first child node with a matching name.
sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut KdlNode>
pub fn get_mut(&mut self, name: &str) -> Option<&mut KdlNode>
Gets a reference to the first child node with a matching name.
sourcepub fn get_arg(&self, name: &str) -> Option<&KdlValue>
pub fn get_arg(&self, name: &str) -> Option<&KdlValue>
Gets the first argument (value) of the first child node with a matching name. This is a shorthand utility for cases where a document is being used as a key/value store.
Examples
Given a document like this:
foo 1
bar false
You can fetch the value of foo
in a single call like this:
assert_eq!(doc.get_arg("foo"), Some(&1.into()));
sourcepub fn get_args(&self, name: &str) -> Vec<&KdlValue>
pub fn get_args(&self, name: &str) -> Vec<&KdlValue>
Gets the all node arguments (value) of the first child node with a matching name. This is a shorthand utility for cases where a document is being used as a key/value store and the value is expected to be array-ish.
If a node has no arguments, this will return an empty array.
Examples
Given a document like this:
foo 1 2 3
bar false
You can fetch the arguments for foo
in a single call like this:
assert_eq!(doc.get_args("foo"), vec![&1.into(), &2.into(), &3.into()]);
sourcepub fn get_arg_mut(&mut self, name: &str) -> Option<&mut KdlValue>
pub fn get_arg_mut(&mut self, name: &str) -> Option<&mut KdlValue>
Gets a mutable reference to the first argument (value) of the first child node with a matching name. This is a shorthand utility for cases where a document is being used as a key/value store.
sourcepub fn get_dash_vals(&self, name: &str) -> Vec<&KdlValue>
pub fn get_dash_vals(&self, name: &str) -> Vec<&KdlValue>
This utility makes it easy to interact with a KDL convention where
child nodes named -
are treated as array-ish values.
Examples
Given a document like this:
foo {
- 1
- 2
- false
}
You can fetch the dashed child values of foo
in a single call like this:
assert_eq!(doc.get_dash_vals("foo"), vec![&1.into(), &2.into(), &false.into()]);
sourcepub fn nodes_mut(&mut self) -> &mut Vec<KdlNode>
pub fn nodes_mut(&mut self) -> &mut Vec<KdlNode>
Returns a mutable reference to this document’s child nodes.
sourcepub fn leading(&self) -> Option<&str>
pub fn leading(&self) -> Option<&str>
Gets leading text (whitespace, comments) for this KdlDocument.
sourcepub fn set_leading(&mut self, leading: impl Into<String>)
pub fn set_leading(&mut self, leading: impl Into<String>)
Sets leading text (whitespace, comments) for this KdlDocument.
sourcepub fn trailing(&self) -> Option<&str>
pub fn trailing(&self) -> Option<&str>
Gets trailing text (whitespace, comments) for this KdlDocument.
sourcepub fn set_trailing(&mut self, trailing: impl Into<String>)
pub fn set_trailing(&mut self, trailing: impl Into<String>)
Sets trailing text (whitespace, comments) for this KdlDocument.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this document is completely empty (including whitespace)
sourcepub fn clear_fmt(&mut self)
pub fn clear_fmt(&mut self)
Clears leading and trailing text (whitespace, comments). KdlNode
s in
this document will be unaffected.
If you need to clear the KdlNode
s, use Self::clear_fmt_recursive
.
sourcepub fn clear_fmt_recursive(&mut self)
pub fn clear_fmt_recursive(&mut self)
Clears leading and trailing text (whitespace, comments), also clearing
all the KdlNode
s in the document.
sourcepub fn fmt(&mut self)
pub fn fmt(&mut self)
Auto-formats this Document, making everything nice while preserving comments.
sourcepub fn fmt_no_comments(&mut self)
pub fn fmt_no_comments(&mut self)
Formats the document and removes all comments from the document.
Trait Implementations
sourceimpl Clone for KdlDocument
impl Clone for KdlDocument
sourcefn clone(&self) -> KdlDocument
fn clone(&self) -> KdlDocument
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more