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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
extern crate serde_json;

use std::default::Default;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, USER_AGENT};
use reqwest::Client as HttpClient;
use reqwest::{Response, StatusCode};

pub const MARKUP_VALIDATOR_URI: &str = "https://validator.w3.org/nu/";
pub const TEXT_HTML_UTF_8: &str = "text/html; charset=utf-8";

#[derive(Serialize, Deserialize)]
pub struct Message {
    #[serde(rename = "type")]
    pub _type: String,
    #[serde(rename = "lastLine")]
    pub last_line: Option<u8>,
    #[serde(rename = "lastColumn")]
    pub last_column: Option<u8>,
    #[serde(rename = "firstColumn")]
    pub first_column: Option<u8>,
    #[serde(rename = "subType")]
    pub sub_type: Option<String>,
    pub message: String,
    pub extract: Option<String>,
    #[serde(rename = "hiliteStart")]
    pub hilite_start: Option<u8>,
    #[serde(rename = "hiliteLength")]
    pub hilite_length: Option<u8>,
}

impl Message {
    pub fn is_warning(&self) -> bool {
        &self._type == "warning"
    }

    pub fn is_error(&self) -> bool {
        &self._type == "error"
    }
}

#[derive(Serialize, Deserialize)]
pub struct NuValidatorResult {
    pub url: Option<String>,
    pub messages: Vec<Message>,
    pub language: Option<String>,
}

impl NuValidatorResult {
    pub fn is_valid(&self) -> bool {
        !self.messages.iter().any(|msg| msg.is_error())
    }
}

pub struct NuValidatorOpts {
    validator_uri: String,
}

impl Default for NuValidatorOpts {
    fn default() -> NuValidatorOpts {
        NuValidatorOpts {
            validator_uri: MARKUP_VALIDATOR_URI.to_string(),
        }
    }
}

pub struct NuValidator {
    pub validator_uri: String,
    http_client: HttpClient,
}

/// # Examples
///
/// ```
/// use std::default::Default;
/// use w3c_validators::NuValidator;
/// let nu = NuValidator::new(Default::default());
/// match nu.validate_text("<!DOCTYPE html><html lang=en></html>") {
///     Some(result) => {
///         if !result.is_valid() {
///             for msg in result.messages {
///                 println!("{}: {}", msg._type, msg.message);
///             }
///         }
///     }
///     None => {},
/// }
/// ```
impl NuValidator {
    pub fn new(opts: NuValidatorOpts) -> NuValidator {
        let http_client = HttpClient::builder()
            .timeout(Duration::from_secs(5))
            .build()
            .expect("HttpClient failed to construct");
        NuValidator {
            validator_uri: opts.validator_uri,
            http_client,
        }
    }

    fn fetch_result(&self, mut response: Response) -> Option<NuValidatorResult> {
        match response.text() {
            Ok(data) => match serde_json::from_str(&data) {
                Ok(v) => Some(v),
                _ => None,
            },
            _ => None,
        }
    }

    pub fn validate_uri(&self, uri: &str) -> Option<NuValidatorResult> {
        let url = &format!("{}?doc={}&out=json", MARKUP_VALIDATOR_URI, uri);
        match self
            .http_client
            .get(url)
            .header(USER_AGENT, ua_validator!())
            .send()
        {
            Err(_) => None,
            Ok(response) => match response.status() {
                StatusCode::OK | StatusCode::NOT_MODIFIED => self.fetch_result(response),
                _ => None,
            },
        }
    }

    pub fn validate_text(&self, text: &'static str) -> Option<NuValidatorResult> {
        let url = &format!("{}?out=json", MARKUP_VALIDATOR_URI);
        let mut headers = HeaderMap::new();
        headers.insert(USER_AGENT, HeaderValue::from_static(ua_validator!()));
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_str(TEXT_HTML_UTF_8).expect(""),
        );
        match self
            .http_client
            .post(url)
            .headers(headers)
            .body(text)
            .send()
        {
            Err(_) => None,
            Ok(response) => match response.status() {
                StatusCode::OK | StatusCode::NOT_MODIFIED => self.fetch_result(response),
                _ => None,
            },
        }
    }
}