use std::path::Path;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WikiLocation<'a> {
Local {
path: &'a Path,
display_path: &'a Path,
},
Untitled,
}
impl<'a> WikiLocation<'a> {
pub fn path(self) -> Option<&'a Path> {
match self {
Self::Local { path, .. } => Some(path),
Self::Untitled => None,
}
}
pub fn display_path(self) -> Option<&'a Path> {
match self {
Self::Local { display_path, .. } => Some(display_path),
Self::Untitled => None,
}
}
}
pub fn relative_path<'a>(base_directory: &Path, path: &'a Path) -> &'a Path {
path.strip_prefix(base_directory).unwrap_or(path)
}
#[cfg(test)]
mod tests {
use super::relative_path;
use std::path::Path;
#[test]
fn contained_path() {
assert_eq!(
relative_path(Path::new("/notes"), Path::new("/notes/archive/file.txt")),
Path::new("archive/file.txt"),
);
}
#[test]
fn outside_path() {
assert_eq!(
relative_path(Path::new("/notes"), Path::new("/other/file.txt")),
Path::new("/other/file.txt"),
);
}
}