hledger_parser/directive/
include.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
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
use chumsky::prelude::*;

mod format;

use crate::component::whitespace::whitespace;
use crate::directive::include::format::format;
use crate::state::State;
use crate::utils::end_of_line;

pub use crate::directive::include::format::Format;

#[derive(Clone, Debug, PartialEq)]
pub struct Include {
    pub format: Option<Format>,
    pub path: std::path::PathBuf,
}

#[must_use]
pub fn include<'a>() -> impl Parser<'a, &'a str, Include, extra::Full<Rich<'a, char>, State, ()>> {
    let path = any()
        .and_is(text::newline().not())
        .and_is(just(";").not())
        .repeated()
        .collect::<Vec<_>>();
    just("include")
        .ignore_then(whitespace().repeated().at_least(1))
        .ignore_then(format().then_ignore(just(":")).or_not())
        .then(path)
        .then_ignore(end_of_line())
        .map(|(format, path)| Include {
            format,
            path: std::path::PathBuf::from(path.iter().collect::<String>().trim_end()),
        })
}

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

    #[test]
    fn ok_without_format() {
        let result = include()
            .then_ignore(end())
            .parse("include path")
            .into_result();
        assert_eq!(
            result,
            Ok(Include {
                format: None,
                path: std::path::PathBuf::from("path")
            })
        );
    }

    #[test]
    fn ok_with_comment() {
        let result = include()
            .then_ignore(end())
            .parse("include path  ; with a comment !")
            .into_result();
        assert_eq!(
            result,
            Ok(Include {
                format: None,
                path: std::path::PathBuf::from("path")
            })
        );
    }

    #[test]
    fn ok_with_spaces() {
        let result = include()
            .then_ignore(end())
            .parse("include Path with space.csv")
            .into_result();
        assert_eq!(
            result,
            Ok(Include {
                format: None,
                path: std::path::PathBuf::from("Path with space.csv")
            })
        );
    }

    #[test]
    fn ok_with_format() {
        let result = include()
            .then_ignore(end())
            .parse("include rules:path")
            .into_result();
        assert_eq!(
            result,
            Ok(Include {
                format: Some(Format::Rules),
                path: std::path::PathBuf::from("path")
            })
        );
    }

    #[test]
    fn ok_trailing() {
        let result = include()
            .then_ignore(end())
            .parse("include path   ")
            .into_result();
        assert_eq!(
            result,
            Ok(Include {
                format: None,
                path: std::path::PathBuf::from("path")
            })
        );
    }
}