aboutsummaryrefslogtreecommitdiff
path: root/src/types.rs
blob: 9a496c287fcfdb073313a59ba505de5828c007f5 (plain)
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
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DomainStatus {
    Available,
    Registered { expiry: Option<String> },
    Error { kind: ErrorKind, message: String },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ErrorKind {
    InvalidTld,
    Unknown,
    Timeout,
    RateLimit,
    Forbidden,
}

impl ErrorKind {
    /// parse from config string (case insensitive, underscores and hyphens both work)
    pub fn from_config_str(s: &str) -> Option<Self> {
        match s.to_lowercase().replace('-', "_").as_str() {
            "invalid_tld" | "invalidtld" => Some(ErrorKind::InvalidTld),
            "unknown" => Some(ErrorKind::Unknown),
            "timeout" => Some(ErrorKind::Timeout),
            "rate_limit" | "ratelimit" => Some(ErrorKind::RateLimit),
            "forbidden" => Some(ErrorKind::Forbidden),
            _ => None,
        }
    }

    /// back to config string
    pub fn to_config_str(&self) -> &'static str {
        match self {
            ErrorKind::InvalidTld => "invalid_tld",
            ErrorKind::Unknown => "unknown",
            ErrorKind::Timeout => "timeout",
            ErrorKind::RateLimit => "rate_limit",
            ErrorKind::Forbidden => "forbidden",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DomainResult {
    pub name: String,
    pub tld: String,
    pub full: String,
    pub status: DomainStatus,
}

impl DomainResult {
    pub fn new(name: &str, tld: &str, status: DomainStatus) -> Self {
        Self {
            name: name.to_string(),
            tld: tld.to_string(),
            full: format!("{}.{}", name, tld),
            status,
        }
    }

    pub fn is_available(&self) -> bool {
        matches!(self.status, DomainStatus::Available)
    }

    pub fn is_error(&self) -> bool {
        matches!(self.status, DomainStatus::Error { .. })
    }

    pub fn status_str(&self) -> &str {
        match &self.status {
            DomainStatus::Available => "available",
            DomainStatus::Registered { .. } => "registered",
            DomainStatus::Error { .. } => "error",
        }
    }

    pub fn note_str(&self) -> String {
        match &self.status {
            DomainStatus::Available => "-".to_string(),
            DomainStatus::Registered { expiry } => match expiry {
                Some(date) => format!("until {}", date),
                None => "no expiry info".to_string(),
            },
            DomainStatus::Error { kind, message } => match kind {
                ErrorKind::InvalidTld => "invalid tld".to_string(),
                _ => message.clone(),
            },
        }
    }
}