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
use std::path::PathBuf;

use crate::dev::{config::GrassConfig, strategy::path::PathStrategyError, RepositoryLocation};

use super::PathStrategy;

pub struct LocalPathStrategy<'a> {
    config: &'a GrassConfig,
}

impl<'a> LocalPathStrategy<'a> {
    pub fn new(config: &'a GrassConfig) -> Self {
        LocalPathStrategy { config }
    }
}

impl<'a> PathStrategy for LocalPathStrategy<'a> {
    fn get_containing_directory<T>(&self, repository: T) -> super::Result<PathBuf>
    where
        T: Into<RepositoryLocation>,
    {
        let RepositoryLocation { category, .. } = repository.into();

        let result = match self.config.get_by_category(category) {
            Some(category) => self.config.base_dir.join(&category.name),
            None => {
                return Err(PathStrategyError::RepositoryNotFound {
                    context: "When getting the category from configuration.".into(),
                    reason: "Category not defined.".into(),
                })
            }
        };
        Ok(result)
    }

    fn get_directory<T>(&self, repository: T) -> super::Result<PathBuf>
    where
        T: Into<RepositoryLocation>,
    {
        let repository: RepositoryLocation = repository.into();
        let category_directory = self.get_containing_directory(repository.clone())?;

        let RepositoryLocation { repository, .. } = repository;

        Ok(category_directory.join(repository))
    }
}