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
use std::{fmt::Display, num::ParseIntError, ops::RangeInclusive, str::FromStr};

use once_cell::sync::Lazy;
use regex::Regex;
use thiserror::Error;

static RANGE_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^([0-9]{3})?\.\.(=?)([0-9]{3})+$|^([0-9]{3})$").unwrap());

/// The [`AcceptRangeParseError`] indicates that the parsing process of an
/// [`AcceptRange`]  from a string failed due to various underlying reasons.
#[derive(Debug, Error, PartialEq)]
pub enum AcceptRangeError {
    /// The string input didn't contain any range pattern.
    #[error("no range pattern found")]
    NoRangePattern,

    /// The start or end index could not be parsed as an integer.
    #[error("failed to parse str as integer")]
    ParseIntError(#[from] ParseIntError),

    /// The start index is larger than the end index.
    #[error("invalid range indices, only start < end supported")]
    InvalidRangeIndices,
}

/// [`AcceptRange`] specifies which HTTP status codes are accepted and
/// considered successful when checking a remote URL.
#[derive(Clone, Debug, PartialEq)]
pub struct AcceptRange(RangeInclusive<u16>);

impl FromStr for AcceptRange {
    type Err = AcceptRangeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let captures = RANGE_PATTERN
            .captures(s)
            .ok_or(AcceptRangeError::NoRangePattern)?;

        if let Some(value) = captures.get(4) {
            let value: u16 = value.as_str().parse()?;
            Self::new_from(value, value)
        } else {
            let start: u16 = match captures.get(1) {
                Some(start) => start.as_str().parse().unwrap_or_default(),
                None => 0,
            };

            let inclusive = !captures[2].is_empty();
            let end: u16 = captures[3].parse()?;

            if inclusive {
                Self::new_from(start, end)
            } else {
                Self::new_from(start, end - 1)
            }
        }
    }
}

impl Display for AcceptRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}..={}", self.start(), self.end())
    }
}

impl AcceptRange {
    /// Creates a new [`AcceptRange`] which matches values between `start` and
    /// `end` (both inclusive).
    #[must_use]
    pub const fn new(start: u16, end: u16) -> Self {
        Self(RangeInclusive::new(start, end))
    }

    /// Creates a new [`AcceptRange`] which matches values between `start` and
    /// `end` (both inclusive). It additionally validates that `start` > `end`.
    ///
    /// # Errors
    ///
    /// Returns an error if `start` > `end`.
    pub const fn new_from(start: u16, end: u16) -> Result<Self, AcceptRangeError> {
        if start > end {
            return Err(AcceptRangeError::InvalidRangeIndices);
        }

        Ok(Self::new(start, end))
    }

    /// Returns the `start` value of this [`AcceptRange`].
    #[must_use]
    pub const fn start(&self) -> &u16 {
        self.0.start()
    }

    /// Returns the `end` value of this [`AcceptRange`].
    #[must_use]
    pub const fn end(&self) -> &u16 {
        self.0.end()
    }

    /// Returns whether this [`AcceptRange`] contains `value`.
    #[must_use]
    pub fn contains(&self, value: u16) -> bool {
        self.0.contains(&value)
    }

    /// Consumes self and returns the inner range.
    #[must_use]
    pub const fn inner(self) -> RangeInclusive<u16> {
        self.0
    }

    pub(crate) fn update_start(&mut self, new_start: u16) -> Result<(), AcceptRangeError> {
        let end = *self.end();

        if new_start > end {
            return Err(AcceptRangeError::InvalidRangeIndices);
        }

        self.0 = RangeInclusive::new(new_start, end);
        Ok(())
    }

    pub(crate) fn update_end(&mut self, new_end: u16) -> Result<(), AcceptRangeError> {
        let start = *self.start();

        if start > new_end {
            return Err(AcceptRangeError::InvalidRangeIndices);
        }

        self.0 = RangeInclusive::new(*self.start(), new_end);
        Ok(())
    }

    pub(crate) fn merge(&mut self, other: &Self) -> bool {
        // Merge when the end value of self overlaps with other's start
        if self.end() >= other.start() && other.end() >= self.end() {
            // We can ignore the result here, as it is guaranteed that
            // start < new_end
            let _ = self.update_end(*other.end());
            return true;
        }

        // Merge when the start value of self overlaps with other's end
        if self.start() <= other.end() && other.start() <= self.start() {
            // We can ignore the result here, as it is guaranteed that
            // start < new_end
            let _ = self.update_start(*other.start());
            return true;
        }

        false
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use rstest::rstest;

    #[rstest]
    #[case("100..=200", vec![100, 150, 200], vec![250, 300])]
    #[case("..=100", vec![0, 50, 100], vec![150, 200])]
    #[case("100..200", vec![100, 150], vec![200, 250])]
    #[case("..100", vec![0, 50], vec![100, 150])]
    #[case("404", vec![404], vec![200, 304, 500])]
    fn test_from_str(
        #[case] input: &str,
        #[case] valid_values: Vec<u16>,
        #[case] invalid_values: Vec<u16>,
    ) {
        let range = AcceptRange::from_str(input).unwrap();

        for valid in valid_values {
            assert!(range.contains(valid));
        }

        for invalid in invalid_values {
            assert!(!range.contains(invalid));
        }
    }

    #[rstest]
    #[case("200..=100", AcceptRangeError::InvalidRangeIndices)]
    #[case("-100..=100", AcceptRangeError::NoRangePattern)]
    #[case("-100..100", AcceptRangeError::NoRangePattern)]
    #[case("100..=-100", AcceptRangeError::NoRangePattern)]
    #[case("100..-100", AcceptRangeError::NoRangePattern)]
    #[case("0..0", AcceptRangeError::NoRangePattern)]
    #[case("abcd", AcceptRangeError::NoRangePattern)]
    #[case("-1", AcceptRangeError::NoRangePattern)]
    #[case("0", AcceptRangeError::NoRangePattern)]
    fn test_from_str_invalid(#[case] input: &str, #[case] error: AcceptRangeError) {
        let range = AcceptRange::from_str(input);
        assert_eq!(range, Err(error));
    }

    #[rstest]
    #[case("100..=200", "210..=300", "100..=200")]
    #[case("100..=200", "190..=300", "100..=300")]
    #[case("100..200", "200..300", "100..200")]
    #[case("100..200", "190..300", "100..300")]
    fn test_merge(#[case] range: &str, #[case] other: &str, #[case] result: &str) {
        let mut range = AcceptRange::from_str(range).unwrap();
        let other = AcceptRange::from_str(other).unwrap();

        let result = AcceptRange::from_str(result).unwrap();
        range.merge(&other);

        assert_eq!(result, range);
    }
}