shindan_maker/
shindan_domain.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
use std::fmt;
use anyhow::anyhow;
use std::str::FromStr;

/// A domain of ShindanMaker.
#[derive(Debug, Clone, Copy)]
pub enum ShindanDomain {
    Jp,
    En,
    Cn,
    Kr,
    Th,
}

impl fmt::Display for ShindanDomain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let url = match self {
            Self::Jp => "https://shindanmaker.com/",
            Self::En => "https://en.shindanmaker.com/",
            Self::Cn => "https://cn.shindanmaker.com/",
            Self::Kr => "https://kr.shindanmaker.com/",
            Self::Th => "https://th.shindanmaker.com/",
        };
        write!(f, "{}", url)
    }
}

impl FromStr for ShindanDomain {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "JP" => Ok(Self::Jp),
            "EN" => Ok(Self::En),
            "CN" => Ok(Self::Cn),
            "KR" => Ok(Self::Kr),
            "TH" => Ok(Self::Th),
            _ => Err(anyhow!("Invalid domain")),
        }
    }
}

impl TryFrom<&str> for ShindanDomain {
    type Error = anyhow::Error;

    fn try_from(value: &str) -> anyhow::Result<Self, Self::Error> {
        value.parse()
    }
}

impl TryFrom<String> for ShindanDomain {
    type Error = anyhow::Error;

    fn try_from(value: String) -> anyhow::Result<Self, Self::Error> {
        value.parse()
    }
}