mirror of https://github.com/kdl-org/kdl-rs.git
feat(len): add APIs to calculate component lengths (#36)
Fixes: https://github.com/kdl-org/kdl-rs/issues/19
This commit is contained in:
parent
40b04418c9
commit
177c42cae7
|
|
@ -168,6 +168,16 @@ impl KdlDocument {
|
|||
self.trailing = Some(trailing.into());
|
||||
}
|
||||
|
||||
/// Length of this document when rendered as a string.
|
||||
pub fn len(&self) -> usize {
|
||||
format!("{}", self).len()
|
||||
}
|
||||
|
||||
/// Returns true if this document is completely empty (including whitespace)
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Clears leading and trailing text (whitespace, comments). `KdlNode`s in
|
||||
/// this document will be unaffected.
|
||||
pub fn clear_fmt(&mut self) {
|
||||
|
|
|
|||
10
src/entry.rs
10
src/entry.rs
|
|
@ -98,6 +98,16 @@ impl KdlEntry {
|
|||
self.value_repr = Some(repr.into());
|
||||
}
|
||||
|
||||
/// Length of this entry when rendered as a string.
|
||||
pub fn len(&self) -> usize {
|
||||
format!("{}", self).len()
|
||||
}
|
||||
|
||||
/// Returns true if this entry is completely empty (including whitespace).
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Auto-formats this entry.
|
||||
pub fn fmt(&mut self) {
|
||||
self.leading = None;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,16 @@ impl KdlIdentifier {
|
|||
self.repr = Some(repr.into());
|
||||
}
|
||||
|
||||
/// Length of this identifier when rendered as a string.
|
||||
pub fn len(&self) -> usize {
|
||||
format!("{}", self).len()
|
||||
}
|
||||
|
||||
/// Returns true if this identifier is completely empty.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Resets this identifier to its default representation. It will attempt
|
||||
/// to make it an unquoted identifier, and fall back to a string
|
||||
/// representation if that would be invalid.
|
||||
|
|
|
|||
10
src/node.rs
10
src/node.rs
|
|
@ -106,6 +106,16 @@ impl KdlNode {
|
|||
self.trailing = Some(trailing.into());
|
||||
}
|
||||
|
||||
/// Length of this node when rendered as a string.
|
||||
pub fn len(&self) -> usize {
|
||||
format!("{}", self).len()
|
||||
}
|
||||
|
||||
/// Returns true if this node is completely empty (including whitespace).
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Clears leading and trailing text (whitespace, comments), as well as
|
||||
/// the space before the children block, if any. Individual entries and
|
||||
/// their formatting will be preserved.
|
||||
|
|
|
|||
Loading…
Reference in New Issue