feat(len): add APIs to calculate component lengths (#36)

Fixes: https://github.com/kdl-org/kdl-rs/issues/19
This commit is contained in:
Kat Marchán 2022-04-23 02:15:31 -07:00 committed by GitHub
parent 40b04418c9
commit 177c42cae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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.

View File

@ -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.