Skip to content

Commit c933111

Browse files
committed
Tidy and document
1 parent 08644f6 commit c933111

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

src/year.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::input::is_sorted_and_unique;
33
use anyhow::{ensure, Context, Result};
44
use itertools::Itertools;
55

6-
/// Parse a single year from a string
7-
fn parse_year(s: &str, milestone_years: &[u32]) -> Option<u32> {
6+
/// Parse a single year from a string and check it is in `valid_years`
7+
fn parse_and_validate_year(s: &str, valid_years: &[u32]) -> Option<u32> {
88
let year = s.trim().parse::<u32>().ok()?;
9-
if milestone_years.binary_search(&year).is_ok() {
9+
if valid_years.binary_search(&year).is_ok() {
1010
Some(year)
1111
} else {
1212
None
@@ -17,20 +17,38 @@ fn parse_year(s: &str, milestone_years: &[u32]) -> Option<u32> {
1717
///
1818
/// The string can be either "all" (case-insensitive), a single year, or a semicolon-separated list
1919
/// of years (e.g. "2020;2021;2022" or "2020; 2021; 2022")
20-
pub fn parse_year_str(s: &str, milestone_years: &[u32]) -> Result<Vec<u32>> {
21-
// We depend on this in `parse_year`
22-
assert!(is_sorted_and_unique(milestone_years));
20+
///
21+
/// # Arguments
22+
///
23+
/// - `s` - Input string to parse
24+
/// - `valid_years` - The possible years which can be referenced in `s` (must be sorted and unique)
25+
///
26+
/// # Returns
27+
///
28+
/// A [`Vec`] of years or an error.
29+
///
30+
/// # Panics
31+
///
32+
/// If `valid_years` is unsorted or non-unique.
33+
pub fn parse_year_str(s: &str, valid_years: &[u32]) -> Result<Vec<u32>> {
34+
// We depend on this in `parse_and_validate_year`
35+
assert!(
36+
is_sorted_and_unique(valid_years),
37+
"`valid_years` must be sorted and unique"
38+
);
2339

2440
let s = s.trim();
2541
ensure!(!s.is_empty(), "No years provided");
2642

2743
if s.eq_ignore_ascii_case("all") {
28-
return Ok(Vec::from_iter(milestone_years.iter().copied()));
44+
return Ok(Vec::from_iter(valid_years.iter().copied()));
2945
}
3046

3147
let years: Vec<_> = s
3248
.split(";")
33-
.map(|y| parse_year(y, milestone_years).with_context(|| format!("Invalid year: {y}")))
49+
.map(|y| {
50+
parse_and_validate_year(y, valid_years).with_context(|| format!("Invalid year: {y}"))
51+
})
3452
.try_collect()?;
3553

3654
ensure!(

0 commit comments

Comments
 (0)