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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

pub use dns::DnsCommand;
pub use domain::DomainCommand;
use error::ErrorExt;
pub use invoice::{InvoiceAction, InvoiceCommand};
use parse::{Rule, TransipCommandParser};
use pest::{iterators::Pair, Parser};
pub use product::ProductCommand;
use std::{fmt::Display, str::FromStr};
pub use vps::{VpsAction, VpsCommand};

pub use error::Error;
pub type Result<T> = std::result::Result<T, Error>;

mod dns;
mod domain;
mod error;
mod invoice;
mod parse;
mod product;
mod vps;

#[derive(Debug, PartialEq)]
pub enum TransipCommand {
    /// # Example
    ///
    /// ```
    /// use transip_command::TransipCommand;
    ///
    /// let commandline = "# lkasjkfiekf";
    /// assert_eq!(
    ///     commandline.parse::<TransipCommand>().unwrap(),
    ///     TransipCommand::Comment(commandline.to_owned()),
    /// );
    /// ```
    Comment(String),

    Domain(domain::DomainCommand),

    Dns(dns::DnsCommand),

    Invoice(invoice::InvoiceCommand),

    Product(product::ProductCommand),

    Vps(vps::VpsCommand),

    /// # Example
    ///
    /// ```
    /// use transip_command::TransipCommand;
    ///
    /// let commandline = "sleep 5";
    /// assert_eq!(
    ///     commandline.parse::<TransipCommand>().unwrap(),
    ///     TransipCommand::Sleep(5),
    /// );
    /// ```
    Sleep(u64),
}

impl FromStr for TransipCommand {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let mut pairs = TransipCommandParser::parse(Rule::transip, s).map_err(Box::new)?;
        let pair = pairs
            .nth(0)
            .ok_or(Error::ParseTransipCommand(s.to_owned()))?;
        let inner = pair.into_inner().next().unwrap();
        match inner.as_rule() {
            Rule::comment => Ok(TransipCommand::Comment(inner.as_str().to_owned())),
            Rule::dns_command => DnsCommand::try_from(inner).map(TransipCommand::Dns),
            Rule::domain_command => DomainCommand::try_from(inner).map(TransipCommand::Domain),
            Rule::invoice_command => InvoiceCommand::try_from(inner).map(TransipCommand::Invoice),
            Rule::product_command => ProductCommand::try_from(inner).map(TransipCommand::Product),
            Rule::sleep_command => Ok(TransipCommand::Sleep(
                inner
                    .into_inner()
                    .next()
                    .unwrap()
                    .as_str()
                    .parse::<u64>()
                    .unwrap(),
            )),
            Rule::vps_command => VpsCommand::try_from(inner).map(TransipCommand::Vps),
            _ => Err(Error::ParseTransipCommand(s.to_owned())),
        }
    }
}

impl Display for TransipCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TransipCommand::Comment(comment) => write!(f, "{}", comment),
            TransipCommand::Dns(command) => write!(f, "dns {}", command),
            TransipCommand::Domain(command) => write!(f, "domain {}", command),
            TransipCommand::Invoice(command) => write!(f, "invoice {}", command),
            TransipCommand::Product(command) => write!(f, "product {}", command),
            TransipCommand::Sleep(timeout) => write!(f, "sleep {}", timeout),
            TransipCommand::Vps(command) => write!(f, "vps {}", command),
        }
    }
}

fn parameter(pair: Pair<'_, Rule>) -> Result<String> {
    match pair.as_rule() {
        Rule::env => {
            let name = pair
                .as_str()
                .strip_prefix("${")
                .unwrap()
                .strip_suffix('}')
                .unwrap();

            std::env::var(name).err_into()
        }
        Rule::value => Ok(pair.as_str().to_owned()),
        _ => Err(Error::ParseVpsCommand(pair.as_str().to_owned())),
    }
}

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

    #[test]
    fn display() {
        assert_eq!(
            TransipCommand::Comment("# lksadjf".to_owned()).to_string(),
            "# lksadjf".to_owned(),
        );

        assert_eq!(
            TransipCommand::Dns(crate::DnsCommand::List("paulmin.nl".to_owned())).to_string(),
            "dns list paulmin.nl".to_owned(),
        );

        assert_eq!(
            TransipCommand::Domain(crate::DomainCommand::List).to_string(),
            "domain list".to_owned(),
        );

        assert_eq!(
            TransipCommand::Invoice(crate::InvoiceCommand::List).to_string(),
            "invoice list".to_owned(),
        );

        assert_eq!(
            TransipCommand::Product(crate::ProductCommand::List).to_string(),
            "product list".to_owned(),
        );

        assert_eq!(
            TransipCommand::Vps(crate::VpsCommand::List).to_string(),
            "vps list".to_owned(),
        );
    }
}