From 790ea414c3c190ab7a42c3b616a0dc2a7ab7f82e Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 13 Nov 2025 00:54:48 +0300 Subject: [PATCH] eh: clean up dispatch logic Signed-off-by: NotAShelf Change-Id: I63671355750a3c839f71d9155b8c918d6a6a6964 --- eh/src/command.rs | 9 +++++--- eh/src/error.rs | 3 ++- eh/src/main.rs | 57 ++++++++++++++++++++++++++--------------------- eh/src/util.rs | 21 ++++++++--------- 4 files changed, 51 insertions(+), 39 deletions(-) diff --git a/eh/src/command.rs b/eh/src/command.rs index 2f1fb8d..4e314fd 100644 --- a/eh/src/command.rs +++ b/eh/src/command.rs @@ -62,17 +62,20 @@ impl NixCommand { self } - #[must_use] pub const fn impure(mut self, yes: bool) -> Self { + #[must_use] + pub const fn impure(mut self, yes: bool) -> Self { self.impure = yes; self } - #[must_use] pub const fn interactive(mut self, yes: bool) -> Self { + #[must_use] + pub const fn interactive(mut self, yes: bool) -> Self { self.interactive = yes; self } - #[must_use] pub const fn print_build_logs(mut self, yes: bool) -> Self { + #[must_use] + pub const fn print_build_logs(mut self, yes: bool) -> Self { self.print_build_logs = yes; self } diff --git a/eh/src/error.rs b/eh/src/error.rs index 5cb90fb..47296ba 100644 --- a/eh/src/error.rs +++ b/eh/src/error.rs @@ -33,7 +33,8 @@ pub enum EhError { pub type Result = std::result::Result; impl EhError { - #[must_use] pub const fn exit_code(&self) -> i32 { + #[must_use] + pub const fn exit_code(&self) -> i32 { match self { Self::ProcessExit { code } => *code, Self::NixCommandFailed(_) => 1, diff --git a/eh/src/main.rs b/eh/src/main.rs index e5363b0..0cfba89 100644 --- a/eh/src/main.rs +++ b/eh/src/main.rs @@ -30,6 +30,36 @@ fn main() { } } +// Design partially taken from Stash +fn dispatch_multicall(app_name: &str, args: std::env::Args) -> Option> { + let rest: Vec = args.collect(); + let hash_extractor = util::RegexHashExtractor; + let fixer = util::DefaultNixFileFixer; + let classifier = util::DefaultNixErrorClassifier; + + match app_name { + "nr" => Some(run::handle_nix_run( + &rest, + &hash_extractor, + &fixer, + &classifier, + )), + "ns" => Some(shell::handle_nix_shell( + &rest, + &hash_extractor, + &fixer, + &classifier, + )), + "nb" => Some(build::handle_nix_build( + &rest, + &hash_extractor, + &fixer, + &classifier, + )), + _ => None, + } +} + fn run_app() -> Result { let mut args = env::args(); let bin = args.next().unwrap_or_else(|| "eh".to_string()); @@ -39,31 +69,8 @@ fn run_app() -> Result { .unwrap_or("eh"); // If invoked as nr/ns/nb, dispatch directly and exit - match app_name { - "nr" => { - let rest: Vec = args.collect(); - let hash_extractor = util::RegexHashExtractor; - let fixer = util::DefaultNixFileFixer; - let classifier = util::DefaultNixErrorClassifier; - return run::handle_nix_run(&rest, &hash_extractor, &fixer, &classifier); - } - - "ns" => { - let rest: Vec = args.collect(); - let hash_extractor = util::RegexHashExtractor; - let fixer = util::DefaultNixFileFixer; - let classifier = util::DefaultNixErrorClassifier; - return shell::handle_nix_shell(&rest, &hash_extractor, &fixer, &classifier); - } - - "nb" => { - let rest: Vec = args.collect(); - let hash_extractor = util::RegexHashExtractor; - let fixer = util::DefaultNixFileFixer; - let classifier = util::DefaultNixErrorClassifier; - return build::handle_nix_build(&rest, &hash_extractor, &fixer, &classifier); - } - _ => {} + if let Some(result) = dispatch_multicall(app_name, args) { + return result; } let cli = Cli::parse(); diff --git a/eh/src/util.rs b/eh/src/util.rs index e93e417..d7cb5ae 100644 --- a/eh/src/util.rs +++ b/eh/src/util.rs @@ -23,9 +23,10 @@ impl HashExtractor for RegexHashExtractor { for pattern in &patterns { if let Ok(re) = Regex::new(pattern) && let Some(captures) = re.captures(stderr) - && let Some(hash) = captures.get(1) { - return Some(hash.as_str().to_string()); - } + && let Some(hash) = captures.get(1) + { + return Some(hash.as_str().to_string()); + } } None } @@ -62,9 +63,10 @@ impl NixFileFixer for DefaultNixFileFixer { if path.is_dir() { stack.push(path); } else if let Some(ext) = path.extension() - && ext.eq_ignore_ascii_case("nix") { - files.push(path); - } + && ext.eq_ignore_ascii_case("nix") + { + files.push(path); + } } } if files.is_empty() { @@ -99,10 +101,9 @@ impl NixFileFixer for DefaultNixFileFixer { } } if replaced { - fs::write(file_path, new_content) - .map_err(|_| EhError::HashFixFailed { - path: file_path.to_string_lossy().to_string() - })?; + fs::write(file_path, new_content).map_err(|_| EhError::HashFixFailed { + path: file_path.to_string_lossy().to_string(), + })?; Ok(true) } else { Ok(false)