hledger_parser/directive/
decimal_mark.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
63
64
65
66
67
use chumsky::prelude::*;

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

#[derive(Clone, Debug, PartialEq)]
pub struct DecimalMark(pub char);

pub fn decimal_mark<'a>(
) -> impl Parser<'a, &'a str, DecimalMark, extra::Full<Rich<'a, char>, State, ()>> {
    just("decimal-mark")
        .ignore_then(whitespace().repeated().at_least(1))
        .ignore_then(one_of(".,"))
        .then_ignore(end_of_line())
        .map(DecimalMark)
}

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

    #[test]
    fn ok_trailing() {
        let result = decimal_mark()
            .then_ignore(end())
            .parse("decimal-mark , ")
            .into_result();
        assert_eq!(result, Ok(DecimalMark(',')));
    }

    #[test]
    fn ok_comma() {
        let result = decimal_mark()
            .then_ignore(end())
            .parse("decimal-mark ,")
            .into_result();
        assert_eq!(result, Ok(DecimalMark(',')));
    }

    #[test]
    fn ok_dot() {
        let result = decimal_mark()
            .then_ignore(end())
            .parse("decimal-mark .")
            .into_result();
        assert_eq!(result, Ok(DecimalMark('.')));
    }

    #[test]
    fn ok_comment() {
        let result = decimal_mark()
            .then_ignore(end())
            .parse("decimal-mark .  ; test")
            .into_result();
        assert_eq!(result, Ok(DecimalMark('.')));
    }

    #[test]
    fn err_format() {
        let result = decimal_mark()
            .then_ignore(end())
            .parse("decimal-mark ")
            .into_result();
        assert!(result.is_err());
    }
}