mithril_client_cli/utils/
expander.rs

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use anyhow::anyhow;
use futures::Future;
use mithril_client::MithrilResult;

/// Utilities to expand aliases into their associated ids.
pub struct ExpanderUtils;

impl ExpanderUtils {
    /// Expands an id alias to the latest id.
    /// If the provided id is "latest", retrieves the list of ids using the given future and returns the first one.
    /// Otherwise, returns the provided id as is.
    pub async fn expand_eventual_id_alias(
        id: &str,
        get_list_of_ids: impl Future<Output = MithrilResult<Vec<String>>>,
    ) -> MithrilResult<String> {
        if id.to_lowercase() == "latest" {
            let list = get_list_of_ids.await?;
            let last_element = list.first().ok_or_else(|| anyhow!("Entity not found"))?;
            Ok(last_element.to_string())
        } else {
            Ok(id.to_string())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    async fn get_list_of_ids(ids: Vec<&str>) -> MithrilResult<Vec<String>> {
        Ok(ids.iter().map(|h| h.to_string()).collect::<Vec<_>>())
    }

    #[tokio::test]
    async fn expand_eventual_id_alias_should_returns_id() {
        let hash = ExpanderUtils::expand_eventual_id_alias(
            "hash-234",
            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
        )
        .await
        .expect("expand_eventual_id_alias should not error when hash-234 is passed as parameter.");

        assert_eq!("hash-234", hash);
    }

    #[tokio::test]
    async fn expand_eventual_id_alias_latest_lowercase() {
        let hash = ExpanderUtils::expand_eventual_id_alias(
            "latest",
            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
        )
        .await
        .expect("expand_eventual_id_alias should not error when latest is passed as parameter.");

        assert_eq!("hash-123".to_string(), hash);
    }

    #[tokio::test]
    async fn expand_eventual_id_alias_latest_uppercase() {
        let hash = ExpanderUtils::expand_eventual_id_alias(
            "LATEST",
            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
        )
        .await
        .expect("expand_eventual_id_alias should not error when latest is passed as parameter.");

        assert_eq!("hash-123".to_string(), hash);
    }

    #[tokio::test]
    async fn expand_eventual_id_alias_should_error_if_list_is_empty() {
        let _ = ExpanderUtils::expand_eventual_id_alias("LATEST", get_list_of_ids(vec![]))
            .await
            .expect_err(
                "expand_eventual_id_alias should returns an error if returned list is empty.",
            );
    }

    #[tokio::test]
    async fn expand_eventual_id_alias_should_return_id_even_if_not_in_list() {
        let hash = ExpanderUtils::expand_eventual_id_alias(
            "hash-whatever",
            get_list_of_ids(vec!["hash-123", "hash-234", "hash-345"]),
        )
        .await
        .expect(
            "expand_eventual_id_alias should not error when hash-whatever is passed as parameter.",
        );

        assert_eq!("hash-whatever".to_string(), hash);
    }
}