about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-03-03 15:32:17 +0100
committerProfpatsch <mail@profpatsch.de>2024-03-03 15:33:01 +0100
commit6e6ca6d7222d9b5e5474955a129ec520bd0d2b26 (patch)
tree74bc831aa22357322caffc02b7c73ac4c01e4480
parentcd67dc67cdf3f7bdb4a74c4dadb27ff75efca333 (diff)
pkgs/profpatsch/nman: move main to top
-rw-r--r--pkgs/profpatsch/nman/nman.rs138
1 files 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<String>, Vec<String>) =
+            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<String>, Vec<String>) =
-            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::*;