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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// This file is part of rss.
//
// Copyright © 2015-2021 The rust-syndication Developers
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the MIT License and/or Apache 2.0 License.

use std::collections::HashSet;
use std::error::Error as StdError;
use std::fmt;
use std::num::ParseIntError;

use chrono::DateTime;
use chrono::ParseError as DateParseError;
use mime::FromStrError as MimeParseError;
use mime::Mime;
use url::ParseError as UrlParseError;
use url::Url;

use crate::{Category, Channel, Cloud, Enclosure, Image, Item, Source, TextInput};

#[derive(Debug)]
/// Errors that occur during validation.
pub enum ValidationError {
    /// An error while parsing a string to a date.
    DateParsing(DateParseError),
    /// An error while parsing a string to an integer.
    IntParsing(ParseIntError),
    /// An error while parsing a string to a URL.
    UrlParsing(UrlParseError),
    /// An error while parsing a string to a MIME type.
    MimeParsing(MimeParseError),
    /// A different validation error.
    Validation(String),
}

impl StdError for ValidationError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self {
            ValidationError::DateParsing(ref err) => Some(err),
            ValidationError::IntParsing(ref err) => Some(err),
            ValidationError::UrlParsing(ref err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ValidationError::DateParsing(ref err) => err.fmt(f),
            ValidationError::IntParsing(ref err) => err.fmt(f),
            ValidationError::UrlParsing(ref err) => err.fmt(f),
            ValidationError::MimeParsing(_) => write!(f, "Unable to parse MIME type"),
            ValidationError::Validation(ref s) => write!(f, "{}", s),
        }
    }
}

impl From<DateParseError> for ValidationError {
    fn from(err: DateParseError) -> Self {
        ValidationError::DateParsing(err)
    }
}

impl From<ParseIntError> for ValidationError {
    fn from(err: ParseIntError) -> Self {
        ValidationError::IntParsing(err)
    }
}

impl From<UrlParseError> for ValidationError {
    fn from(err: UrlParseError) -> Self {
        ValidationError::UrlParsing(err)
    }
}

impl From<MimeParseError> for ValidationError {
    fn from(err: MimeParseError) -> Self {
        ValidationError::MimeParsing(err)
    }
}

/// A trait to support data validation.
pub trait Validate {
    /// Validate the data against the RSS specification.
    fn validate(&self) -> Result<(), ValidationError>;
}

macro_rules! validate {
    ($e: expr, $msg: expr) => {{
        if !($e) {
            return Err(ValidationError::Validation(String::from($msg)));
        }
    }};
}

impl Validate for Channel {
    fn validate(&self) -> Result<(), ValidationError> {
        Url::parse(self.link())?;

        for category in self.categories() {
            category.validate()?;
        }

        if let Some(cloud) = self.cloud() {
            cloud.validate()?;
        }

        if let Some(docs) = self.docs() {
            Url::parse(docs)?;
        }

        if let Some(image) = self.image() {
            image.validate()?;
        }

        for item in self.items() {
            item.validate()?;
        }

        if let Some(last_build_date) = self.last_build_date() {
            DateTime::parse_from_rfc2822(last_build_date)?;
        }

        if let Some(pub_date) = self.pub_date() {
            DateTime::parse_from_rfc2822(pub_date)?;
        }

        for hour in self.skip_hours() {
            let hour = hour.parse::<i64>()?;
            validate!(
                (0..=23).contains(&hour),
                "Channel skip hour is not between 0 and 23"
            );
        }

        let valid_days = {
            let mut set = HashSet::with_capacity(7);
            set.insert("Monday");
            set.insert("Tuesday");
            set.insert("Wednesday");
            set.insert("Thursday");
            set.insert("Friday");
            set.insert("Saturday");
            set.insert("Sunday");
            set
        };

        for day in self.skip_days() {
            validate!(
                valid_days.contains(day.as_str()),
                format!("Unknown skip day: {}", day)
            );
        }

        if let Some(text_input) = self.text_input() {
            text_input.validate()?;
        }

        if let Some(ttl) = self.ttl() {
            let ttl = ttl.parse::<i64>()?;
            validate!(ttl > 0, "Channel TTL is not greater than 0");
        }

        Ok(())
    }
}

impl Validate for Category {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(domain) = self.domain() {
            Url::parse(domain)?;
        }
        Ok(())
    }
}

impl Validate for Cloud {
    fn validate(&self) -> Result<(), ValidationError> {
        let port = self.port().parse::<i64>()?;
        validate!(port > 0, "Cloud port must be greater than 0");
        Url::parse(self.domain())?;
        validate!(
            vec!["xml-rpc", "soap", "http-post"].contains(&self.protocol()),
            format!("Unknown cloud protocol: {}", self.protocol())
        );
        Ok(())
    }
}

impl Validate for Enclosure {
    fn validate(&self) -> Result<(), ValidationError> {
        Url::parse(self.url())?;
        self.mime_type().parse::<Mime>()?;
        let length = self.length().parse::<i64>()?;
        validate!(length > 0, "Enclosure length is not greater than 0");
        Ok(())
    }
}

impl Validate for TextInput {
    fn validate(&self) -> Result<(), ValidationError> {
        Url::parse(self.link())?;
        Ok(())
    }
}

impl Validate for Image {
    fn validate(&self) -> Result<(), ValidationError> {
        Url::parse(self.link())?;
        Url::parse(self.url())?;

        if let Some(width) = self.width() {
            let width = width.parse::<i64>()?;
            validate!(
                (0..=144).contains(&width),
                "Image width is not between 0 and 144"
            );
        }

        if let Some(height) = self.height() {
            let height = height.parse::<i64>()?;
            validate!(
                (0..=144).contains(&height),
                "Image height is not between 0 and 144"
            );
        }

        Ok(())
    }
}

impl Validate for Item {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(link) = self.link() {
            Url::parse(link)?;
        }

        if let Some(comments) = self.comments() {
            Url::parse(comments)?;
        }

        if let Some(enclosure) = self.enclosure() {
            enclosure.validate()?;
        }

        if let Some(pub_date) = self.pub_date() {
            DateTime::parse_from_rfc2822(pub_date)?;
        }

        if let Some(source) = self.source() {
            source.validate()?;
        }

        Ok(())
    }
}

impl Validate for Source {
    fn validate(&self) -> Result<(), ValidationError> {
        Url::parse(self.url())?;
        Ok(())
    }
}