ephemeris_files/path/
repository_root.rs1use std::path::{Path, PathBuf};
2
3use super::{PathError, RepositoryPath};
4use crate::FilesError;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct RepositoryRoot(PathBuf);
12
13impl RepositoryRoot {
14 pub fn new(path: &Path) -> Result<Self, PathError> {
19 path.canonicalize()
20 .map(Self)
21 .map_err(|source| PathError::Io {
22 path: path.to_path_buf(),
23 source,
24 })
25 }
26
27 pub fn discover(start: &Path) -> Result<Self, FilesError> {
34 let repository = git2::Repository::discover(start).map_err(|source| FilesError::Open {
35 path: start.to_path_buf(),
36 source,
37 })?;
38 let work_tree = repository.workdir().ok_or(FilesError::NoWorktree)?;
39 Ok(Self::new(work_tree)?)
40 }
41
42 #[must_use]
44 pub fn as_path(&self) -> &Path {
45 &self.0
46 }
47
48 pub fn relative(&self, path: &Path) -> Result<RepositoryPath, PathError> {
54 let relative = path
55 .strip_prefix(&self.0)
56 .map_err(|_outside| PathError::Outside(path.to_path_buf()))?;
57 RepositoryPath::try_from(relative)
58 }
59}