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
use crate::serde_json::Value;
use crate::validators::prelude::*;

use crate::{WhoIsError, WhoIsHost};

const DEFAULT_PUNYCODE: bool = true;

/// The model of a WHOIS server.
#[derive(Debug, Clone)]
pub struct WhoIsServerValue {
    pub host: WhoIsHost,
    pub query: Option<String>,
    pub punycode: bool,
}

impl WhoIsServerValue {
    pub fn from_value(value: &Value) -> Result<WhoIsServerValue, WhoIsError> {
        match value {
            Value::Object(map) => {
                match map.get("host") {
                    Some(Value::String(host)) => {
                        let host = match WhoIsHost::parse_str(host) {
                            Ok(host) => host,
                            Err(_) => return Err(WhoIsError::MapError("The server value is an object, but it has not a correct host string."))
                        };

                        let query = match map.get("query") {
                            Some(query) => {
                                if let Value::String(query) = query {
                                    Some(String::from(query))
                                } else {
                                    return Err(WhoIsError::MapError("The server value is an object, but it has an incorrect query string."));
                                }
                            }
                            None => None,
                        };

                        let punycode = match map.get("punycode") {
                            Some(punycode) => {
                                if let Value::Bool(punycode) = punycode {
                                    *punycode
                                } else {
                                    return Err(WhoIsError::MapError("The server value is an object, but it has an incorrect punycode boolean value."));
                                }
                            }
                            None => DEFAULT_PUNYCODE,
                        };

                        Ok(WhoIsServerValue {
                            host,
                            query,
                            punycode,
                        })
                    }
                    _ => {
                        Err(WhoIsError::MapError(
                            "The server value is an object, but it has not a host string.",
                        ))
                    }
                }
            }
            Value::String(host) => Self::from_string(host),
            _ => Err(WhoIsError::MapError("The server value is not an object or a host string.")),
        }
    }

    #[inline]
    pub fn from_string<S: AsRef<str>>(string: S) -> Result<WhoIsServerValue, WhoIsError> {
        let host = string.as_ref();

        let host = match WhoIsHost::parse_str(host) {
            Ok(host) => host,
            Err(_) => {
                return Err(WhoIsError::MapError("The server value is not a correct host string."))
            }
        };

        Ok(WhoIsServerValue {
            host,
            query: None,
            punycode: DEFAULT_PUNYCODE,
        })
    }
}