From 6e6ca6d7222d9b5e5474955a129ec520bd0d2b26 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 3 Mar 2024 15:32:17 +0100 Subject: pkgs/profpatsch/nman: move main to top --- pkgs/profpatsch/nman/nman.rs | 138 ++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs index e11a74f8..401d9c8a 100644 --- a/pkgs/profpatsch/nman/nman.rs +++ b/pkgs/profpatsch/nman/nman.rs @@ -8,17 +8,69 @@ use std::path::PathBuf; use std::process::{Stdio, ExitStatus, Command}; use temp::TempDir; -/// Pretty print an [`ExitStatus`] -fn pretty_exit_status(status: &ExitStatus) -> String { - match status.code() { - Some(i) => format!("exited with {}", i), - None => match status.signal() { - Some(s) => format!("was killed by signal {}", s), - None => String::from("exited for unknown reason"), + +enum CliAction<'a> { + /// attribute, section, page + Man(&'a str, Option<&'a str>, &'a str), +} + +enum CliResult<'a> { + ShowUsage{err_msg: Option<&'a str>}, + Action(CliAction<'a>), +} + +fn main() { + use CliResult::*; + let (opts, args) : (Vec, Vec) = + std::env::args().partition(|s| s.starts_with("-")); + + let mut cli_res : CliResult = match args.len() { + 2 => Action(CliAction::Man(&args[1], None, &args[1])), + 3 => match parse_man_section(&args[2]) { + Ok(s) => Action(CliAction::Man(&args[1], Some(s), &args[1])), + Err(_) => Action(CliAction::Man(&args[1], None, &args[2])), + }, + 4 => match parse_man_section(&args[2]) { + Err(err_msg) => ShowUsage{err_msg: Some(err_msg)}, + Ok(s) => Action(CliAction::Man(&args[1], Some(s), &args[3])), + } + _ => ShowUsage { err_msg: Some("Unexpected number of arguments") }, + }; + + for opt in opts { + match &opt[..] { + "--help" | "--usage" | "-h" => + cli_res = ShowUsage{err_msg: None}, + _ => cli_res = ShowUsage{err_msg: Some("Unknown option")}, + } + } + + match cli_res { + ShowUsage{err_msg} => { + if let Some(msg) = err_msg { + eprintln!("usage error: {}", msg); + } + println!("Usage: {} ATTR [PAGE | SECTION [PAGE]]", &args[0]); + std::process::exit(NmanError::Usage.code()); + }, + Action(action) => match action { + CliAction::Man(attr, section, page) => + match open_man_page(attr, section, page) { + Ok(_) => (), + Err(t) => { + let msg = t.msg(); + eprint!("error: {}", msg); + if !msg.ends_with("\n") { + eprint!("\n"); + } + std::process::exit(t.code()) + }, + }, } } } + /// Represents all errors that can occurr in `nman`. /// The inner structure of this type is rather messy /// as it is highly specific to the location it may @@ -75,6 +127,17 @@ impl NmanError<'_> { } } +/// Pretty print an [`ExitStatus`] +fn pretty_exit_status(status: &ExitStatus) -> String { + match status.code() { + Some(i) => format!("exited with {}", i), + None => match status.signal() { + Some(s) => format!("was killed by signal {}", s), + None => String::from("exited for unknown reason"), + } + } +} + /// Represents an output of a Nix derivation. /// These can theoretically be any strings, /// but are limited to the first 9 options @@ -367,67 +430,6 @@ fn parse_man_section(section: &str) -> Result<&str, &str> { } } -enum CliAction<'a> { - /// attribute, section, page - Man(&'a str, Option<&'a str>, &'a str), -} - -enum CliResult<'a> { - ShowUsage{err_msg: Option<&'a str>}, - Action(CliAction<'a>), -} - -fn main() { - use CliResult::*; - let (opts, args) : (Vec, Vec) = - std::env::args().partition(|s| s.starts_with("-")); - - let mut cli_res : CliResult = match args.len() { - 2 => Action(CliAction::Man(&args[1], None, &args[1])), - 3 => match parse_man_section(&args[2]) { - Ok(s) => Action(CliAction::Man(&args[1], Some(s), &args[1])), - Err(_) => Action(CliAction::Man(&args[1], None, &args[2])), - }, - 4 => match parse_man_section(&args[2]) { - Err(err_msg) => ShowUsage{err_msg: Some(err_msg)}, - Ok(s) => Action(CliAction::Man(&args[1], Some(s), &args[3])), - } - _ => ShowUsage { err_msg: Some("Unexpected number of arguments") }, - }; - - for opt in opts { - match &opt[..] { - "--help" | "--usage" | "-h" => - cli_res = ShowUsage{err_msg: None}, - _ => cli_res = ShowUsage{err_msg: Some("Unknown option")}, - } - } - - match cli_res { - ShowUsage{err_msg} => { - if let Some(msg) = err_msg { - eprintln!("usage error: {}", msg); - } - println!("Usage: {} ATTR [PAGE | SECTION [PAGE]]", &args[0]); - std::process::exit(NmanError::Usage.code()); - }, - Action(action) => match action { - CliAction::Man(attr, section, page) => - match open_man_page(attr, section, page) { - Ok(_) => (), - Err(t) => { - let msg = t.msg(); - eprint!("error: {}", msg); - if !msg.ends_with("\n") { - eprint!("\n"); - } - std::process::exit(t.code()) - }, - }, - } - } -} - #[cfg(test)] mod tests { use super::*; -- cgit 1.4.1