grass/dev/strategy/path/
local.rs

1use std::path::PathBuf;
2
3use crate::dev::{config::GrassConfig, strategy::path::PathStrategyError, RepositoryLocation};
4
5use super::PathStrategy;
6
7pub struct LocalPathStrategy<'a> {
8    config: &'a GrassConfig,
9}
10
11impl<'a> LocalPathStrategy<'a> {
12    pub fn new(config: &'a GrassConfig) -> Self {
13        LocalPathStrategy { config }
14    }
15}
16
17impl<'a> PathStrategy for LocalPathStrategy<'a> {
18    fn get_containing_directory<T>(&self, repository: T) -> super::Result<PathBuf>
19    where
20        T: Into<RepositoryLocation>,
21    {
22        let RepositoryLocation { category, .. } = repository.into();
23
24        let result = match self.config.get_by_category(category) {
25            Some(category) => self.config.base_dir.join(&category.name),
26            None => {
27                return Err(PathStrategyError::RepositoryNotFound {
28                    context: "When getting the category from configuration.".into(),
29                    reason: "Category not defined.".into(),
30                })
31            }
32        };
33        Ok(result)
34    }
35
36    fn get_directory<T>(&self, repository: T) -> super::Result<PathBuf>
37    where
38        T: Into<RepositoryLocation>,
39    {
40        let repository: RepositoryLocation = repository.into();
41        let category_directory = self.get_containing_directory(repository.clone())?;
42
43        let RepositoryLocation { repository, .. } = repository;
44
45        Ok(category_directory.join(repository))
46    }
47}