mithril_client_cli/utils/
expander.rs

1use anyhow::anyhow;
2use futures::Future;
3use mithril_client::MithrilResult;
4
5/// Utilities to expand aliases into their associated ids.
6pub struct ExpanderUtils;
7
8impl ExpanderUtils {
9    /// Expands an id alias to the latest id.
10    /// If the provided id is "latest", retrieves the list of ids using the given future and returns the first one.
11    /// Otherwise, returns the provided id as is.
12    pub async fn expand_eventual_id_alias(
13        id: &str,
14        get_list_of_ids: impl Future<Output = MithrilResult<Vec<String>>>,
15    ) -> MithrilResult<String> {
16        if id.to_lowercase() == "latest" {
17            let list = get_list_of_ids.await?;
18            let last_element = list.first().ok_or_else(|| anyhow!("Entity not found"))?;
19            Ok(last_element.to_string())
20        } else {
21            Ok(id.to_string())
22        }
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    async fn get_list_of_ids(ids: Vec<&str>) -> MithrilResult<Vec<String>> {
31        Ok(ids.iter().map(|h| h.to_string()).collect::<Vec<_>>())
32    }
33
34    #[tokio::test]
35    async fn expand_eventual_id_alias_should_returns_id() {
36        let hash = ExpanderUtils::expand_eventual_id_alias(
37            "hash-234",
38            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
39        )
40        .await
41        .expect("expand_eventual_id_alias should not error when hash-234 is passed as parameter.");
42
43        assert_eq!("hash-234", hash);
44    }
45
46    #[tokio::test]
47    async fn expand_eventual_id_alias_latest_lowercase() {
48        let hash = ExpanderUtils::expand_eventual_id_alias(
49            "latest",
50            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
51        )
52        .await
53        .expect("expand_eventual_id_alias should not error when latest is passed as parameter.");
54
55        assert_eq!("hash-123".to_string(), hash);
56    }
57
58    #[tokio::test]
59    async fn expand_eventual_id_alias_latest_uppercase() {
60        let hash = ExpanderUtils::expand_eventual_id_alias(
61            "LATEST",
62            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
63        )
64        .await
65        .expect("expand_eventual_id_alias should not error when latest is passed as parameter.");
66
67        assert_eq!("hash-123".to_string(), hash);
68    }
69
70    #[tokio::test]
71    async fn expand_eventual_id_alias_should_error_if_list_is_empty() {
72        let _ = ExpanderUtils::expand_eventual_id_alias("LATEST", get_list_of_ids(vec![]))
73            .await
74            .expect_err(
75                "expand_eventual_id_alias should returns an error if returned list is empty.",
76            );
77    }
78
79    #[tokio::test]
80    async fn expand_eventual_id_alias_should_return_id_even_if_not_in_list() {
81        let hash = ExpanderUtils::expand_eventual_id_alias(
82            "hash-whatever",
83            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
84        )
85        .await
86        .expect(
87            "expand_eventual_id_alias should not error when hash-whatever is passed as parameter.",
88        );
89
90        assert_eq!("hash-whatever".to_string(), hash);
91    }
92}