about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-03-03 21:51:32 +0100
committerProfpatsch <mail@profpatsch.de>2024-03-03 22:53:30 +0100
commit91578fe8979ebe87d7302acd09bf66392327a250 (patch)
treedd52eabbfc6278004dec25521beda93e90d5334a
parent5f401726d12fd46bf80da259fb598ba6095f713a (diff)
pkgs/profpatsch/nman: always enumerate all manpages
First step towards getting some info about which manpages are indeed
available if we don’t find any.
-rw-r--r--pkgs/profpatsch/nman/nman.rs36
1 files changed, 19 insertions, 17 deletions
diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs
index 4e3e71dc..36348ea3 100644
--- a/pkgs/profpatsch/nman/nman.rs
+++ b/pkgs/profpatsch/nman/nman.rs
@@ -408,23 +408,16 @@ impl Main {
 
         // expected sub directory of share/man or, if no section
         // is given, all potential sub directories
-        let mut section_dirs: Vec<(OsString, PathBuf)> = match section {
-            Some(s) => {
-                let dir_name = OsString::from(format!("man{}", s));
-                let dir_path = path.join(dir_name.as_os_str());
-
-                if dir_path.exists() {
-                    vec![(dir_name, dir_path)]
-                } else {
-                    Vec::new()
-                }
-            }
-            None => read_dir(path.as_path())
-                .map_err(NmanError::IO)?
-                .filter_map(|entry| entry.ok())
-                .map(|e| (e.file_name(), e.path()))
-                .collect(),
-        };
+        let mut section_dirs: Vec<(OsString, PathBuf)> = Self::enumerate_man_pages(&path)?;
+
+        if let Some(sect) = section {
+            let dir_name = OsString::from(format!("man{}", sect));
+            let dir_path = path.join(dir_name.as_os_str());
+            section_dirs = section_dirs
+                .into_iter()
+                .find(|(_, dp)| dp == &dir_path)
+                .map_or(Vec::new(), |x| vec![x]);
+        }
 
         // sorting should be ascending in terms of numerics,
         // apart from that, not many requirements
@@ -462,6 +455,15 @@ impl Main {
         Ok(OutputDirResult::NoManPageFound)
     }
 
+    fn enumerate_man_pages<'a>(path: &PathBuf) -> Result<Vec<(OsString, PathBuf)>, NmanError<'a>> {
+        Ok(read_dir(path.as_path())
+            .map_err(NmanError::IO)?
+            // ignore directories/files that cannot be read
+            .filter_map(|entry| entry.ok())
+            .map(|e| (e.file_name(), e.path()))
+            .collect())
+    }
+
     /// Realises the given derivation output using `nix-store --realise` and
     /// returns the path to the output directory.
     fn build_drv_with_output<'a>(