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
93
94
95
96
97
use crate::{error::Error, str_extension::StrExtension};
use std::{fmt::Display, str::FromStr};

pub type ProductName = String;

const LIST: &str = "list";
const ELEMENTS: &str = "elements";

#[derive(Debug, PartialEq)]
pub enum ProductCommand {
    /// # Example
    ///
    /// ```
    /// use transip_command::{ProductCommand, TransipCommand};
    ///
    /// let commandline = "product list";
    /// assert_eq!(
    ///     commandline.parse::<TransipCommand>().unwrap(),
    ///     TransipCommand::Product(ProductCommand::List),
    /// );
    /// ```
    List,

    /// # Example
    ///
    /// ```
    /// use transip_command::{ProductCommand, TransipCommand};
    ///
    /// let commandline = "product elements haip-basic-contract";
    /// assert_eq!(
    ///     commandline.parse::<TransipCommand>().unwrap(),
    ///     TransipCommand::Product(
    ///         ProductCommand::Elements(
    ///             "haip-basic-contract".to_owned(),
    ///         )
    ///     ),
    /// );
    /// ```
    Elements(ProductName),
}

impl FromStr for ProductCommand {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        if s.trim() == LIST {
            return Ok(ProductCommand::List);
        }

        if let Some(product_name) = s.one_param(ELEMENTS) {
            return Ok(ProductCommand::Elements(product_name.to_owned()));
        }

        Err(Error::ParseProductCommand(s.to_owned()))
    }
}

impl Display for ProductCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProductCommand::Elements(name) => write!(f, "{} {}", ELEMENTS, name),
            ProductCommand::List => write!(f, "{}", LIST),
        }
    }
}

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

    #[test]
    fn display() {
        assert_eq!(
            ProductCommand::Elements("haip-basic-contract".to_owned()).to_string(),
            "elements haip-basic-contract".to_owned(),
        );

        assert_eq!(ProductCommand::List.to_string(), "list".to_string(),);
    }

    #[test]
    fn from_str() {
        assert_eq!(
            "list".parse::<ProductCommand>().unwrap(),
            ProductCommand::List,
        );

        assert!("list kdf".parse::<ProductCommand>().is_err());

        assert_eq!(
            "elements lkjdf".parse::<ProductCommand>().unwrap(),
            ProductCommand::Elements("lkjdf".to_owned()),
        );

        assert!("elements ldkfj dkfjf".parse::<ProductCommand>().is_err());
    }
}