Skip to content

Commit 683e87a

Browse files
authored
feat(api): update the KdlNode and KdlDocument APIs to be more Vec-like (#101)
Fixes: #81
1 parent 4734b06 commit 683e87a

File tree

3 files changed

+329
-174
lines changed

3 files changed

+329
-174
lines changed

src/document.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl KdlDocument {
108108
self.get(name).and_then(|node| node.get(0))
109109
}
110110

111-
/// Gets the all node arguments (value) of the first child node with a
111+
/// Returns an iterator of the all node arguments (value) of the first child node with a
112112
/// matching name. This is a shorthand utility for cases where a document
113113
/// is being used as a key/value store and the value is expected to be
114114
/// array-ish.
@@ -127,16 +127,18 @@ impl KdlDocument {
127127
/// ```rust
128128
/// # use kdl::{KdlDocument, KdlValue};
129129
/// # let doc: KdlDocument = "foo 1 2 3\nbar #false".parse().unwrap();
130-
/// assert_eq!(doc.get_args("foo"), vec![&1.into(), &2.into(), &3.into()]);
130+
/// assert_eq!(
131+
/// doc.iter_args("foo").collect::<Vec<&KdlValue>>(),
132+
/// vec![&1.into(), &2.into(), &3.into()]
133+
/// );
131134
/// ```
132-
pub fn get_args(&self, name: &str) -> Vec<&KdlValue> {
135+
pub fn iter_args(&self, name: &str) -> impl Iterator<Item = &KdlValue> {
133136
self.get(name)
134137
.map(|n| n.entries())
135138
.unwrap_or_default()
136139
.iter()
137140
.filter(|e| e.name().is_none())
138141
.map(|e| e.value())
139-
.collect()
140142
}
141143

142144
/// Gets a mutable reference to the first argument (value) of the first
@@ -164,17 +166,19 @@ impl KdlDocument {
164166
/// ```rust
165167
/// # use kdl::{KdlDocument, KdlValue};
166168
/// # let doc: KdlDocument = "foo {\n - 1\n - 2\n - #false\n}".parse().unwrap();
167-
/// assert_eq!(doc.get_dash_args("foo"), vec![&1.into(), &2.into(), &false.into()]);
169+
/// assert_eq!(
170+
/// doc.iter_dash_args("foo").collect::<Vec<&KdlValue>>(),
171+
/// vec![&1.into(), &2.into(), &false.into()]
172+
/// );
168173
/// ```
169-
pub fn get_dash_args(&self, name: &str) -> Vec<&KdlValue> {
174+
pub fn iter_dash_args(&self, name: &str) -> impl Iterator<Item = &KdlValue> {
170175
self.get(name)
171176
.and_then(|n| n.children())
172177
.map(|doc| doc.nodes())
173178
.unwrap_or_default()
174179
.iter()
175180
.filter(|e| e.name().value() == "-")
176181
.filter_map(|e| e.get(0))
177-
.collect()
178182
}
179183

180184
/// Returns a reference to this document's child nodes.
@@ -441,8 +445,8 @@ second_node /* This time, the comment is here */ param=153 {
441445
/* block comment */
442446
inline /*comment*/ here
443447
another /-comment there
444-
445-
448+
449+
446450
after some whitespace
447451
trailing /* multiline */
448452
trailing // single line
@@ -480,7 +484,7 @@ final;";
480484

481485
assert_eq!(doc.get_arg("foo"), Some(&1.into()));
482486
assert_eq!(
483-
doc.get_dash_args("foo"),
487+
doc.iter_dash_args("foo").collect::<Vec<&KdlValue>>(),
484488
vec![&1.into(), &2.into(), &"three".into()]
485489
);
486490
assert_eq!(

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! ## Example
2020
//!
2121
//! ```rust
22-
//! use kdl::KdlDocument;
22+
//! use kdl::{KdlDocument, KdlValue};
2323
//!
2424
//! let doc_str = r#"
2525
//! hello 1 2 3
@@ -35,7 +35,7 @@
3535
//! let doc: KdlDocument = doc_str.parse().expect("failed to parse KDL");
3636
//!
3737
//! assert_eq!(
38-
//! doc.get_args("hello"),
38+
//! doc.iter_args("hello").collect::<Vec<&KdlValue>>(),
3939
//! vec![&1.into(), &2.into(), &3.into()]
4040
//! );
4141
//!

0 commit comments

Comments
 (0)