Skip to content

Commit

Permalink
Kdl sketch
Browse files Browse the repository at this point in the history
does not work because kdl does not serialize with serde

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Nov 5, 2024
1 parent 33eb219 commit a53ef65
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ toml = { version = "0.8.12", features = ["display"] }
serde_yaml = "0.9.34"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
kdl = "4.6.0"

[profile.release]
lto = true
Expand Down
24 changes: 24 additions & 0 deletions lq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum Input {
Yaml,
Json,
Toml,
Kdl,
}

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

fn read_kdl(&mut self) -> Result<Vec<u8>> {
use kdl::KdlDocument;
let mut buf = String::new();
let kdl_str = if let Some(f) = &self.file {
if !std::path::Path::new(&f).exists() {
Self::try_parse_from(["cmd", "-h"])?;
std::process::exit(2);
}
std::fs::read_to_string(f)?
} else if !stdin().is_terminal() && !cfg!(test) {
debug!("reading from stdin");
stdin().read_to_string(&mut buf)?;
buf
} else {
Self::try_parse_from(["cmd", "-h"])?;
std::process::exit(2);
};
let doc: KdlDocument = kdl_str.parse()?;
let doc_as: serde_json::Value = doc.try_into()?;
Ok(serde_json::to_vec(&doc_as)?)
}

fn read_json(&mut self) -> Result<Vec<u8>> {
let json_value: serde_json::Value = if let Some(f) = &self.file {
if !std::path::Path::new(&f).exists() {
Expand All @@ -209,6 +232,7 @@ impl Args {
let ser = match self.input {
Input::Yaml => self.read_yaml()?,
Input::Toml => self.read_toml()?,
Input::Kdl => self.read_kdl()?,
Input::Json => self.read_json()?,
};
debug!("input decoded as json: {}", String::from_utf8_lossy(&ser));
Expand Down

0 comments on commit a53ef65

Please sign in to comment.