grass/dev/public/
discovery.rs

1use crate::dev::{
2    strategy::{
3        alias::{AliasStrategy, SupportsAlias},
4        discovery::{
5            DiscoveryExists, DiscoveryStrategy, DiscoveryStrategyError, SupportsDiscovery,
6        },
7    },
8    Api, Category, RepositoryLocation,
9};
10
11pub fn list_repositories_in_category<T, U, V>(
12    api: &Api<T>,
13    category: U,
14) -> Result<V, DiscoveryStrategyError>
15where
16    T: SupportsDiscovery + SupportsAlias,
17    U: AsRef<str>,
18    V: FromIterator<RepositoryLocation>,
19{
20    let category: Category = api
21        .get_alias_strategy()
22        .resolve_alias(category.as_ref())?
23        .into();
24    let iterator = api
25        .get_discovery_strategy()
26        .list_repositories_in_category(category)?;
27
28    Ok(iterator
29        .filter_map(|value| match value {
30            Ok(value) => Some(value),
31            Err(_) => None,
32        })
33        .collect())
34}
35
36pub fn list_repositories_in_category_with_errors<T, U, V>(
37    api: &Api<T>,
38    category: U,
39) -> Result<V, DiscoveryStrategyError>
40where
41    T: SupportsDiscovery + SupportsAlias,
42    U: AsRef<str>,
43    V: FromIterator<Result<RepositoryLocation, DiscoveryStrategyError>>,
44{
45    let category: Category = api
46        .get_alias_strategy()
47        .resolve_alias(category.as_ref())?
48        .into();
49
50    Ok(api
51        .get_discovery_strategy()
52        .list_repositories_in_category(category)?
53        .collect())
54}
55
56/// List all repositories detected by the API
57///
58/// # Todo:
59///
60/// - [ ] Change it so it doesn't require `SupportsAlias`
61pub fn list_all_repositories<T, U>(api: &Api<T>) -> Result<U, DiscoveryStrategyError>
62where
63    T: SupportsDiscovery + SupportsAlias,
64    U: FromIterator<RepositoryLocation>,
65{
66    let categories: Vec<_> = list_categories(api)?;
67    Ok(categories
68        .iter()
69        .filter_map(|category| list_repositories_in_category::<_, _, Vec<_>>(api, category).ok())
70        .flatten()
71        .collect())
72}
73
74pub fn list_categories<T: SupportsDiscovery, U: FromIterator<String>>(
75    api: &Api<T>,
76) -> Result<U, DiscoveryStrategyError> {
77    api.get_discovery_strategy().list_categories()
78}
79
80/// Returns whether or not a repository exists
81///
82/// # Todo:
83///
84/// - [ ] Return a generic crate wide error
85///       (<https://github.com/damymetzke/grass/issues/2>)
86///
87/// # Example
88///
89/// ```rust
90/// # use grass::dev::{
91/// #     self,
92/// #     strategy::{
93/// #         alias::SupportsAlias,
94/// #         api::MockApiStrategy,
95/// #         discovery::{DiscoveryExists, SupportsDiscovery},
96/// #     },
97/// #     Api,
98/// # };
99/// # let api = Api::from(MockApiStrategy::default());
100/// #
101/// fn test_public<T: SupportsDiscovery + SupportsAlias>(api: &Api<T>) {
102///     assert_eq!(
103///         dev::verify_repository_exists(api, ("all_good", "first")).unwrap(),
104///         DiscoveryExists::Exists
105///     )
106/// }
107///
108/// test_public(&api)
109/// ```
110pub fn verify_repository_exists<T, U>(
111    api: &Api<T>,
112    repository: U,
113) -> Result<DiscoveryExists, DiscoveryStrategyError>
114where
115    T: SupportsDiscovery + SupportsAlias,
116    U: Into<RepositoryLocation>,
117{
118    let result = api
119        .get_discovery_strategy()
120        .check_repository_exists(api.get_alias_strategy().resolve_alias(repository.into())?)?;
121
122    Ok(result)
123}
124
125pub fn create_repository<T, U>(api: &Api<T>, location: U) -> Result<(), DiscoveryStrategyError>
126where
127    T: SupportsDiscovery + SupportsAlias,
128    U: Into<RepositoryLocation>,
129{
130    let alias = api.get_alias_strategy();
131    let discovery = api.get_discovery_strategy();
132
133    let location = alias.resolve_alias(location.into())?;
134    discovery.create_repository(location)?;
135
136    Ok(())
137}
138
139pub fn move_repository<T, U, V>(
140    api: &Api<T>,
141    old_location: U,
142    new_location: V,
143) -> Result<(), DiscoveryStrategyError>
144where
145    T: SupportsDiscovery + SupportsAlias,
146    U: Into<RepositoryLocation>,
147    V: Into<RepositoryLocation>,
148{
149    let alias = api.get_alias_strategy();
150    let discovery = api.get_discovery_strategy();
151
152    let old_location = alias.resolve_alias(old_location.into())?;
153    let new_location = alias.resolve_alias(new_location.into())?;
154    discovery.move_repository(old_location, new_location)?;
155
156    Ok(())
157}