diff --git a/Cargo.lock b/Cargo.lock index 2f60fee..aac2c86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,12 @@ version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "cfg-if" version = "1.0.0" @@ -151,6 +157,18 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "kdl" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a927c788db5413fbaced8fc5ac08a4a341c50766cba1d363ba3e25f3db343af0" +dependencies = [ + "miette", + "num", + "thiserror", + "winnow", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -169,6 +187,7 @@ version = "0.12.2" dependencies = [ "anyhow", "clap", + "kdl", "serde_json", "serde_yaml", "toml", @@ -191,6 +210,29 @@ version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +[[package]] +name = "miette" +version = "7.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317f146e2eb7021892722af37cf1b971f0a70c8406f487e24952667616192c64" +dependencies = [ + "cfg-if", + "miette-derive", + "thiserror", + "unicode-width", +] + +[[package]] +name = "miette-derive" +version = "7.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c9b935fbe1d6cbd1dac857b54a688145e2d93f48db36010514d0f612d0ad67" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -201,6 +243,79 @@ dependencies = [ "winapi", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -372,6 +487,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.7" @@ -483,6 +618,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -591,9 +732,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "winnow" -version = "0.6.5" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 1cff787..da637e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = "6.1.0" [profile.release] lto = true diff --git a/lq.rs b/lq.rs index f104cb1..82f3d89 100644 --- a/lq.rs +++ b/lq.rs @@ -12,6 +12,7 @@ enum Input { Yaml, Json, Toml, + Kdl, } #[derive(Copy, Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] @@ -187,6 +188,28 @@ impl Args { Ok(serde_json::to_vec(&doc_as)?) } + fn read_kdl(&mut self) -> Result> { + 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> { let json_value: serde_json::Value = if let Some(f) = &self.file { if !std::path::Path::new(&f).exists() { @@ -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));