Skip to content

Commit 177c42c

Browse files
authored
feat(len): add APIs to calculate component lengths (#36)
Fixes: #19
1 parent 40b0441 commit 177c42c

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/document.rs

+10
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ impl KdlDocument {
168168
self.trailing = Some(trailing.into());
169169
}
170170

171+
/// Length of this document when rendered as a string.
172+
pub fn len(&self) -> usize {
173+
format!("{}", self).len()
174+
}
175+
176+
/// Returns true if this document is completely empty (including whitespace)
177+
pub fn is_empty(&self) -> bool {
178+
self.len() == 0
179+
}
180+
171181
/// Clears leading and trailing text (whitespace, comments). `KdlNode`s in
172182
/// this document will be unaffected.
173183
pub fn clear_fmt(&mut self) {

src/entry.rs

+10
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ impl KdlEntry {
9898
self.value_repr = Some(repr.into());
9999
}
100100

101+
/// Length of this entry when rendered as a string.
102+
pub fn len(&self) -> usize {
103+
format!("{}", self).len()
104+
}
105+
106+
/// Returns true if this entry is completely empty (including whitespace).
107+
pub fn is_empty(&self) -> bool {
108+
self.len() == 0
109+
}
110+
101111
/// Auto-formats this entry.
102112
pub fn fmt(&mut self) {
103113
self.leading = None;

src/identifier.rs

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ impl KdlIdentifier {
3131
self.repr = Some(repr.into());
3232
}
3333

34+
/// Length of this identifier when rendered as a string.
35+
pub fn len(&self) -> usize {
36+
format!("{}", self).len()
37+
}
38+
39+
/// Returns true if this identifier is completely empty.
40+
pub fn is_empty(&self) -> bool {
41+
self.len() == 0
42+
}
43+
3444
/// Resets this identifier to its default representation. It will attempt
3545
/// to make it an unquoted identifier, and fall back to a string
3646
/// representation if that would be invalid.

src/node.rs

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ impl KdlNode {
106106
self.trailing = Some(trailing.into());
107107
}
108108

109+
/// Length of this node when rendered as a string.
110+
pub fn len(&self) -> usize {
111+
format!("{}", self).len()
112+
}
113+
114+
/// Returns true if this node is completely empty (including whitespace).
115+
pub fn is_empty(&self) -> bool {
116+
self.len() == 0
117+
}
118+
109119
/// Clears leading and trailing text (whitespace, comments), as well as
110120
/// the space before the children block, if any. Individual entries and
111121
/// their formatting will be preserved.

0 commit comments

Comments
 (0)