From d6bec193caa1c2ea8cf9d24b309ff06c1a0ca746 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 17:16:14 +0100 Subject: [PATCH 1/9] feat: rename config settings & use crates.io config to load from both file & env (prefixed with CL) --- Cargo.lock | 460 +++++++++++++++++++++++++++++++++++++++++++- cli/Cargo.toml | 4 +- cli/src/config.rs | 142 -------------- cli/src/main.rs | 8 +- cli/src/message.rs | 4 +- cli/src/settings.rs | 57 ++++++ 6 files changed, 525 insertions(+), 150 deletions(-) delete mode 100644 cli/src/config.rs create mode 100644 cli/src/settings.rs diff --git a/Cargo.lock b/Cargo.lock index 8a6fbea..07dc3fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,18 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -26,6 +38,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anstream" version = "0.6.18" @@ -75,6 +93,23 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "arraydeque" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" + +[[package]] +name = "async-trait" +version = "0.1.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -96,11 +131,29 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +dependencies = [ + "serde", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] [[package]] name = "bytes" @@ -165,6 +218,7 @@ name = "commitlint-rs" version = "0.2.0" dependencies = [ "clap", + "config", "futures", "insta", "paste", @@ -176,6 +230,25 @@ dependencies = [ "tokio", ] +[[package]] +name = "config" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68578f196d2a33ff61b27fae256c3164f65e36382648e30666dde05b8cc9dfdf" +dependencies = [ + "async-trait", + "convert_case", + "json5", + "nom", + "pathdiff", + "ron", + "rust-ini", + "serde", + "serde_json", + "toml", + "yaml-rust2", +] + [[package]] name = "console" version = "0.15.8" @@ -188,6 +261,79 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "const-random" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom", + "once_cell", + "tiny-keccak", +] + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "cpufeatures" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" +dependencies = [ + "libc", +] + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "dlv-list" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] + [[package]] name = "dyn-clone" version = "1.0.17" @@ -200,6 +346,15 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -295,18 +450,58 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", +] + [[package]] name = "hashbrown" version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +[[package]] +name = "hashlink" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" +dependencies = [ + "hashbrown 0.14.5", +] + [[package]] name = "heck" version = "0.5.0" @@ -326,7 +521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.1", ] [[package]] @@ -354,6 +549,17 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -388,6 +594,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.0" @@ -409,6 +621,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "object" version = "0.36.5" @@ -418,6 +640,22 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "ordered-multimap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" +dependencies = [ + "dlv-list", + "hashbrown 0.14.5", +] + [[package]] name = "parking_lot" version = "0.12.3" @@ -446,6 +684,57 @@ name = "paste" version = "1.0.15" source = "git+https://github.com/elcoosp/paste?branch=feat/dash#4efa1dd157f1a9bc8994fffd653bb2e1c744828a" +[[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + +[[package]] +name = "pest" +version = "2.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879952a81a83930934cbf1786752d6dedc3b1f29e8f8fb2ad1d0a36f377cf442" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d214365f632b123a47fd913301e14c946c61d1c183ee245fa76eb752e59a02dd" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb55586734301717aea2ac313f50b2eb8f60d2fc3dc01d190eefa2e625f60c4e" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b75da2a70cf4d9cb76833c990ac9cd3923c9a8905a8929789ce347c84564d03d" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "pin-project-lite" version = "0.2.15" @@ -534,6 +823,28 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "ron" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" +dependencies = [ + "base64", + "bitflags", + "serde", + "serde_derive", +] + +[[package]] +name = "rust-ini" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0698206bcb8882bf2a9ecb4c1e7785db57ff052297085a6efd4fe42302068a" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -620,6 +931,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" @@ -633,6 +953,17 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -690,6 +1021,35 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tokio" version = "1.41.0" @@ -719,12 +1079,64 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "ucd-trie" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" + [[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -737,6 +1149,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -824,3 +1242,43 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" +dependencies = [ + "memchr", +] + +[[package]] +name = "yaml-rust2" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8902160c4e6f2fb145dbe9d6760a75e3c9522d8bf796ed7047c85919ac7115f8" +dependencies = [ + "arraydeque", + "encoding_rs", + "hashlink", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 69dc990..c7f20be 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -18,6 +18,7 @@ clap = { version = "4.5.4", features = ["derive", "env", "string"] } futures = "0.3.30" regex = "1.10.5" schemars = { version = "1.0.0-alpha.16", optional = true } +config = { version = "0.14.1", optional = true } serde = { version = "1.0.201", features = ["derive"] } serde_json = "1.0.121" serde_yaml = "0.9.34" @@ -32,7 +33,8 @@ path = "src/main.rs" pkg-url = "{ repo }/releases/download/v{ version }/commitlint-v{ version }-{ target }{ archive-suffix }" [features] schemars = ["dep:schemars"] -default = [] +config = ["dep:config"] +default = ["config"] [dev-dependencies] insta = { version = "1.41.1", features = ["yaml"] } diff --git a/cli/src/config.rs b/cli/src/config.rs deleted file mode 100644 index 6f252ff..0000000 --- a/cli/src/config.rs +++ /dev/null @@ -1,142 +0,0 @@ -use serde::{Deserialize, Serialize}; -use std::fmt; -use std::{fs, path::PathBuf}; - -use crate::rule::Rules; - -/// Default Root config file path to search for. -const DEFAULT_CONFIG_ROOT: &str = "."; - -/// Default commitlintrc configuration files -/// If the user didn't specify a configuration file with -c or --config argument, -/// we will try to find one of these files in the current directory. -const DEFAULT_CONFIG_FILE: [&str; 4] = [ - ".commitlintrc", - ".commitlintrc.json", - ".commitlintrc.yaml", - ".commitlintrc.yml", -]; - -/// Config represents the configuration of commitlint. -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -pub struct Config { - /// Rules represents the rules of commitlint. - pub rules: Rules, -} - -impl fmt::Display for Config { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let s = serde_yaml::to_string(&self).unwrap(); - write!(f, "{}", s) - } -} - -/// Load configuration from the specified path. -pub async fn load(path: Option) -> Result { - let config_file = match &path { - Some(p) => Some(p.clone()), - None => find_config_file(PathBuf::from(DEFAULT_CONFIG_ROOT)), - }; - - match (config_file, path) { - // If the file was specified and found, load it. - (Some(p), _) => load_config_file(p).await, - // If the file was not specified and not found, return default config. - (None, None) => Ok(Config::default()), - // If the was explicitly specified but not found, return an error. - (None, Some(p)) => Err(format!("Configuration file not found in {}", p.display())), - } -} - -/// Find configuration file in the specified path. -/// Note that the first file found will be returned. -pub fn find_config_file(path: PathBuf) -> Option { - let mut path = path; - for file in DEFAULT_CONFIG_FILE.iter() { - path.push(file); - if path.exists() { - return Some(path); - } - path.pop(); - } - - None -} - -/// Load config file from the specified path. -pub async fn load_config_file(path: PathBuf) -> Result { - if !path.exists() { - return Err(format!( - "Configuration file not found in {}", - path.display() - )); - } - - match path.extension() { - Some(ext) => match ext.to_str() { - Some("json") => load_json_config_file(path).await, - Some("yaml") | Some("yml") => load_yaml_config_file(path).await, - _ => load_unknown_config_file(path).await, - }, - None => Err(format!( - "Unsupported configuration file format: {}", - path.display() - )), - } -} - -/// Load JSON config file from the specified path. -async fn load_json_config_file(path: PathBuf) -> Result { - let text = fs::read_to_string(path).unwrap(); - - match serde_json::from_str::(&text) { - Ok(config) => Ok(config), - Err(err) => Err(format!("Failed to parse configuration file: {}", err)), - } -} - -/// Load YAML config file from the specified path. -async fn load_yaml_config_file(path: PathBuf) -> Result { - let text = fs::read_to_string(path).unwrap(); - - match serde_yaml::from_str::(&text) { - Ok(config) => Ok(config), - Err(err) => Err(format!("Failed to parse configuration file: {}", err)), - } -} - -/// Try to load configuration file from the specified path. -/// First try to load it as JSON, then as YAML. -/// If both fail, return an error. -async fn load_unknown_config_file(path: PathBuf) -> Result { - let text = fs::read_to_string(path.clone()).unwrap(); - - if let Ok(config) = serde_json::from_str::(&text) { - return Ok(config); - } - - if let Ok(config) = serde_yaml::from_str::(&text) { - return Ok(config); - } - - Err(format!( - "Failed to parse configuration file: {}", - path.display() - )) -} -#[cfg(test)] -mod tests { - #[cfg(feature = "schemars")] - #[test] - fn generate_json_schema() { - use crate::config::Config; - use insta::assert_yaml_snapshot; - use std::fs; - - let config_schema = schemars::schema_for!(Config); - let config_schema_json = serde_json::to_string_pretty(&config_schema).unwrap(); - assert_yaml_snapshot!(config_schema); - fs::write("json-schema/config.json", config_schema_json).unwrap(); - } -} diff --git a/cli/src/main.rs b/cli/src/main.rs index 007530d..812c765 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,9 +1,9 @@ mod args; -mod config; mod git; mod message; mod result; mod rule; +mod settings; use args::Args; use clap::Parser; @@ -15,7 +15,7 @@ use std::process::exit; async fn main() { let args = Args::parse(); - let config = match config::load(args.config.clone()).await { + let settings = match settings::load(args.config.clone()).await { Ok(c) => c, Err(err) => { eprintln!("Failed to load config: {}", err); @@ -24,7 +24,7 @@ async fn main() { }; if args.print_config { - println!("{}", config); + println!("{}", settings); } let messages = match args.read() { @@ -38,7 +38,7 @@ async fn main() { let threads = messages .into_iter() .map(|message| { - let config = config.clone(); + let config = settings.clone(); tokio::spawn(async move { validate(&message, &config).await }) }) .collect::>(); diff --git a/cli/src/message.rs b/cli/src/message.rs index f65dbfb..cde98b8 100644 --- a/cli/src/message.rs +++ b/cli/src/message.rs @@ -1,7 +1,7 @@ use crate::{ - config::Config, git::{parse_commit_message, parse_subject}, result::Result as LintResult, + settings::Settings, }; use std::{collections::HashMap, fmt::Error}; @@ -59,7 +59,7 @@ impl Message { } /// validate the raw commit message. -pub async fn validate(msg: &Message, config: &Config) -> Result { +pub async fn validate(msg: &Message, config: &Settings) -> Result { let violations = config.rules.validate(msg); Ok(LintResult { violations }) diff --git a/cli/src/settings.rs b/cli/src/settings.rs new file mode 100644 index 0000000..b0c45d4 --- /dev/null +++ b/cli/src/settings.rs @@ -0,0 +1,57 @@ +use crate::rule::Rules; +use config::*; +use serde::{Deserialize, Serialize}; +use std::fmt; +use std::path::PathBuf; + +/// Config represents the configuration of commitlint. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct Settings { + /// Rules represents the rules of commitlint. + pub rules: Rules, +} +impl Settings { + #[cfg(feature = "config")] + const DEFAULT_CONFIG_FILENAME: &str = ".commitlintrc"; + #[cfg(feature = "config")] + pub fn new(path: Option) -> Result { + let mut conf_builder = Config::builder(); + if let Some(path) = path { + conf_builder = + conf_builder.add_source(File::with_name(path.to_str().unwrap()).required(true)); + } + conf_builder = conf_builder + .add_source(File::with_name(Self::DEFAULT_CONFIG_FILENAME).required(false)) + .add_source(Environment::with_prefix("CL")); + let conf = conf_builder.build()?; + + conf.try_deserialize() + } +} +impl fmt::Display for Settings { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s = serde_yaml::to_string(&self).unwrap(); + write!(f, "{}", s) + } +} + +pub async fn load(path: Option) -> Result { + Settings::new(path) +} + +#[cfg(test)] +mod tests { + #[cfg(feature = "schemars")] + #[test] + fn generate_json_schema() { + use crate::settings::Settings; + use insta::assert_yaml_snapshot; + use std::fs; + + let config_schema = schemars::schema_for!(Config); + let config_schema_json = serde_json::to_string_pretty(&config_schema).unwrap(); + assert_yaml_snapshot!(config_schema); + fs::write("json-schema/config.json", config_schema_json).unwrap(); + } +} From 0937f9c4ec0c6f255af500281eff70674b7f2d50 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 17:39:49 +0100 Subject: [PATCH 2/9] fix(cli): add explicit Environment.separator --- cli/src/settings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/settings.rs b/cli/src/settings.rs index b0c45d4..fc95e55 100644 --- a/cli/src/settings.rs +++ b/cli/src/settings.rs @@ -23,7 +23,7 @@ impl Settings { } conf_builder = conf_builder .add_source(File::with_name(Self::DEFAULT_CONFIG_FILENAME).required(false)) - .add_source(Environment::with_prefix("CL")); + .add_source(Environment::with_prefix("CL").separator("_")); let conf = conf_builder.build()?; conf.try_deserialize() From 00d58c7863626289876363bfc37f8b8888dd6f1a Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 17:48:06 +0100 Subject: [PATCH 3/9] fix: settings rename missed & use [serde(rename_all)] --- cli/json-schema/config.json | 32 +- cli/src/rule.rs | 16 +- cli/src/settings.rs | 2 +- ...settings__tests__generate_json_schema.snap | 283 ++++++++++++++++++ 4 files changed, 301 insertions(+), 32 deletions(-) create mode 100644 cli/src/snapshots/commitlint__settings__tests__generate_json_schema.snap diff --git a/cli/json-schema/config.json b/cli/json-schema/config.json index a622a28..8fffabf 100644 --- a/cli/json-schema/config.json +++ b/cli/json-schema/config.json @@ -1,6 +1,6 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "Config", + "title": "Settings", "description": "Config represents the configuration of commitlint.", "type": "object", "properties": { @@ -14,7 +14,7 @@ ], "$defs": { "BodyEmpty": { - "description": "[BodyEmpty] represents the `[body-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/body-empty)` rule.", + "description": "[BodyEmpty] represents the [`body-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/body-empty) rule.", "type": "object", "properties": { "level": { @@ -31,7 +31,7 @@ } }, "BodyMaxLength": { - "description": "[BodyMaxLength] represents the `[body-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/body-max-length)` rule.", + "description": "[BodyMaxLength] represents the [`body-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/body-max-length) rule.", "type": "object", "properties": { "length": { @@ -57,7 +57,7 @@ ] }, "DescriptionEmpty": { - "description": "[DescriptionEmpty] represents the `[description-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/description-empty)` rule.", + "description": "[DescriptionEmpty] represents the [`description-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-empty) rule.", "type": "object", "properties": { "level": { @@ -74,7 +74,7 @@ } }, "DescriptionFormat": { - "description": "[DescriptionFormat] represents the `[description-format](https://keisukeyamashita.github.io/commitlint-rs/rules/description-format)` rule.", + "description": "[DescriptionFormat] represents the [`description-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-format) rule.", "type": "object", "properties": { "format": { @@ -98,7 +98,7 @@ } }, "DescriptionMaxLength": { - "description": "[DescriptionMaxLength] represents the `[description-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/description-max-length)` rule.", + "description": "[DescriptionMaxLength] represents the [`description-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-max-length) rule.", "type": "object", "properties": { "length": { @@ -124,7 +124,7 @@ ] }, "FootersEmpty": { - "description": "[FootersEmpty] represents the `[footers-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/footers-empty)` rule.", + "description": "[FootersEmpty] represents the [`footers-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/footers-empty) rule.", "type": "object", "properties": { "level": { @@ -306,7 +306,7 @@ } }, "Scope": { - "description": "[Scope] represents the `[scope](https://keisukeyamashita.github.io/commitlint-rs/rules/scope)` rule.", + "description": "[Scope] represents the [`scope`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope) rule.", "type": "object", "properties": { "level": { @@ -338,7 +338,7 @@ ] }, "ScopeEmpty": { - "description": "[ScopeEmpty] represents the `[scope-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-empty)` rule.", + "description": "[ScopeEmpty] represents the [`scope-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-empty) rule.", "type": "object", "properties": { "level": { @@ -355,7 +355,7 @@ } }, "ScopeFormat": { - "description": "[ScopeFormat] represents the `[scope-format](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-format)` rule.", + "description": "[ScopeFormat] represents the [`scope-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-format) rule.", "type": "object", "properties": { "format": { @@ -379,7 +379,7 @@ } }, "ScopeMaxLength": { - "description": "[ScopeMaxLength] represents the `[scope-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-max-length)` rule.", + "description": "[ScopeMaxLength] represents the [`scope-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-max-length) rule.", "type": "object", "properties": { "length": { @@ -405,7 +405,7 @@ ] }, "SubjectEmpty": { - "description": "[SubjectEmpty] represents the `[subject-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/subject-empty)` rule.", + "description": "[SubjectEmpty] represents the [`subject-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/subject-empty) rule.", "type": "object", "properties": { "level": { @@ -422,7 +422,7 @@ } }, "Type": { - "description": "[Type] represents the `[type](https://keisukeyamashita.github.io/commitlint-rs/rules/type)` rule.", + "description": "[Type] represents the [`type`](https://keisukeyamashita.github.io/commitlint-rs/rules/type) rule.", "type": "object", "properties": { "level": { @@ -449,7 +449,7 @@ ] }, "TypeEmpty": { - "description": "[TypeEmpty] represents the `[type-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/type-empty)` rule.", + "description": "[TypeEmpty] represents the [`type-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-empty) rule.", "type": "object", "properties": { "level": { @@ -466,7 +466,7 @@ } }, "TypeFormat": { - "description": "[TypeFormat] represents the `[type-format](https://keisukeyamashita.github.io/commitlint-rs/rules/type-format)` rule.", + "description": "[TypeFormat] represents the [`type-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-format) rule.", "type": "object", "properties": { "format": { @@ -490,7 +490,7 @@ } }, "TypeMaxLength": { - "description": "[TypeMaxLength] represents the `[type-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/type-max-length)` rule.", + "description": "[TypeMaxLength] represents the [`type-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-max-length) rule.", "type": "object", "properties": { "length": { diff --git a/cli/src/rule.rs b/cli/src/rule.rs index 3ed4b4b..dc9a3fb 100644 --- a/cli/src/rule.rs +++ b/cli/src/rule.rs @@ -29,65 +29,51 @@ pub mod type_max_length; /// Rules represents the rules of commitlint. /// See: https://commitlint.js.org/reference/rules.html #[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct Rules { - #[serde(rename = "body-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub body_empty: Option, - #[serde(rename = "body-max-length")] #[serde(skip_serializing_if = "Option::is_none")] pub body_max_length: Option, - #[serde(rename = "description-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub description_empty: Option, - #[serde(rename = "description-format")] #[serde(skip_serializing_if = "Option::is_none")] pub description_format: Option, - #[serde(rename = "description-max-length")] #[serde(skip_serializing_if = "Option::is_none")] pub description_max_length: Option, - #[serde(rename = "footers-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub footers_empty: Option, - #[serde(rename = "scope")] #[serde(skip_serializing_if = "Option::is_none")] pub scope: Option, - #[serde(rename = "scope-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub scope_empty: Option, - #[serde(rename = "scope-format")] #[serde(skip_serializing_if = "Option::is_none")] pub scope_format: Option, - #[serde(rename = "scope-max-length")] #[serde(skip_serializing_if = "Option::is_none")] pub scope_max_length: Option, - #[serde(rename = "subject-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub subject_empty: Option, - #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] pub r#type: Option, - #[serde(rename = "type-empty")] #[serde(skip_serializing_if = "Option::is_none")] pub type_empty: Option, - #[serde(rename = "type-format")] #[serde(skip_serializing_if = "Option::is_none")] pub type_format: Option, - #[serde(rename = "type-max-length")] #[serde(skip_serializing_if = "Option::is_none")] pub type_max_length: Option, } diff --git a/cli/src/settings.rs b/cli/src/settings.rs index fc95e55..10d3efb 100644 --- a/cli/src/settings.rs +++ b/cli/src/settings.rs @@ -49,7 +49,7 @@ mod tests { use insta::assert_yaml_snapshot; use std::fs; - let config_schema = schemars::schema_for!(Config); + let config_schema = schemars::schema_for!(Settings); let config_schema_json = serde_json::to_string_pretty(&config_schema).unwrap(); assert_yaml_snapshot!(config_schema); fs::write("json-schema/config.json", config_schema_json).unwrap(); diff --git a/cli/src/snapshots/commitlint__settings__tests__generate_json_schema.snap b/cli/src/snapshots/commitlint__settings__tests__generate_json_schema.snap new file mode 100644 index 0000000..9f6b0c5 --- /dev/null +++ b/cli/src/snapshots/commitlint__settings__tests__generate_json_schema.snap @@ -0,0 +1,283 @@ +--- +source: cli/src/settings.rs +expression: config_schema +snapshot_kind: text +--- +$schema: "https://json-schema.org/draft/2020-12/schema" +title: Settings +description: Config represents the configuration of commitlint. +type: object +properties: + rules: + description: Rules represents the rules of commitlint. + $ref: "#/$defs/Rules" +required: + - rules +$defs: + BodyEmpty: + description: "[BodyEmpty] represents the [`body-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/body-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + BodyMaxLength: + description: "[BodyMaxLength] represents the [`body-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/body-max-length) rule." + type: object + properties: + length: + description: "Length represents the maximum length of the \"body\"." + type: integer + format: uint + minimum: 0 + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + required: + - length + DescriptionEmpty: + description: "[DescriptionEmpty] represents the [`description-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + DescriptionFormat: + description: "[DescriptionFormat] represents the [`description-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-format) rule." + type: object + properties: + format: + description: "Format represents the format of the \"description\"." + type: + - string + - "null" + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + DescriptionMaxLength: + description: "[DescriptionMaxLength] represents the [`description-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/description-max-length) rule." + type: object + properties: + length: + description: "Length represents the maximum length of the \"description\"." + type: integer + format: uint + minimum: 0 + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + required: + - length + FootersEmpty: + description: "[FootersEmpty] represents the [`footers-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/footers-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + Level: + description: Level represents the level of a rule. + type: string + enum: + - error + - ignore + - warning + Rules: + description: "Rules represents the rules of commitlint.\n See: https://commitlint.js.org/reference/rules.html" + type: object + properties: + type: + anyOf: + - $ref: "#/$defs/Type" + - type: "null" + body-empty: + anyOf: + - $ref: "#/$defs/BodyEmpty" + - type: "null" + body-max-length: + anyOf: + - $ref: "#/$defs/BodyMaxLength" + - type: "null" + description-empty: + anyOf: + - $ref: "#/$defs/DescriptionEmpty" + - type: "null" + description-format: + anyOf: + - $ref: "#/$defs/DescriptionFormat" + - type: "null" + description-max-length: + anyOf: + - $ref: "#/$defs/DescriptionMaxLength" + - type: "null" + footers-empty: + anyOf: + - $ref: "#/$defs/FootersEmpty" + - type: "null" + scope: + anyOf: + - $ref: "#/$defs/Scope" + - type: "null" + scope-empty: + anyOf: + - $ref: "#/$defs/ScopeEmpty" + - type: "null" + scope-format: + anyOf: + - $ref: "#/$defs/ScopeFormat" + - type: "null" + scope-max-length: + anyOf: + - $ref: "#/$defs/ScopeMaxLength" + - type: "null" + subject-empty: + anyOf: + - $ref: "#/$defs/SubjectEmpty" + - type: "null" + type-empty: + anyOf: + - $ref: "#/$defs/TypeEmpty" + - type: "null" + type-format: + anyOf: + - $ref: "#/$defs/TypeFormat" + - type: "null" + type-max-length: + anyOf: + - $ref: "#/$defs/TypeMaxLength" + - type: "null" + Scope: + description: "[Scope] represents the [`scope`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + optional: + description: "Optional scope. If true, even if the scope is not present, it is allowed." + type: boolean + options: + description: "Options represents the options of the rule. If the option is empty, it means that no \"scope\" is allowed." + type: array + items: + type: string + required: + - optional + - options + ScopeEmpty: + description: "[ScopeEmpty] represents the [`scope-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + ScopeFormat: + description: "[ScopeFormat] represents the [`scope-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-format) rule." + type: object + properties: + format: + description: "Format represents the format of the \"scope\"." + type: + - string + - "null" + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + ScopeMaxLength: + description: "[ScopeMaxLength] represents the [`scope-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-max-length) rule." + type: object + properties: + length: + description: "Length represents the maximum length of the \"scope\"." + type: integer + format: uint + minimum: 0 + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + required: + - length + SubjectEmpty: + description: "[SubjectEmpty] represents the [`subject-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/subject-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + Type: + description: "[Type] represents the [`type`](https://keisukeyamashita.github.io/commitlint-rs/rules/type) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + options: + description: "Options represents the options of the rule. If the option is empty, it means that no \"type\" is allowed." + type: array + items: + type: string + required: + - options + TypeEmpty: + description: "[TypeEmpty] represents the [`type-empty`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-empty) rule." + type: object + properties: + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + TypeFormat: + description: "[TypeFormat] represents the [`type-format`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-format) rule." + type: object + properties: + format: + description: "Format represents the format of the \"type\"." + type: + - string + - "null" + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + TypeMaxLength: + description: "[TypeMaxLength] represents the [`type-max-length`](https://keisukeyamashita.github.io/commitlint-rs/rules/type-max-length) rule." + type: object + properties: + length: + description: "Length represents the maximum length of the \"type\"." + type: integer + format: uint + minimum: 0 + level: + description: Level represents the level of the rule. + anyOf: + - $ref: "#/$defs/Level" + - type: "null" + required: + - length From 8f1e190f5d307abf4263fa71be90c21589372538 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Mon, 2 Dec 2024 17:50:08 +0100 Subject: [PATCH 4/9] test: rm old snap --- ...__config__tests__generate_json_schema.snap | 283 ------------------ 1 file changed, 283 deletions(-) delete mode 100644 cli/src/snapshots/commitlint__config__tests__generate_json_schema.snap diff --git a/cli/src/snapshots/commitlint__config__tests__generate_json_schema.snap b/cli/src/snapshots/commitlint__config__tests__generate_json_schema.snap deleted file mode 100644 index bba0784..0000000 --- a/cli/src/snapshots/commitlint__config__tests__generate_json_schema.snap +++ /dev/null @@ -1,283 +0,0 @@ ---- -source: cli/src/config.rs -expression: config_schema -snapshot_kind: text ---- -$schema: "https://json-schema.org/draft/2020-12/schema" -title: Config -description: Config represents the configuration of commitlint. -type: object -properties: - rules: - description: Rules represents the rules of commitlint. - $ref: "#/$defs/Rules" -required: - - rules -$defs: - BodyEmpty: - description: "[BodyEmpty] represents the `[body-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/body-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - BodyMaxLength: - description: "[BodyMaxLength] represents the `[body-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/body-max-length)` rule." - type: object - properties: - length: - description: "Length represents the maximum length of the \"body\"." - type: integer - format: uint - minimum: 0 - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - required: - - length - DescriptionEmpty: - description: "[DescriptionEmpty] represents the `[description-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/description-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - DescriptionFormat: - description: "[DescriptionFormat] represents the `[description-format](https://keisukeyamashita.github.io/commitlint-rs/rules/description-format)` rule." - type: object - properties: - format: - description: "Format represents the format of the \"description\"." - type: - - string - - "null" - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - DescriptionMaxLength: - description: "[DescriptionMaxLength] represents the `[description-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/description-max-length)` rule." - type: object - properties: - length: - description: "Length represents the maximum length of the \"description\"." - type: integer - format: uint - minimum: 0 - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - required: - - length - FootersEmpty: - description: "[FootersEmpty] represents the `[footers-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/footers-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - Level: - description: Level represents the level of a rule. - type: string - enum: - - error - - ignore - - warning - Rules: - description: "Rules represents the rules of commitlint.\n See: https://commitlint.js.org/reference/rules.html" - type: object - properties: - type: - anyOf: - - $ref: "#/$defs/Type" - - type: "null" - body-empty: - anyOf: - - $ref: "#/$defs/BodyEmpty" - - type: "null" - body-max-length: - anyOf: - - $ref: "#/$defs/BodyMaxLength" - - type: "null" - description-empty: - anyOf: - - $ref: "#/$defs/DescriptionEmpty" - - type: "null" - description-format: - anyOf: - - $ref: "#/$defs/DescriptionFormat" - - type: "null" - description-max-length: - anyOf: - - $ref: "#/$defs/DescriptionMaxLength" - - type: "null" - footers-empty: - anyOf: - - $ref: "#/$defs/FootersEmpty" - - type: "null" - scope: - anyOf: - - $ref: "#/$defs/Scope" - - type: "null" - scope-empty: - anyOf: - - $ref: "#/$defs/ScopeEmpty" - - type: "null" - scope-format: - anyOf: - - $ref: "#/$defs/ScopeFormat" - - type: "null" - scope-max-length: - anyOf: - - $ref: "#/$defs/ScopeMaxLength" - - type: "null" - subject-empty: - anyOf: - - $ref: "#/$defs/SubjectEmpty" - - type: "null" - type-empty: - anyOf: - - $ref: "#/$defs/TypeEmpty" - - type: "null" - type-format: - anyOf: - - $ref: "#/$defs/TypeFormat" - - type: "null" - type-max-length: - anyOf: - - $ref: "#/$defs/TypeMaxLength" - - type: "null" - Scope: - description: "[Scope] represents the `[scope](https://keisukeyamashita.github.io/commitlint-rs/rules/scope)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - optional: - description: "Optional scope. If true, even if the scope is not present, it is allowed." - type: boolean - options: - description: "Options represents the options of the rule. If the option is empty, it means that no \"scope\" is allowed." - type: array - items: - type: string - required: - - optional - - options - ScopeEmpty: - description: "[ScopeEmpty] represents the `[scope-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - ScopeFormat: - description: "[ScopeFormat] represents the `[scope-format](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-format)` rule." - type: object - properties: - format: - description: "Format represents the format of the \"scope\"." - type: - - string - - "null" - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - ScopeMaxLength: - description: "[ScopeMaxLength] represents the `[scope-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/scope-max-length)` rule." - type: object - properties: - length: - description: "Length represents the maximum length of the \"scope\"." - type: integer - format: uint - minimum: 0 - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - required: - - length - SubjectEmpty: - description: "[SubjectEmpty] represents the `[subject-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/subject-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - Type: - description: "[Type] represents the `[type](https://keisukeyamashita.github.io/commitlint-rs/rules/type)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - options: - description: "Options represents the options of the rule. If the option is empty, it means that no \"type\" is allowed." - type: array - items: - type: string - required: - - options - TypeEmpty: - description: "[TypeEmpty] represents the `[type-empty](https://keisukeyamashita.github.io/commitlint-rs/rules/type-empty)` rule." - type: object - properties: - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - TypeFormat: - description: "[TypeFormat] represents the `[type-format](https://keisukeyamashita.github.io/commitlint-rs/rules/type-format)` rule." - type: object - properties: - format: - description: "Format represents the format of the \"type\"." - type: - - string - - "null" - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - TypeMaxLength: - description: "[TypeMaxLength] represents the `[type-max-length](https://keisukeyamashita.github.io/commitlint-rs/rules/type-max-length)` rule." - type: object - properties: - length: - description: "Length represents the maximum length of the \"type\"." - type: integer - format: uint - minimum: 0 - level: - description: Level represents the level of the rule. - anyOf: - - $ref: "#/$defs/Level" - - type: "null" - required: - - length From 37f871ea3ac36aeb43747130fc54066177f1abaa Mon Sep 17 00:00:00 2001 From: elcoosp Date: Tue, 3 Dec 2024 12:05:43 +0100 Subject: [PATCH 5/9] feat: use rsmack_wrap::wrap --- Cargo.lock | 594 ++++++++++++++++++++++++++++++++++++++++----- cli/Cargo.toml | 9 + cli/build.rs | 5 + cli/src/message.rs | 4 +- cli/src/rule.rs | 67 ++--- cspell.yml | 2 +- 6 files changed, 575 insertions(+), 106 deletions(-) create mode 100644 cli/build.rs diff --git a/Cargo.lock b/Cargo.lock index 07dc3fb..5564869 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -44,6 +44,21 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.18" @@ -107,7 +122,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -137,6 +152,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bitflags" version = "2.6.0" @@ -155,11 +176,38 @@ dependencies = [ "generic-array", ] +[[package]] +name = "build-print" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a2128d00b7061b82b72844a351e80acd29e05afc60e9261e2ac90dca9ecc2ac" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" + +[[package]] +name = "cc" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +dependencies = [ + "shlex", +] [[package]] name = "cfg-if" @@ -167,11 +215,24 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets", +] + [[package]] name = "clap" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" dependencies = [ "clap_builder", "clap_derive", @@ -179,9 +240,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.20" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" dependencies = [ "anstream", "anstyle", @@ -198,14 +259,14 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" [[package]] name = "colorchoice" @@ -223,9 +284,13 @@ dependencies = [ "insta", "paste", "regex", + "rsmack-fs", + "rsmack-utils", + "rsmack-wrap", "schemars", "serde", "serde_json", + "serde_with", "serde_yaml", "tokio", ] @@ -290,6 +355,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.16" @@ -315,6 +386,51 @@ dependencies = [ "typenum", ] +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.90", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.90", +] + +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + [[package]] name = "digest" version = "0.10.7" @@ -334,6 +450,12 @@ dependencies = [ "const-random", ] +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "dyn-clone" version = "1.0.17" @@ -361,6 +483,22 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "fsio" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dad0ce30be0cc441b325c5d705c8b613a0ca0d92b6a8953d41bd236dc09a36d0" +dependencies = [ + "dunce", + "rand", +] + [[package]] name = "futures" version = "0.3.31" @@ -417,7 +555,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -477,6 +615,12 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.14.5" @@ -489,9 +633,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "hashlink" @@ -509,19 +653,60 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] -name = "hermit-abi" -version = "0.3.9" +name = "hex" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "indexmap" -version = "2.6.0" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", - "hashbrown 0.15.1", + "hashbrown 0.15.2", + "serde", ] [[package]] @@ -545,9 +730,19 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itoa" -version = "1.0.11" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "js-sys" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" +dependencies = [ + "once_cell", + "wasm-bindgen", +] [[package]] name = "json5" @@ -568,9 +763,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.161" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "linked-hash-map" @@ -588,6 +783,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + [[package]] name = "memchr" version = "2.7.4" @@ -611,11 +812,10 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ - "hermit-abi", "libc", "wasi", "windows-sys 0.52.0", @@ -631,6 +831,21 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.5" @@ -682,7 +897,7 @@ dependencies = [ [[package]] name = "paste" version = "1.0.15" -source = "git+https://github.com/elcoosp/paste?branch=feat/dash#4efa1dd157f1a9bc8994fffd653bb2e1c744828a" +source = "git+https://github.com/elcoosp/paste?branch=feat%2Fdash#4efa1dd157f1a9bc8994fffd653bb2e1c744828a" [[package]] name = "pathdiff" @@ -721,7 +936,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -747,11 +962,50 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -765,6 +1019,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.5.7" @@ -791,7 +1075,7 @@ checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -808,9 +1092,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", @@ -829,12 +1113,60 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" dependencies = [ - "base64", + "base64 0.21.7", "bitflags", "serde", "serde_derive", ] +[[package]] +name = "rsmack-fs" +version = "0.0.0" +dependencies = [ + "darling", + "paste", + "proc-macro-error", + "proc-macro2", + "quote", + "rsmack-utils", + "run_script", + "syn 2.0.90", +] + +[[package]] +name = "rsmack-utils" +version = "0.0.0" +dependencies = [ + "build-print", + "darling", + "proc-macro-error", + "proc-macro2", + "quote", + "stringcase", + "syn 2.0.90", +] + +[[package]] +name = "rsmack-wrap" +version = "0.0.0" +dependencies = [ + "darling", + "proc-macro-error", + "proc-macro2", + "quote", + "rsmack-utils", + "syn 2.0.90", +] + +[[package]] +name = "run_script" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f20d7c40e6d6fc2ff69ad8e8bac4d39a5fc1874402647ac92b620954d56d5c3e" +dependencies = [ + "fsio", +] + [[package]] name = "rust-ini" version = "0.20.0" @@ -879,7 +1211,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn", + "syn 2.0.90", ] [[package]] @@ -890,22 +1222,22 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.214" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.214" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -916,14 +1248,14 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", "memchr", @@ -940,13 +1272,43 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.7.0", + "serde", + "serde_derive", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.90", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ - "indexmap", + "indexmap 2.7.0", "itoa", "ryu", "serde", @@ -964,6 +1326,12 @@ dependencies = [ "digest", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -996,14 +1364,20 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" dependencies = [ "libc", "windows-sys 0.52.0", ] +[[package]] +name = "stringcase" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04028eeb851ed08af6aba5caa29f2d59a13ed168cee4d6bd753aeefcf1d636b0" + [[package]] name = "strsim" version = "0.11.1" @@ -1012,9 +1386,19 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.87" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -1038,7 +1422,38 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", +] + +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", ] [[package]] @@ -1052,9 +1467,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.41.0" +version = "1.41.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" +checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" dependencies = [ "backtrace", "bytes", @@ -1076,7 +1491,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -1106,7 +1521,7 @@ version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ - "indexmap", + "indexmap 2.7.0", "serde", "serde_spanned", "toml_datetime", @@ -1127,9 +1542,9 @@ checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" [[package]] name = "unicode-ident" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "unicode-segmentation" @@ -1161,6 +1576,70 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.90", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -1269,6 +1748,7 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] @@ -1280,5 +1760,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index c7f20be..522f3ec 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -22,8 +22,17 @@ config = { version = "0.14.1", optional = true } serde = { version = "1.0.201", features = ["derive"] } serde_json = "1.0.121" serde_yaml = "0.9.34" +serde_with = { version = "3.11.0", features = ["macros"] } tokio = { version = "1.37.0", features = ["full"] } paste = { git = "https://github.com/elcoosp/paste", branch = "feat/dash" } +rsmack-wrap = { path = "../../rsmack/packages/rsmack-wrap" } +[build-dependencies] +serde_with = { version = "3.11.0", features = ["macros"] } +rsmack-fs = { path = "../../rsmack/packages/rsmack-fs" } +rsmack-utils = { path = "../../rsmack/packages/rsmack-utils" } +rsmack-wrap = { path = "../../rsmack/packages/rsmack-wrap" } +schemars = { version = "1.0.0-alpha.16", optional = true } +serde = { version = "1.0.201", features = ["derive"] } [[bin]] name = "commitlint" diff --git a/cli/build.rs b/cli/build.rs new file mode 100644 index 0000000..11935bf --- /dev/null +++ b/cli/build.rs @@ -0,0 +1,5 @@ +use rsmack_utils::*; + +fn main() { + build_folder_iso_struct("Rules", "rule", "cli") +} diff --git a/cli/src/message.rs b/cli/src/message.rs index cde98b8..a685127 100644 --- a/cli/src/message.rs +++ b/cli/src/message.rs @@ -59,8 +59,8 @@ impl Message { } /// validate the raw commit message. -pub async fn validate(msg: &Message, config: &Settings) -> Result { - let violations = config.rules.validate(msg); +pub async fn validate(msg: &Message, settings: &Settings) -> Result { + let violations = settings.rules.validate(msg); Ok(LintResult { violations }) } diff --git a/cli/src/rule.rs b/cli/src/rule.rs index dc9a3fb..0449c20 100644 --- a/cli/src/rule.rs +++ b/cli/src/rule.rs @@ -25,57 +25,32 @@ pub mod r#type; pub mod type_empty; pub mod type_format; pub mod type_max_length; - +// include!(concat!(env!("OUT_DIR"), "/Rules.rs")); /// Rules represents the rules of commitlint. /// See: https://commitlint.js.org/reference/rules.html +#[rsmack_wrap::wrap(with = Option)] #[derive(Clone, Debug, Deserialize, Serialize)] -#[serde(rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[serde(rename_all = "kebab-case")] +#[serde_with::apply( + Option => #[serde(skip_serializing_if = "Option::is_none")], +)] pub struct Rules { - #[serde(skip_serializing_if = "Option::is_none")] - pub body_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub body_max_length: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub description_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub description_format: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub description_max_length: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub footers_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub scope: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub scope_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub scope_format: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub scope_max_length: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub subject_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub r#type: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub type_empty: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub type_format: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub type_max_length: Option, + pub body_empty: BodyEmpty, + pub body_max_length: BodyMaxLength, + pub description_empty: DescriptionEmpty, + pub description_format: DescriptionFormat, + pub description_max_length: DescriptionMaxLength, + pub footers_empty: FootersEmpty, + pub scope: Scope, + pub scope_empty: ScopeEmpty, + pub scope_format: ScopeFormat, + pub scope_max_length: ScopeMaxLength, + pub subject_empty: SubjectEmpty, + pub r#type: Type, + pub type_empty: TypeEmpty, + pub type_format: TypeFormat, + pub type_max_length: TypeMaxLength, } /// Rule is a collection of rules. diff --git a/cspell.yml b/cspell.yml index 1d7a0cd..5c1bce2 100644 --- a/cspell.yml +++ b/cspell.yml @@ -1 +1 @@ -words: [commitlint, schemars, commitlintrc, Keke, insta, binstall, serde, Keisuke, astrojs, splitn, insta, schemars] \ No newline at end of file +words: [commitlint, schemars, commitlintrc, Keke, insta, binstall, serde, Keisuke, astrojs, splitn, insta, schemars,rsmack] \ No newline at end of file From 280479539db330e30d0ff8d461ab714e869113f9 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Tue, 3 Dec 2024 16:36:44 +0100 Subject: [PATCH 6/9] fix: fix settings renaming --- cli/src/lib.rs | 2 +- cli/src/settings.rs | 16 ---------------- schema/src/main.rs | 8 ++++---- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index c4d8f69..2b8d35d 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,5 +1,5 @@ -pub mod config; pub mod git; pub mod message; pub mod result; pub mod rule; +pub mod settings; diff --git a/cli/src/settings.rs b/cli/src/settings.rs index 10d3efb..7d860e6 100644 --- a/cli/src/settings.rs +++ b/cli/src/settings.rs @@ -39,19 +39,3 @@ impl fmt::Display for Settings { pub async fn load(path: Option) -> Result { Settings::new(path) } - -#[cfg(test)] -mod tests { - #[cfg(feature = "schemars")] - #[test] - fn generate_json_schema() { - use crate::settings::Settings; - use insta::assert_yaml_snapshot; - use std::fs; - - let config_schema = schemars::schema_for!(Settings); - let config_schema_json = serde_json::to_string_pretty(&config_schema).unwrap(); - assert_yaml_snapshot!(config_schema); - fs::write("json-schema/config.json", config_schema_json).unwrap(); - } -} diff --git a/schema/src/main.rs b/schema/src/main.rs index c47416c..caa3b8a 100644 --- a/schema/src/main.rs +++ b/schema/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use cli::config::Config; +use cli::settings::Settings; use std::fs; /// CLI Arguments #[derive(Parser, Debug)] @@ -12,17 +12,17 @@ struct Args { fn main() { let args = Args::parse(); - let config_schema = schemars::schema_for!(Config); + let config_schema = schemars::schema_for!(Settings); let config_schema_json = serde_json::to_string_pretty(&config_schema).unwrap(); fs::write(&args.path, config_schema_json).unwrap(); } #[cfg(test)] mod tests { + use cli::settings::Settings; #[test] fn snap_json_schema() { - use cli::config::Config; use insta::assert_yaml_snapshot; - let config_schema = schemars::schema_for!(Config); + let config_schema = schemars::schema_for!(Settings); assert_yaml_snapshot!(config_schema); } } From f22d79017c1b3d02d4746c43eb85ce08b149d521 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Tue, 3 Dec 2024 17:15:50 +0100 Subject: [PATCH 7/9] feat: functionnal generated Rules in build.rs with all necessary attrs --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++++++++++ cli/Cargo.toml | 1 + cli/build.rs | 17 ++++++++++++++++- cli/src/rule.rs | 44 +++++--------------------------------------- 4 files changed, 65 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d17f37e..c8a0d54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,6 +176,31 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bon" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9276fe602371cd8a7f70fe68c4db55b2d3e92c570627d6ed0427646edfa5cf47" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94828b84b32b4f3ac3865f692fcdbc46c7d0dd87b29658a391d58a244e1ce45a" +dependencies = [ + "darling", + "ident_case", + "prettyplease", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.90", +] + [[package]] name = "build-print" version = "0.1.1" @@ -282,6 +307,7 @@ dependencies = [ "config", "futures", "paste", + "quote", "regex", "rsmack-fs", "rsmack-utils", @@ -976,6 +1002,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "prettyplease" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" +dependencies = [ + "proc-macro2", + "syn 2.0.90", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1136,6 +1172,7 @@ dependencies = [ name = "rsmack-utils" version = "0.0.0" dependencies = [ + "bon", "build-print", "darling", "proc-macro-error", @@ -1182,6 +1219,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + [[package]] name = "ryu" version = "1.0.18" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 20fc806..167c453 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -34,6 +34,7 @@ rsmack-fs = { path = "../../rsmack/packages/rsmack-fs" } rsmack-utils = { path = "../../rsmack/packages/rsmack-utils" } rsmack-wrap = { path = "../../rsmack/packages/rsmack-wrap" } schemars = { version = "1.0.0-alpha.16", optional = true } +quote = "1.0.37" serde = { version = "1.0.201", features = ["derive"] } [features] diff --git a/cli/build.rs b/cli/build.rs index 11935bf..0cc8aa8 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,5 +1,20 @@ use rsmack_utils::*; fn main() { - build_folder_iso_struct("Rules", "rule", "cli") + folder_iso_struct() + .name("Rules") + .pre(quote::quote! { + /// Rules represents the rules of commitlint. + /// See: https://commitlint.js.org/reference/rules.html + #[rsmack_wrap::wrap(with = Option)] + #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] + #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] + #[serde(rename_all = "kebab-case")] + #[serde_with::apply( + Option => #[serde(skip_serializing_if = "Option::is_none")], + )] + }) + .folder("rule") + .from_crate("cli") + .call(); } diff --git a/cli/src/rule.rs b/cli/src/rule.rs index 48460b7..328f71f 100644 --- a/cli/src/rule.rs +++ b/cli/src/rule.rs @@ -1,15 +1,6 @@ use crate::{message::Message, result::Violation}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; - -use self::{ - body_empty::BodyEmpty, body_max_length::BodyMaxLength, description_empty::DescriptionEmpty, - description_format::DescriptionFormat, description_max_length::DescriptionMaxLength, - footers_empty::FootersEmpty, r#type::Type, scope::Scope, scope_empty::ScopeEmpty, - scope_format::ScopeFormat, scope_max_length::ScopeMaxLength, subject_empty::SubjectEmpty, - type_empty::TypeEmpty, type_format::TypeFormat, type_max_length::TypeMaxLength, -}; - pub mod body_empty; pub mod body_max_length; pub mod description_empty; @@ -25,33 +16,8 @@ pub mod r#type; pub mod type_empty; pub mod type_format; pub mod type_max_length; -// include!(concat!(env!("OUT_DIR"), "/Rules.rs")); -/// Rules represents the rules of commitlint. -/// See: https://commitlint.js.org/reference/rules.html -#[rsmack_wrap::wrap(with = Option)] -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(rename_all = "kebab-case")] -#[serde_with::apply( - Option => #[serde(skip_serializing_if = "Option::is_none")], -)] -pub struct Rules { - pub body_empty: BodyEmpty, - pub body_max_length: BodyMaxLength, - pub description_empty: DescriptionEmpty, - pub description_format: DescriptionFormat, - pub description_max_length: DescriptionMaxLength, - pub footers_empty: FootersEmpty, - pub scope: Scope, - pub scope_empty: ScopeEmpty, - pub scope_format: ScopeFormat, - pub scope_max_length: ScopeMaxLength, - pub subject_empty: SubjectEmpty, - pub r#type: Type, - pub type_empty: TypeEmpty, - pub type_format: TypeFormat, - pub type_max_length: TypeMaxLength, -} + +include!(concat!(env!("OUT_DIR"), "/Rules.rs")); /// Rule is a collection of rules. impl Rules { @@ -153,7 +119,7 @@ impl Default for Rules { Self { body_empty: None, body_max_length: None, - description_empty: DescriptionEmpty::default().into(), + description_empty: description_empty::DescriptionEmpty::default().into(), description_format: None, description_max_length: None, footers_empty: None, @@ -161,9 +127,9 @@ impl Default for Rules { scope_empty: None, scope_format: None, scope_max_length: None, - subject_empty: SubjectEmpty::default().into(), + subject_empty: subject_empty::SubjectEmpty::default().into(), r#type: None, - type_empty: TypeEmpty::default().into(), + type_empty: type_empty::TypeEmpty::default().into(), type_format: None, type_max_length: None, } From 656e761891d8f508a949bdb60b2d8a528a610485 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Tue, 3 Dec 2024 18:04:41 +0100 Subject: [PATCH 8/9] feat: separate rule & rules mod - so that folder_iso_struct does not have to handle generating pub mod, which failed - --- cli/src/args.rs | 8 ++++++-- cli/src/lib.rs | 1 + cli/src/main.rs | 6 +++--- cli/src/result.rs | 2 +- cli/src/rule/body_empty.rs | 4 ++-- cli/src/rule/body_max_length.rs | 4 ++-- cli/src/rule/description_empty.rs | 4 ++-- cli/src/rule/description_format.rs | 4 ++-- cli/src/rule/description_max_length.rs | 4 ++-- cli/src/rule/footers_empty.rs | 4 ++-- cli/src/rule/mod.rs | 15 +++++++++++++++ cli/src/rule/scope.rs | 4 ++-- cli/src/rule/scope_empty.rs | 4 ++-- cli/src/rule/scope_format.rs | 4 ++-- cli/src/rule/scope_max_length.rs | 4 ++-- cli/src/rule/subject_empty.rs | 4 ++-- cli/src/rule/type.rs | 4 ++-- cli/src/rule/type_empty.rs | 4 ++-- cli/src/rule/type_format.rs | 4 ++-- cli/src/rule/type_max_length.rs | 4 ++-- cli/src/{rule.rs => rules.rs} | 18 +----------------- cli/src/settings.rs | 2 +- 22 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 cli/src/rule/mod.rs rename cli/src/{rule.rs => rules.rs} (94%) diff --git a/cli/src/args.rs b/cli/src/args.rs index dcf631f..9891bfe 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -87,8 +87,12 @@ impl Args { } let default_path = std::path::PathBuf::from(".git").join("COMMIT_EDITMSG"); - let msg = std::fs::read_to_string(&default_path).unwrap_or_else(|_| panic!("Failed to read commit message from {}", - default_path.display())); + let msg = std::fs::read_to_string(&default_path).unwrap_or_else(|_| { + panic!( + "Failed to read commit message from {}", + default_path.display() + ) + }); Ok(vec![Message::new(msg)]) } } diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 2b8d35d..61d2e75 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -2,4 +2,5 @@ pub mod git; pub mod message; pub mod result; pub mod rule; +pub mod rules; pub mod settings; diff --git a/cli/src/main.rs b/cli/src/main.rs index 812c765..107923a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -3,8 +3,8 @@ mod git; mod message; mod result; mod rule; +mod rules; mod settings; - use args::Args; use clap::Parser; use message::validate; @@ -55,11 +55,11 @@ async fn main() { if !h.violations.is_empty() { for violation in &h.violations { match violation.level { - rule::Level::Error => { + rules::Level::Error => { eprintln!("{}", violation.message); has_error = true } - rule::Level::Warning => { + rules::Level::Warning => { println!("{}", violation.message); } _ => {} diff --git a/cli/src/result.rs b/cli/src/result.rs index e3b5a27..7f75222 100644 --- a/cli/src/result.rs +++ b/cli/src/result.rs @@ -1,4 +1,4 @@ -use crate::rule::Level; +use crate::rules::Level; /// Result of the check. #[derive(Clone, Debug)] diff --git a/cli/src/rule/body_empty.rs b/cli/src/rule/body_empty.rs index 097dca8..e1f5efd 100644 --- a/cli/src/rule/body_empty.rs +++ b/cli/src/rule/body_empty.rs @@ -1,5 +1,5 @@ -use super::Level; -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::rules::Level; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; make_rule! { BodyEmpty, diff --git a/cli/src/rule/body_max_length.rs b/cli/src/rule/body_max_length.rs index cf3a66e..6528048 100644 --- a/cli/src/rule/body_max_length.rs +++ b/cli/src/rule/body_max_length.rs @@ -1,6 +1,6 @@ -use crate::{make_length_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_length_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_length_rule! { BodyMaxLength, "body" diff --git a/cli/src/rule/description_empty.rs b/cli/src/rule/description_empty.rs index b3d964c..61e628c 100644 --- a/cli/src/rule/description_empty.rs +++ b/cli/src/rule/description_empty.rs @@ -1,6 +1,6 @@ -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_rule! { DescriptionEmpty, } diff --git a/cli/src/rule/description_format.rs b/cli/src/rule/description_format.rs index cae74d5..a5950cf 100644 --- a/cli/src/rule/description_format.rs +++ b/cli/src/rule/description_format.rs @@ -1,5 +1,5 @@ -use super::Level; -use crate::{make_format_rule, message::Message, result::Violation, rule::Rule}; +use crate::rules::Level; +use crate::{make_format_rule, message::Message, result::Violation, rules::Rule}; make_format_rule! { DescriptionFormat, diff --git a/cli/src/rule/description_max_length.rs b/cli/src/rule/description_max_length.rs index 0d9d7b6..1371ed4 100644 --- a/cli/src/rule/description_max_length.rs +++ b/cli/src/rule/description_max_length.rs @@ -1,6 +1,6 @@ -use crate::{make_length_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_length_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_length_rule! { DescriptionMaxLength, diff --git a/cli/src/rule/footers_empty.rs b/cli/src/rule/footers_empty.rs index 966be42..60a3c56 100644 --- a/cli/src/rule/footers_empty.rs +++ b/cli/src/rule/footers_empty.rs @@ -1,6 +1,6 @@ -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_rule! { FootersEmpty, } diff --git a/cli/src/rule/mod.rs b/cli/src/rule/mod.rs new file mode 100644 index 0000000..9170dea --- /dev/null +++ b/cli/src/rule/mod.rs @@ -0,0 +1,15 @@ +pub mod body_empty; +pub mod body_max_length; +pub mod description_empty; +pub mod description_format; +pub mod description_max_length; +pub mod footers_empty; +pub mod scope; +pub mod scope_empty; +pub mod scope_format; +pub mod scope_max_length; +pub mod subject_empty; +pub mod r#type; +pub mod type_empty; +pub mod type_format; +pub mod type_max_length; diff --git a/cli/src/rule/scope.rs b/cli/src/rule/scope.rs index cdecc9b..02f1eba 100644 --- a/cli/src/rule/scope.rs +++ b/cli/src/rule/scope.rs @@ -1,5 +1,5 @@ -use super::Level; -use crate::{make_options_rule, message::Message, result::Violation, rule::Rule}; +use crate::rules::Level; +use crate::{make_options_rule, message::Message, result::Violation, rules::Rule}; make_options_rule! { Scope, diff --git a/cli/src/rule/scope_empty.rs b/cli/src/rule/scope_empty.rs index ec553c1..6de29ce 100644 --- a/cli/src/rule/scope_empty.rs +++ b/cli/src/rule/scope_empty.rs @@ -1,5 +1,5 @@ -use super::Level; -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::rules::Level; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; make_rule! { ScopeEmpty, diff --git a/cli/src/rule/scope_format.rs b/cli/src/rule/scope_format.rs index cc2c0ff..f72c65f 100644 --- a/cli/src/rule/scope_format.rs +++ b/cli/src/rule/scope_format.rs @@ -1,6 +1,6 @@ -use crate::{make_format_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_format_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_format_rule! { ScopeFormat, "scope" diff --git a/cli/src/rule/scope_max_length.rs b/cli/src/rule/scope_max_length.rs index ad635f9..fa8b85d 100644 --- a/cli/src/rule/scope_max_length.rs +++ b/cli/src/rule/scope_max_length.rs @@ -1,5 +1,5 @@ -use super::Level; -use crate::{make_length_rule, message::Message, result::Violation, rule::Rule}; +use crate::rules::Level; +use crate::{make_length_rule, message::Message, result::Violation, rules::Rule}; make_length_rule! { ScopeMaxLength, diff --git a/cli/src/rule/subject_empty.rs b/cli/src/rule/subject_empty.rs index f5d6235..7b8a05c 100644 --- a/cli/src/rule/subject_empty.rs +++ b/cli/src/rule/subject_empty.rs @@ -1,6 +1,6 @@ -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_rule! { SubjectEmpty, diff --git a/cli/src/rule/type.rs b/cli/src/rule/type.rs index b4158d3..3ff55a5 100644 --- a/cli/src/rule/type.rs +++ b/cli/src/rule/type.rs @@ -1,6 +1,6 @@ -use crate::{make_options_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_options_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_options_rule! { Type, "type", diff --git a/cli/src/rule/type_empty.rs b/cli/src/rule/type_empty.rs index 31881e0..cf3eb33 100644 --- a/cli/src/rule/type_empty.rs +++ b/cli/src/rule/type_empty.rs @@ -1,6 +1,6 @@ -use crate::{make_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_rule! { TypeEmpty, } diff --git a/cli/src/rule/type_format.rs b/cli/src/rule/type_format.rs index 4f65b2a..b40518d 100644 --- a/cli/src/rule/type_format.rs +++ b/cli/src/rule/type_format.rs @@ -1,6 +1,6 @@ -use crate::{make_format_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_format_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_format_rule! { TypeFormat, "type" diff --git a/cli/src/rule/type_max_length.rs b/cli/src/rule/type_max_length.rs index f7a71fa..05109ff 100644 --- a/cli/src/rule/type_max_length.rs +++ b/cli/src/rule/type_max_length.rs @@ -1,6 +1,6 @@ -use crate::{make_length_rule, message::Message, result::Violation, rule::Rule}; +use crate::{make_length_rule, message::Message, result::Violation, rules::Rule}; -use super::Level; +use crate::rules::Level; make_length_rule! { TypeMaxLength, diff --git a/cli/src/rule.rs b/cli/src/rules.rs similarity index 94% rename from cli/src/rule.rs rename to cli/src/rules.rs index 328f71f..73d70b0 100644 --- a/cli/src/rule.rs +++ b/cli/src/rules.rs @@ -1,22 +1,6 @@ -use crate::{message::Message, result::Violation}; +use crate::{message::Message, result::Violation, rule::*}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; -pub mod body_empty; -pub mod body_max_length; -pub mod description_empty; -pub mod description_format; -pub mod description_max_length; -pub mod footers_empty; -pub mod scope; -pub mod scope_empty; -pub mod scope_format; -pub mod scope_max_length; -pub mod subject_empty; -pub mod r#type; -pub mod type_empty; -pub mod type_format; -pub mod type_max_length; - include!(concat!(env!("OUT_DIR"), "/Rules.rs")); /// Rule is a collection of rules. diff --git a/cli/src/settings.rs b/cli/src/settings.rs index 7d860e6..14078a0 100644 --- a/cli/src/settings.rs +++ b/cli/src/settings.rs @@ -1,4 +1,4 @@ -use crate::rule::Rules; +use crate::rules::Rules; use config::*; use serde::{Deserialize, Serialize}; use std::fmt; From 9cb6ed9bb6d56189a28c25804f5e924848915885 Mon Sep 17 00:00:00 2001 From: elcoosp Date: Tue, 3 Dec 2024 20:02:28 +0100 Subject: [PATCH 9/9] fix: remove boilerplate by using rsmack_fs::folder_iso_struct macro instead of manual call to rsmack_utils::folder_iso_struct --- cli/build.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 0cc8aa8..fd3c411 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,20 +1,12 @@ -use rsmack_utils::*; - +#[rsmack_fs::folder_iso_struct(from_crate = cli, folder = rule)] +#[rsmack_wrap::wrap(with = Option)] +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[serde(rename_all = "kebab-case")] +#[serde_with::apply(Option => #[serde(skip_serializing_if = "Option::is_none")])] +/// Rules represents the rules of commitlint. +/// See: [rules documentation](https://commitlint.js.org/reference/rules.html) +pub struct Rules {} fn main() { - folder_iso_struct() - .name("Rules") - .pre(quote::quote! { - /// Rules represents the rules of commitlint. - /// See: https://commitlint.js.org/reference/rules.html - #[rsmack_wrap::wrap(with = Option)] - #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] - #[serde(rename_all = "kebab-case")] - #[serde_with::apply( - Option => #[serde(skip_serializing_if = "Option::is_none")], - )] - }) - .folder("rule") - .from_crate("cli") - .call(); + Rules::generate(); }