hledger_parser/directive/
year.rs

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
use chumsky::prelude::*;

use crate::component::whitespace::whitespace;
use crate::state::State;
use crate::utils::end_of_line;

#[derive(Clone, Debug, PartialEq)]
pub struct Year(pub i32);

pub fn year<'a>() -> impl Parser<'a, &'a str, Year, extra::Full<Rich<'a, char>, State, ()>> {
    just("Y")
        .or(just("year").then_ignore(whitespace().repeated().at_least(1)))
        .ignore_then(
            any()
                .filter(|c: &char| c.is_ascii_digit())
                .repeated()
                .at_least(1)
                .collect::<String>()
                .map_with(|p, e| {
                    let state: &mut State = e.state();
                    let year = p.parse::<i32>().unwrap();
                    state.year = year;
                    year
                }),
        )
        .then_ignore(end_of_line())
        .map(Year)
}

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

    #[test]
    fn should_update_state() {
        let mut state = State { year: 1 };
        let result = year()
            .then_ignore(end())
            .parse_with_state("Y2024", &mut state)
            .into_result();
        assert_eq!(result, Ok(Year(2024)));
        assert_eq!(state.year, 2024);
    }

    #[test]
    fn deprecated_form() {
        let result = year()
            .then_ignore(end())
            .parse("year 2024 ; just a comment")
            .into_result();
        assert_eq!(result, Ok(Year(2024)));
    }

    #[test]
    fn ok_with_comment() {
        let result = year()
            .then_ignore(end())
            .parse("Y2024 ; just a comment")
            .into_result();
        assert_eq!(result, Ok(Year(2024)));
    }
}