archunit/common/extraction/
project_locator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::path::{Path, PathBuf};

/// Where Cargo project discovery begins.
///
/// The default starts at the process working directory. An explicit path may name a directory
/// inside a project or its `Cargo.toml` manifest.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct ProjectLocator {
    path: Option<PathBuf>,
}

impl ProjectLocator {
    /// Selects automatic discovery from the current working directory.
    #[must_use]
    pub const fn auto_detect() -> Self {
        Self { path: None }
    }

    /// Starts discovery at an explicit directory or `Cargo.toml` manifest.
    #[must_use]
    pub fn from_path(path: impl Into<PathBuf>) -> Self {
        Self {
            path: Some(path.into()),
        }
    }

    /// Returns the explicit starting path, or `None` for automatic discovery.
    #[must_use]
    pub fn path(&self) -> Option<&Path> {
        self.path.as_deref()
    }
}

impl From<PathBuf> for ProjectLocator {
    fn from(path: PathBuf) -> Self {
        Self::from_path(path)
    }
}

impl From<&Path> for ProjectLocator {
    fn from(path: &Path) -> Self {
        Self::from_path(path)
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::ProjectLocator;

    #[test]
    fn default_locator_requests_auto_detection() {
        assert!(ProjectLocator::default().path().is_none());
        assert!(ProjectLocator::auto_detect().path().is_none());
    }

    #[test]
    fn explicit_locator_owns_the_user_path() {
        let locator = ProjectLocator::from_path("crates/app");

        assert_eq!(locator.path(), Some(Path::new("crates/app")));
    }
}