Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a53ef65

Browse files
committedNov 5, 2024·
Kdl sketch
does not work because kdl does not serialize with serde Signed-off-by: clux <[email protected]>
1 parent 33eb219 commit a53ef65

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
 

‎Cargo.lock

+77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ toml = { version = "0.8.12", features = ["display"] }
2626
serde_yaml = "0.9.34"
2727
tracing = "0.1.40"
2828
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
29+
kdl = "4.6.0"
2930

3031
[profile.release]
3132
lto = true

‎lq.rs

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum Input {
1212
Yaml,
1313
Json,
1414
Toml,
15+
Kdl,
1516
}
1617

1718
#[derive(Copy, Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
@@ -187,6 +188,28 @@ impl Args {
187188
Ok(serde_json::to_vec(&doc_as)?)
188189
}
189190

191+
fn read_kdl(&mut self) -> Result<Vec<u8>> {
192+
use kdl::KdlDocument;
193+
let mut buf = String::new();
194+
let kdl_str = if let Some(f) = &self.file {
195+
if !std::path::Path::new(&f).exists() {
196+
Self::try_parse_from(["cmd", "-h"])?;
197+
std::process::exit(2);
198+
}
199+
std::fs::read_to_string(f)?
200+
} else if !stdin().is_terminal() && !cfg!(test) {
201+
debug!("reading from stdin");
202+
stdin().read_to_string(&mut buf)?;
203+
buf
204+
} else {
205+
Self::try_parse_from(["cmd", "-h"])?;
206+
std::process::exit(2);
207+
};
208+
let doc: KdlDocument = kdl_str.parse()?;
209+
let doc_as: serde_json::Value = doc.try_into()?;
210+
Ok(serde_json::to_vec(&doc_as)?)
211+
}
212+
190213
fn read_json(&mut self) -> Result<Vec<u8>> {
191214
let json_value: serde_json::Value = if let Some(f) = &self.file {
192215
if !std::path::Path::new(&f).exists() {
@@ -209,6 +232,7 @@ impl Args {
209232
let ser = match self.input {
210233
Input::Yaml => self.read_yaml()?,
211234
Input::Toml => self.read_toml()?,
235+
Input::Kdl => self.read_kdl()?,
212236
Input::Json => self.read_json()?,
213237
};
214238
debug!("input decoded as json: {}", String::from_utf8_lossy(&ser));

0 commit comments

Comments
 (0)
Please sign in to comment.