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
#![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::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),
}

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::domain_command => DomainCommand::try_from(inner).map(TransipCommand::Domain),
            Rule::dns_command => DnsCommand::try_from(inner).map(TransipCommand::Dns),
            Rule::vps_command => VpsCommand::try_from(inner).map(TransipCommand::Vps),
            Rule::invoice_command => InvoiceCommand::try_from(inner).map(TransipCommand::Invoice),
            Rule::product_command => ProductCommand::try_from(inner).map(TransipCommand::Product),
            _ => Err(Error::ParseTransipCommand(s.to_owned())),
        }
    }
}

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())),
    }
}