grass/dev/strategy/path/
mock.rs

1use std::path::PathBuf;
2
3use crate::dev::RepositoryLocation;
4
5use super::{PathStrategy, PathStrategyError};
6
7/// A mocked implementation of the path strategy
8///
9/// TODO: Get a central documentation for mocked data
10#[derive(Debug, Default)]
11pub struct MockPathStrategy;
12
13impl PathStrategy for MockPathStrategy {
14    fn get_containing_directory<T>(&self, repository: T) -> super::Result<std::path::PathBuf>
15    where
16        T: Into<crate::dev::RepositoryLocation>,
17    {
18        let repository: RepositoryLocation = repository.into();
19        let repository = (repository.category.as_ref(), repository.repository.as_str());
20        match repository {
21            ("all_good", _) => Ok(PathBuf::from("/home/example/repositories/all_good")),
22            ("with_changes", _) => Ok(PathBuf::from("/home/example/repositories/with_changes")),
23            ("with_error", _) => Ok(PathBuf::from("/home/example/repositories/with_error")),
24            _ => Err(PathStrategyError::RepositoryNotFound {
25                context: "When mocking".into(),
26                reason: "Category not found".into(),
27            }),
28        }
29    }
30
31    fn get_directory<T>(&self, repository: T) -> super::Result<std::path::PathBuf>
32    where
33        T: Into<crate::dev::RepositoryLocation>,
34    {
35        let repository: RepositoryLocation = repository.into();
36        let repository = (repository.category.as_ref(), repository.repository.as_str());
37        match repository {
38            ("all_good", "first") => Ok(PathBuf::from("/home/example/repositories/all_good/first")),
39            ("all_good", "second") => {
40                Ok(PathBuf::from("/home/example/repositories/all_good/second"))
41            }
42            ("all_good", "third") => Ok(PathBuf::from("/home/example/repositories/all_good/third")),
43            ("with_changes", "first") => Ok(PathBuf::from(
44                "/home/example/repositories/with_changes/first",
45            )),
46            ("with_changes", "second") => Ok(PathBuf::from(
47                "/home/example/repositories/with_changes/second",
48            )),
49            ("with_changes", "third") => Ok(PathBuf::from(
50                "/home/example/repositories/with_changes/third",
51            )),
52            ("with_error", "first") => {
53                Ok(PathBuf::from("/home/example/repositories/with_error/first"))
54            }
55            ("with_error", "second") => Err(PathStrategyError::Unknown {
56                context: "When mocking".into(),
57                reason: "Insufficient permissions".into(),
58            }),
59            ("all_good" | "with_changes" | "with_error", _) => {
60                Err(PathStrategyError::RepositoryNotFound {
61                    context: "When mocking".into(),
62                    reason: "Repository not found".into(),
63                })
64            }
65            _ => Err(PathStrategyError::RepositoryNotFound {
66                context: "When mocking".into(),
67                reason: "Category not found".into(),
68            }),
69        }
70    }
71}