whois_rust/
who_is_server_value.rs

1use serde_json::Value;
2use validators::prelude::*;
3
4use crate::{WhoIsError, WhoIsHost};
5
6const DEFAULT_PUNYCODE: bool = true;
7
8/// The model of a WHOIS server.
9#[derive(Debug, Clone)]
10pub struct WhoIsServerValue {
11    pub host:     WhoIsHost,
12    pub query:    Option<String>,
13    pub punycode: bool,
14}
15
16impl WhoIsServerValue {
17    pub fn from_value(value: &Value) -> Result<WhoIsServerValue, WhoIsError> {
18        match value {
19            Value::Object(map) => match map.get("host") {
20                Some(Value::String(host)) => {
21                    let host = match WhoIsHost::parse_str(host) {
22                        Ok(host) => host,
23                        Err(_) => {
24                            return Err(WhoIsError::MapError(
25                                "The server value is an object, but it has not a correct host \
26                                 string.",
27                            ))
28                        },
29                    };
30
31                    let query = match map.get("query") {
32                        Some(query) => {
33                            if let Value::String(query) = query {
34                                Some(String::from(query))
35                            } else {
36                                return Err(WhoIsError::MapError(
37                                    "The server value is an object, but it has an incorrect query \
38                                     string.",
39                                ));
40                            }
41                        },
42                        None => None,
43                    };
44
45                    let punycode = match map.get("punycode") {
46                        Some(punycode) => {
47                            if let Value::Bool(punycode) = punycode {
48                                *punycode
49                            } else {
50                                return Err(WhoIsError::MapError(
51                                    "The server value is an object, but it has an incorrect \
52                                     punycode boolean value.",
53                                ));
54                            }
55                        },
56                        None => DEFAULT_PUNYCODE,
57                    };
58
59                    Ok(WhoIsServerValue {
60                        host,
61                        query,
62                        punycode,
63                    })
64                },
65                _ => Err(WhoIsError::MapError(
66                    "The server value is an object, but it has not a host string.",
67                )),
68            },
69            Value::String(host) => Self::from_string(host),
70            _ => Err(WhoIsError::MapError("The server value is not an object or a host string.")),
71        }
72    }
73
74    #[inline]
75    pub fn from_string<S: AsRef<str>>(string: S) -> Result<WhoIsServerValue, WhoIsError> {
76        let host = string.as_ref();
77
78        let host = match WhoIsHost::parse_str(host) {
79            Ok(host) => host,
80            Err(_) => {
81                return Err(WhoIsError::MapError("The server value is not a correct host string."))
82            },
83        };
84
85        Ok(WhoIsServerValue {
86            host,
87            query: None,
88            punycode: DEFAULT_PUNYCODE,
89        })
90    }
91}