File: ranges.rs

package info (click to toggle)
rust-threecpio 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 504 kB
  • sloc: sh: 53; makefile: 11
file content (229 lines) | stat: -rw-r--r-- 5,904 bytes parent folder | download
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
// Copyright (C) 2025-2026, Benjamin Drung <bdrung@posteo.de>
// SPDX-License-Identifier: ISC

use std::num::ParseIntError;
use std::ops::{RangeFrom, RangeInclusive, RangeTo};
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq)]
struct Range {
    start: Option<i32>,
    end: Option<i32>,
}

impl Range {
    fn new(start: Option<i32>, end: Option<i32>) -> Self {
        Self { start, end }
    }

    fn contains(&self, item: &i32) -> bool {
        if let Some(start) = self.start {
            if *item < start {
                return false;
            }
        }
        if let Some(end) = self.end {
            if *item > end {
                return false;
            }
        }
        true
    }

    fn has_more(&self, item: &i32) -> bool {
        self.end.is_none_or(|end| end > *item)
    }
}

impl From<RangeInclusive<i32>> for Range {
    fn from(item: RangeInclusive<i32>) -> Self {
        Self {
            start: Some(*item.start()),
            end: Some(*item.end()),
        }
    }
}

impl From<RangeFrom<i32>> for Range {
    fn from(item: RangeFrom<i32>) -> Self {
        Self {
            start: Some(item.start),
            end: None,
        }
    }
}

impl From<RangeTo<i32>> for Range {
    fn from(item: RangeTo<i32>) -> Self {
        Self {
            start: None,
            end: Some(item.end),
        }
    }
}

/// An array of ranges.
///
/// Each range can either be
/// * bounded inclusively below and above (`start-end`),
/// * bounded inclusively below (`start-`), or
/// * bounded exclusively above (`-end`).
#[derive(Clone, Debug, PartialEq)]
pub struct Ranges(Vec<Range>);

impl Ranges {
    #[cfg(test)]
    fn new(ranges: Vec<Range>) -> Self {
        Self(ranges)
    }

    /// Returns `true` if `item` is contained in at least of of the ranges.
    ///
    /// # Examples
    ///
    /// ```
    /// use threecpio::ranges::Ranges;
    ///
    /// assert!(!"3-4".parse::<Ranges>().unwrap().contains(&2));
    /// assert!( "3-4".parse::<Ranges>().unwrap().contains(&3));
    /// assert!( "3-4".parse::<Ranges>().unwrap().contains(&4));
    /// assert!(!"3-4".parse::<Ranges>().unwrap().contains(&5));
    /// ```
    pub fn contains(&self, item: &i32) -> bool {
        for range in &self.0 {
            if range.contains(item) {
                return true;
            }
        }
        false
    }

    /// Returns `true` if `Ranges` contain items higher than `item`.
    ///
    /// # Examples
    ///
    /// ```
    /// use threecpio::ranges::Ranges;
    ///
    /// assert!( "2-4".parse::<Ranges>().unwrap().has_more(&3));
    /// assert!(!"2-4".parse::<Ranges>().unwrap().has_more(&4));
    /// ```
    ///
    /// Ranges bounded inclusively below will cause `has_more` to always
    /// return `true`:
    ///
    /// ```
    /// use threecpio::ranges::Ranges;
    ///
    /// assert!("3-".parse::<Ranges>().unwrap().has_more(&9000));
    /// ```
    pub fn has_more(&self, item: &i32) -> bool {
        for range in &self.0 {
            if range.has_more(item) {
                return true;
            }
        }
        false
    }
}

impl FromStr for Ranges {
    type Err = ParseIntError;

    /// Parses a string `s` to return `Ranges`.
    ///
    /// `s` is made up of one range, or many ranges separated by commas.
    /// Each range can either be
    /// * one single item (`item`),
    /// * bounded inclusively below and above (`start-end`),
    /// * bounded inclusively below (`start-`), or
    /// * bounded exclusively above (`-end`).
    ///
    /// # Examples
    ///
    /// ```
    /// use threecpio::ranges::Ranges;
    ///
    /// assert!("1-3,5,7-".parse::<Ranges>().is_ok());
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut ranges = Vec::new();
        for range_str in s.split(",") {
            if let Some((start, end)) = range_str.split_once("-") {
                let start = if start.is_empty() {
                    None
                } else {
                    Some(start.parse()?)
                };
                let end = if end.is_empty() {
                    None
                } else {
                    Some(end.parse()?)
                };
                ranges.push(Range::new(start, end));
            } else {
                let start = range_str.parse()?;
                ranges.push(Range::new(Some(start), Some(start)));
            }
        }
        Ok(Self(ranges))
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    #[test]
    fn test_parse_ranges_error_single() {
        for s in ["str", "1-str", "str-5"] {
            let got = s.parse::<Ranges>().unwrap_err();
            assert_eq!(got.to_string(), "invalid digit found in string");
        }
    }

    #[test]
    fn test_parse_ranges_single() {
        assert_eq!("3".parse::<Ranges>(), Ok(Ranges::new(vec![(3..=3).into()])))
    }

    #[test]
    fn test_parse_ranges_range() {
        assert_eq!(
            "2-4".parse::<Ranges>(),
            Ok(Ranges::new(vec![(2..=4).into()]))
        )
    }

    #[test]
    fn test_parse_ranges_multiple() {
        assert_eq!(
            "1,3-5".parse::<Ranges>(),
            Ok(Ranges::new(vec![(1..=1).into(), (3..=5).into()]))
        )
    }

    #[test]
    fn test_parse_ranges_open_end() {
        assert_eq!("2-".parse::<Ranges>(), Ok(Ranges::new(vec![(2..).into()])))
    }

    #[test]
    fn test_parse_ranges_open_start() {
        assert_eq!("-4".parse::<Ranges>(), Ok(Ranges::new(vec![(..4).into()])))
    }

    #[test]
    fn test_ranges_contains() {
        let ranges = "1-3,5".parse::<Ranges>().unwrap();
        assert!(ranges.contains(&2));
        assert!(!ranges.contains(&4));
    }

    #[test]
    fn test_ranges_has_more() {
        let ranges = "4-5,7,-2".parse::<Ranges>().unwrap();
        assert!(ranges.has_more(&6));
        assert!(!ranges.has_more(&7));
    }
}