From ddd979631521a586c4e8b1df7bf8cef817921c98 Mon Sep 17 00:00:00 2001 From: Sachymetsu Date: Sun, 11 Jan 2026 10:56:32 +0100 Subject: [PATCH] Yeet facet AND kdl, return to toml --- Cargo.lock | 272 ++++++---------------------------------- sachy-config/Cargo.toml | 3 +- sachy-config/src/lib.rs | 172 +++++++++++++------------ 3 files changed, 127 insertions(+), 320 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 034eae0..c226e5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,116 +463,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "facet" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72ee49c69f8a398d01d9b160e3e6288c1a5f7d756e8377f0530bbb4019aa1616" -dependencies = [ - "autocfg", - "facet-core", - "facet-macros", - "static_assertions", -] - -[[package]] -name = "facet-core" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ca85b6f8c289d86e5a0daa6b402ed1edf4001ad9b6ead357cc047fff680e0d" -dependencies = [ - "autocfg", - "impls", -] - -[[package]] -name = "facet-kdl" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b52d9f35c93a85109d9d1d9042fbc1b02972c28eaceb36374995e9d166f4019" -dependencies = [ - "facet", - "facet-core", - "facet-reflect", - "facet-singularize", - "facet-solver", - "kdl", - "log", - "miette", -] - -[[package]] -name = "facet-macro-parse" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "294183c810413075f9c3f075c0b3554d04ad06207dad18debc649a48779321f6" -dependencies = [ - "facet-macro-types", - "proc-macro2", - "quote", -] - -[[package]] -name = "facet-macro-types" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8335dd3290eb5780aa40fb5b0da6c1a1c08980af6ada54c2e0d8cbbcd52b8f33" -dependencies = [ - "proc-macro2", - "quote", - "unsynn", -] - -[[package]] -name = "facet-macros" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54f7c8e20f24f6c933290da20e76ce8b62a28ea7f16ea173a1aa21cb2ebf61f0" -dependencies = [ - "facet-macros-impl", -] - -[[package]] -name = "facet-macros-impl" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc36ba0859bb5fc539e9fb9ed4dab7a5af3b9dbf080e92adaeb5041c58971fcb" -dependencies = [ - "facet-macro-parse", - "facet-macro-types", - "proc-macro2", - "quote", - "strsim", - "unsynn", -] - -[[package]] -name = "facet-reflect" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6ab47f7ccaed7b782b4cdbfa3482f16720c0e7e31c38bf5f7da8b8f8c988690" -dependencies = [ - "facet-core", - "miette", -] - -[[package]] -name = "facet-singularize" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd14cadea48902b862d1f9256f1eac9102680d3fc105e5888008e219f2de6023" - -[[package]] -name = "facet-solver" -version = "0.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a23bcde5d4f562dfed81ca31c15c241536b1b6ef0a6e46cc17d8963b9f9f33" -dependencies = [ - "facet-core", - "facet-reflect", - "strsim", -] - [[package]] name = "fastrand" version = "2.3.0" @@ -733,12 +623,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" -[[package]] -name = "impls" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a46645bbd70538861a90d0f26c31537cdf1e44aae99a794fb75a664b70951bc" - [[package]] name = "indexmap" version = "2.12.1" @@ -780,17 +664,6 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" -[[package]] -name = "kdl" -version = "6.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81a29e7b50079ff44549f68c0becb1c73d7f6de2a4ea952da77966daf3d4761e" -dependencies = [ - "miette", - "num", - "winnow 0.6.24", -] - [[package]] name = "libc" version = "0.2.179" @@ -929,12 +802,6 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" -[[package]] -name = "mutants" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc0287524726960e07b119cebd01678f852f147742ae0d925e6a520dca956126" - [[package]] name = "nb" version = "0.1.3" @@ -987,39 +854,6 @@ dependencies = [ "libc", ] -[[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-derive" version = "0.4.2" @@ -1031,26 +865,6 @@ dependencies = [ "syn", ] -[[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-modular" version = "0.6.1" @@ -1066,17 +880,6 @@ dependencies = [ "num-modular", ] -[[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" @@ -1351,12 +1154,6 @@ version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" -[[package]] -name = "rustc-hash" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" - [[package]] name = "rustix" version = "0.38.44" @@ -1400,9 +1197,8 @@ dependencies = [ name = "sachy-config" version = "0.1.0" dependencies = [ - "facet", - "facet-kdl", "miette", + "toml_edit", ] [[package]] @@ -1444,7 +1240,7 @@ dependencies = [ "defmt 1.0.1", "embassy-time", "sachy-fmt", - "winnow 0.7.14", + "winnow", ] [[package]] @@ -1583,12 +1379,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "strsim" version = "0.11.1" @@ -1709,6 +1499,43 @@ dependencies = [ "syn", ] +[[package]] +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.24.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c740b185920170a6d9191122cafef7010bd6270a3824594bff6784c04d7f09e" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_parser" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" + [[package]] name = "typenum" version = "1.19.0" @@ -1754,17 +1581,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" -[[package]] -name = "unsynn" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501a7adf1a4bd9951501e5c66621e972ef8874d787628b7f90e64f936ef7ec0a" -dependencies = [ - "mutants", - "proc-macro2", - "rustc-hash", -] - [[package]] name = "version_check" version = "0.9.5" @@ -1984,19 +1800,13 @@ checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" [[package]] name = "winnow" -version = "0.6.24" +version = "0.7.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8d71a593cc5c42ad7876e2c1fda56f314f3754c084128833e64f1345ff8a03a" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" dependencies = [ "memchr", ] -[[package]] -name = "winnow" -version = "0.7.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" - [[package]] name = "wit-bindgen" version = "0.46.0" diff --git a/sachy-config/Cargo.toml b/sachy-config/Cargo.toml index 26a938c..9c5c352 100644 --- a/sachy-config/Cargo.toml +++ b/sachy-config/Cargo.toml @@ -8,6 +8,5 @@ version.workspace = true rust-version.workspace = true [dependencies] -facet = "0.34" -facet-kdl = "0.34" +toml_edit = "0.24.0" miette = { version = "7", features = ["fancy"] } diff --git a/sachy-config/src/lib.rs b/sachy-config/src/lib.rs index 361ddd7..b5f415f 100644 --- a/sachy-config/src/lib.rs +++ b/sachy-config/src/lib.rs @@ -1,81 +1,73 @@ use std::io::Write; -use facet::Facet; -use facet_kdl as kdl; -use miette::{IntoDiagnostic, Result}; - -#[derive(Facet, Default)] -#[facet(default)] -pub struct ConfigDoc { - #[facet(kdl::child, default)] - pub constants: Constants, - #[facet(kdl::child, default)] - pub statics: Statics, -} - -#[derive(Facet, Default)] -#[facet(default)] -pub struct Statics { - #[facet(kdl::children, default)] - statics: Vec, -} +use miette::{IntoDiagnostic, Result, miette}; -#[derive(Facet, Default)] -#[facet(default)] -pub struct Constants { - #[facet(kdl::children, default)] - pub constants: Vec, -} +pub fn output_config(config: &str, mut writer: W) -> Result<()> { + let output = parse_config(config)?; -#[derive(Facet)] -pub struct Static { - #[facet(kdl::node_name)] - pub name: String, - #[facet(kdl::argument)] - pub value: String, -} + writer.write_all(output.as_bytes()).into_diagnostic()?; -#[derive(Facet)] -pub struct Constant { - #[facet(kdl::node_name)] - pub name: String, - #[facet(kdl::argument)] - pub kind: String, - #[facet(kdl::argument)] - pub value: String, + Ok(()) } pub fn parse_config(config: &str) -> Result { - let config_doc: ConfigDoc = kdl::from_str_owned(config)?; + let config_doc = config.parse::().into_diagnostic()?; let mut output = String::new(); - for static_value in config_doc.statics.statics.iter() { - let static_output = format!( - "pub static {}: &str = \"{}\";\n", - &static_value.name, &static_value.value - ); - output.push_str(&static_output); - } - - for constant_value in config_doc.constants.constants.iter() { - let constant_output = format!( - "pub const {}: {} = {};\n", - &constant_value.name, &constant_value.kind, &constant_value.value, - ); - output.push_str(&constant_output); - } + config_doc + .get("statics") + .and_then(|item| item.as_table()) + .into_iter() + .flat_map(|statics| statics.iter()) + .try_for_each(|(name, item)| -> miette::Result<()> { + let value = item.as_str().ok_or_else(|| { + miette!( + "{} is not a string value. Actual type: {}", + name, + item.type_name(), + ) + })?; + let static_output = format!("pub static {}: &str = \"{}\";\n", name, value); + output.push_str(&static_output); + Ok(()) + })?; + + config_doc + .get("constants") + .and_then(|item| item.as_table()) + .into_iter() + .flat_map(|constants| constants.iter()) + .try_for_each(|(name, constant_value)| -> miette::Result<()> { + let kind = &constant_value["type"]; + let kind = kind.as_str().ok_or_else(|| { + miette!( + "{} is not a string value. Actual type: {}", + name, + kind.type_name(), + ) + })?; + let value = &constant_value["value"]; + let value = value.as_value().ok_or_else(|| { + miette!( + "{} is not a value. Actual type: {}", + name, + value.type_name(), + ) + })?; + + let constant_output = format!( + "pub const {}: {} = {};\n", + name, + kind, + value.clone().decorated("", "") + ); + output.push_str(&constant_output); + Ok(()) + })?; Ok(output) } -pub fn output_config(config: &str, mut writer: W) -> Result<()> { - let output = parse_config(config)?; - - writer.write_all(output.as_bytes()).into_diagnostic()?; - - Ok(()) -} - #[cfg(test)] mod tests { use std::io::BufWriter; @@ -84,16 +76,15 @@ mod tests { #[test] fn it_parses_config() -> Result<()> { - let input = r#"constants { - FIRST "u8" "2" - VALUE "u64" "42" -} -statics { - EXAMPLE "thing" -}"#; + let input = r#"[constants] +FIRST = { type = "u8", value = 2 } +VALUE = { type = "f32", value = 42.0 } +[statics] +EXAMPLE = "thing" +"#; let expected = r#"pub static EXAMPLE: &str = "thing"; pub const FIRST: u8 = 2; -pub const VALUE: u64 = 42; +pub const VALUE: f32 = 42.0; "#; let output = parse_config(input)?; @@ -105,9 +96,9 @@ pub const VALUE: u64 = 42; #[test] fn it_handles_only_statics() -> Result<()> { - let input = r#"statics { - EXAMPLE "thing" -}"#; + let input = r#"[statics] +EXAMPLE = "thing" +"#; let expected = "pub static EXAMPLE: &str = \"thing\";\n"; @@ -120,9 +111,9 @@ pub const VALUE: u64 = 42; #[test] fn it_handles_only_constants() -> Result<()> { - let input = r#"constants { - FIRST "u8" "42" -}"#; + let input = r#"[constants] + FIRST = { type = "u8", value = 42 } +"#; let expected = "pub const FIRST: u8 = 42;\n"; @@ -135,8 +126,16 @@ pub const VALUE: u64 = 42; #[test] fn it_handles_empty_config() -> Result<()> { - let input = r#"statics {} -constants {}"#; + let input = r#"[statics] +[constants]"#; + + let expected = ""; + + let output = parse_config(input)?; + + assert_eq!(&output, expected); + + let input = ""; let expected = ""; @@ -149,13 +148,12 @@ constants {}"#; #[test] fn it_outputs_to_file() -> Result<()> { - let input = r#"constants { - FIRST "u8" "2" - VALUE "u64" "42" -} -statics { - EXAMPLE "thing" -}"#; + let input = r#"[constants] +FIRST = { type = "u8", value = 2 } +VALUE = { type = "u64", value = 42 } +[statics] +EXAMPLE = "thing" +"#; let expected = r#"pub static EXAMPLE: &str = "thing"; pub const FIRST: u8 = 2; pub const VALUE: u64 = 42; -- 2.51.2