diff --git a/.config/tracey/config.styx b/.config/tracey/config.styx index afcdda00..d2923b57 100644 --- a/.config/tracey/config.styx +++ b/.config/tracey/config.styx @@ -1,7 +1,19 @@ specs ( { - name bough - include ( **/*SPEC*.md ) + name bough-core + include ( crates/bough-core/**/*SPEC*.md ) + impls ( + { + name rust + include (crates/**/*.rs) + exclude (target/**) + } + ) + } + + { + name bough-cli + include ( crates/bough-cli/**/*SPEC*.md ) impls ( { name rust diff --git a/Cargo.lock b/Cargo.lock index 94ec6f09..8da080e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "addr2line" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aho-corasick" version = "1.1.4" @@ -38,7 +53,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8454c8a44ce2cb9cc7e7fae67fc6128465b343b92c6631e94beca3c8d1524ea5" dependencies = [ - "unicode-width", + "unicode-width 0.2.2", "yansi", ] @@ -54,6 +69,30 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "backtrace" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-link", +] + +[[package]] +name = "backtrace-ext" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50" +dependencies = [ + "backtrace", +] + [[package]] name = "bitflags" version = "2.11.0" @@ -78,6 +117,8 @@ dependencies = [ "facet-toml", "facet-yaml", "figue", + "miette", + "thiserror", "tracing", "tracing-subscriber", ] @@ -238,7 +279,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -467,7 +508,7 @@ dependencies = [ "strsim", "supports-color 3.0.2", "tracing", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -521,6 +562,12 @@ dependencies = [ "wasip3", ] +[[package]] +name = "gimli" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" + [[package]] name = "glob" version = "0.3.3" @@ -637,7 +684,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -698,6 +745,45 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "backtrace", + "backtrace-ext", + "cfg-if", + "miette-derive", + "owo-colors", + "supports-color 3.0.2", + "supports-hyperlinks", + "supports-unicode", + "terminal_size", + "textwrap", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + [[package]] name = "mutants" version = "0.0.3" @@ -710,7 +796,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -722,6 +808,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.37.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -824,6 +919,12 @@ version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" +[[package]] +name = "rustc-demangle" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b50b8869d9fc858ce7266cce0194bd74df58b9d0e3f6df3a9fc8eb470d95c09d" + [[package]] name = "rustc-hash" version = "2.1.1" @@ -840,7 +941,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -995,6 +1096,18 @@ dependencies = [ "is_ci", ] +[[package]] +name = "supports-hyperlinks" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e396b6523b11ccb83120b115a0b7366de372751aa6edf19844dfb13a6af97e91" + +[[package]] +name = "supports-unicode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" + [[package]] name = "syn" version = "2.0.117" @@ -1016,7 +1129,47 @@ dependencies = [ "getrandom", "once_cell", "rustix", - "windows-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "terminal_size" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b8cb979cb11c32ce1603f8137b22262a9d131aaa5c37b5678025f22b8becd0" +dependencies = [ + "rustix", + "windows-sys 0.60.2", +] + +[[package]] +name = "textwrap" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" +dependencies = [ + "unicode-linebreak", + "unicode-width 0.2.2", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1146,6 +1299,18 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.2" @@ -1322,7 +1487,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -1384,6 +1549,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.61.2" @@ -1393,6 +1567,71 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + [[package]] name = "winnow" version = "0.7.14" diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index b7c2d03f..60fb4d2d 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -7,6 +7,8 @@ license.workspace = true [dependencies] bough-core = { path = "../bough-core" } facet = "0.44" +miette = { version = "7", features = ["fancy"] } +thiserror = "2" facet-toml = "0.44" facet-yaml = "0.44" figue = "2" diff --git a/crates/bough-cli/Config_SPEC.md b/crates/bough-cli/Config_SPEC.md new file mode 100644 index 00000000..dfb54252 --- /dev/null +++ b/crates/bough-cli/Config_SPEC.md @@ -0,0 +1,4 @@ +## Config + +cli[config.include.at-least-one] +cli must have at least 1 include path diff --git a/crates/bough-cli/src/config.rs b/crates/bough-cli/src/config.rs index dcc53be8..daddfc71 100644 --- a/crates/bough-cli/src/config.rs +++ b/crates/bough-cli/src/config.rs @@ -2,6 +2,8 @@ use std::env; use facet::Facet; use figue::{self as args, ConfigFormat, ConfigFormatError, Driver, builder}; +use miette::Diagnostic; +use thiserror::Error; #[derive(Facet, Debug)] pub struct Cli { @@ -35,13 +37,41 @@ pub enum ShowCommand { #[derive(Facet, Debug)] pub struct Config { - #[facet(default = 4)] + #[facet(default = 1)] pub workers: u64, + #[facet(default = 1)] + pub threads: u64, + pub include: Vec, + pub exclude: Vec, } +#[derive(Debug, Clone, Error, Diagnostic)] +pub enum Error { + #[error("config.include must not be empty")] + #[diagnostic( + code(bough::config::empty_include), + help("add at least one include glob pattern") + )] + EmptyInclude, + + #[error("{0}")] + #[diagnostic(code(bough::config::parse))] + Parse(String), +} + +impl Cli { + pub fn validate(&self) -> Vec { + let mut errors = Vec::new(); + if self.config.include.is_empty() { + errors.push(Error::EmptyInclude); + } + errors + } +} + struct TomlFormat; impl ConfigFormat for TomlFormat { @@ -128,7 +158,177 @@ pub fn parse() -> Cli { } }; output.print_warnings(); - output.get_silent() + let cli = output.get_silent(); + + let errors = cli.validate(); + if !errors.is_empty() { + for error in &errors { + eprintln!("{:?}", miette::Report::new_boxed(Box::new(error.clone()))); + } + std::process::exit(1); + } + + cli +} + +#[cfg(test)] +mod tests { + use super::*; + + const MINIMAL_TOML: &str = r#" +include = ["src/**"] +exclude = [] +"#; + + const FULL_TOML: &str = r#" +workers = 16 +include = ["src/**", "lib/**"] +exclude = ["target/**"] +"#; + + pub fn try_parse_from( + args: &[&str], + config_content: Option<(&str, &str)>, + ) -> Result> { + let b = builder::() + .expect("schema should be valid") + .cli(|cli| cli.args(args.iter().map(|s| s.to_string()))); + + let b = match config_content { + Some((content, filename)) => b.file(|f| { + f.content(content, filename) + .format(TomlFormat) + .format(YamlFormat) + }), + None => b, + }; + + let config = b.build(); + let cli: Cli = Driver::new(config) + .run() + .into_result() + .map_err(|e| vec![Error::Parse(format!("{e:?}"))])? + .get_silent(); + + let errors = cli.validate(); + if !errors.is_empty() { + return Err(errors); + } + + Ok(cli) + } + + fn parse_ok(args: &[&str], toml: &str) -> Cli { + try_parse_from(args, Some((toml, "config.toml"))).expect("should parse") + } + + fn parse_err(args: &[&str], toml: &str) -> Vec { + try_parse_from(args, Some((toml, "config.toml"))).expect_err("should fail") + } + + #[test] + fn defaults_with_minimal_config() { + let cli = parse_ok(&["run"], MINIMAL_TOML); + assert_eq!(cli.config.workers, 1); + assert_eq!(cli.config.include, vec!["src/**"]); + assert!(cli.config.exclude.is_empty()); + assert_eq!(cli.verbose, 0); + } + + #[test] + fn full_config() { + let cli = parse_ok(&["run"], FULL_TOML); + assert_eq!(cli.config.workers, 16); + assert_eq!(cli.config.include, vec!["src/**", "lib/**"]); + assert_eq!(cli.config.exclude, vec!["target/**"]); + } + + #[test] + fn cli_overrides_file() { + let cli = parse_ok(&["run", "--config.workers", "32"], FULL_TOML); + assert_eq!(cli.config.workers, 32); + assert_eq!(cli.config.include, vec!["src/**", "lib/**"]); + } + + #[test] + fn verbose_counted() { + let cli = parse_ok(&["-vvv", "run"], MINIMAL_TOML); + assert_eq!(cli.verbose, 3); + } + + #[test] + fn show_config_subcommand() { + let cli = parse_ok(&["show", "config"], MINIMAL_TOML); + assert!(matches!( + cli.command, + Command::Show { + what: ShowCommand::Config + } + )); + } + + #[test] + fn show_file_subcommand() { + let cli = parse_ok(&["show", "file"], MINIMAL_TOML); + assert!(matches!( + cli.command, + Command::Show { + what: ShowCommand::File + } + )); + } + + #[test] + fn show_cli_subcommand() { + let cli = parse_ok(&["show", "cli"], MINIMAL_TOML); + assert!(matches!( + cli.command, + Command::Show { + what: ShowCommand::Cli + } + )); + } + + #[test] + fn run_subcommand() { + let cli = parse_ok(&["run"], MINIMAL_TOML); + assert!(matches!(cli.command, Command::Run)); + } + + #[test] + fn empty_include_fails_validation() { + let toml = r#" +include = [] +exclude = [] +"#; + let errors = parse_err(&["run"], toml); + assert_eq!(errors.len(), 1); + assert!(matches!(errors[0], Error::EmptyInclude)); + } + + #[test] + fn missing_include_fails_parse() { + let toml = r#" +exclude = [] +"#; + let errors = parse_err(&["run"], toml); + assert_eq!(errors.len(), 1); + assert!(matches!(errors[0], Error::Parse(_))); + } + + #[test] + fn json_config() { + let json = r#"{"include": ["src/**"], "exclude": []}"#; + let cli = try_parse_from(&["run"], Some((json, "config.json"))).expect("should parse"); + assert_eq!(cli.config.include, vec!["src/**"]); + } + + #[test] + fn yaml_config() { + let yaml = "include:\n - \"src/**\"\nexclude: []\n"; + let cli = try_parse_from(&["run"], Some((yaml, "config.yaml"))).expect("should parse"); + assert_eq!(cli.config.include, vec!["src/**"]); + } } impl bough_core::Config for Config {