feat(groups): allow nested group dirs

Instead of expecting all files to be located immediately under the group
dir, we allow them to be nested in additional folders. We walk the group
folder, and treat every file as a group file. The relative path of the
file as seen from the group dir becomes the group name.

Fixes #27.
This commit is contained in:
steven-omaha
2023-05-18 17:16:30 +02:00
parent ab913b4721
commit 0ffe0c345d
4 changed files with 122 additions and 13 deletions
+44 -1
View File
@@ -2,8 +2,8 @@
All functions related to `pacdef`'s internal paths.
*/
use std::env;
use std::path::PathBuf;
use std::{env, path::Path};
use anyhow::{Context, Result};
@@ -99,3 +99,46 @@ pub(crate) fn binary_in_path(name: &str) -> Result<bool> {
}
Ok(false)
}
/// Determine the relative path of `full_path` in relation to `base_path`.
///
/// # Panics
///
/// Panics if at least one element in `base_path` does not match the corresponding
/// element in `full_path`.
pub(crate) fn get_relative_path<P>(full_path: P, base_path: P) -> PathBuf
where
P: AsRef<Path>,
{
let mut file_iter = full_path.as_ref().iter();
base_path
.as_ref()
.iter()
.zip(&mut file_iter)
.for_each(|(a, b)| assert_eq!(a, b));
let relative_path: PathBuf = file_iter.collect();
relative_path
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::get_relative_path;
#[test]
fn relative_path() {
let full = PathBuf::from("/a/b/c/d/e");
let base = PathBuf::from("/a/b/c");
let relative = get_relative_path(full, base);
assert_eq!(relative, PathBuf::from("d/e"));
}
#[test]
#[should_panic]
fn relative_path_panic() {
let full = PathBuf::from("/a/b/z/d/e");
let base = PathBuf::from("/a/b/c");
get_relative_path(full, base);
}
}