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
//! The Simple Switch Cases Configuration PreProcessor
//!
//! Introduction is in the Readme.
//!
//! Typical usage of the library is as follow:
//!
//! ```
//! use ssccpp::Parser;
//! let content =
//! b"1\n\
//! >>> thisone\n\
//! 2\n\
//! >>> other\n\
//! XXX\n\
//! >>>\n\
//! 3";
//! let parser = Parser::new("thisone",">>>").unwrap();
//! let mut res : Vec<u8> = Vec::new();
//! parser.process(&content[..], &mut res).unwrap();
//! let expected_result = b"1\n2\n3\n";
//! assert_eq!(expected_result, res.as_slice());
//! ```

use regex::{self, Regex, RegexSet};
use std::io::{BufRead, Error as IoError, Write};

const REGEX_IDX_DELIMITER_ONLY: usize = 0;
const REGEX_IDX_STAR: usize = 1;
const REGEX_IDX_IDENT: usize = 2;

#[derive(Debug)]
enum LineAction {
    EnterSpecific,
    EnterIgnore,
    EnterOtherwise,
    EnterGeneral,
    Process,
}

/// The struct implementing the actual logic.
///
/// See crate-level doc for typcial usage.
#[derive(Debug)]
pub struct Parser {
    delimiter_regex: Regex,
    caseswitch_regexset: RegexSet,
}

impl Parser {
    /// Build a new parser, with `ident` as identifier, and `delimiter` as delimiter.
    pub fn new(ident: &str, delimiter: &str) -> Result<Parser, regex::Error> {
        let delimiter_regex_string =
            format!(r"^\s*{delimiter}\s*", delimiter = regex::escape(delimiter));
        Ok(Parser {
            delimiter_regex: Regex::new(&delimiter_regex_string)?,
            caseswitch_regexset: RegexSet::new(&[
                r"^$",
                r"(?:[,\s]|^)\*(?:[,\s]|$)",
                &format!(r"\b{ident}\b", ident = regex::escape(ident)),
            ])?,
        })
    }

    fn discriminate(self: &Self, s: &str) -> LineAction {
        use LineAction::*;
        match self.delimiter_regex.find(s) {
            None => Process,
            Some(match_range) => {
                let matches = self.caseswitch_regexset.matches(&s[match_range.end()..]);
                if matches.matched(REGEX_IDX_DELIMITER_ONLY) {
                    EnterGeneral
                } else if matches.matched(REGEX_IDX_STAR) {
                    EnterOtherwise
                } else if matches.matched(REGEX_IDX_IDENT) {
                    EnterSpecific
                } else {
                    EnterIgnore
                }
            }
        }
    }

    /// Process an `input` (which should implement `BufRead`) and writes the corresponding result in
    /// `output`.
    pub fn process<I, O>(self: &Self, input: I, output: &mut O) -> Result<(), IoError>
    where
        I: BufRead,
        O: Write,
    {
        use LineAction::*;
        let mut entered_specific = false;
        let mut output_next = true;
        for line in input.lines() {
            let line = line?;
            match self.discriminate(&line) {
                Process => {
                    if output_next {
                        writeln!(output, "{}", &line)?
                    }

                }
                EnterSpecific => {
                    output_next = true;
                    entered_specific = true;
                }
                EnterIgnore => output_next = false,
                EnterOtherwise => output_next = !entered_specific,
                EnterGeneral => {
                    output_next = true;
                    entered_specific = false;
                }
            }
        }
        Ok(())
    }
}

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

    #[test]
    fn parsing1() {
        let content =
        b"1\n\
        >>> thisone\n\
        2\n\
        >>> other\n\
        XXX\n\
        >>>\n\
        3";
        let parser = Parser::new("thisone",">>>").unwrap();
        let mut res : Vec<u8> = Vec::new();
        parser.process(&content[..], &mut res).unwrap();
        let res_string = String::from_utf8(res).unwrap();
        let expected_result = "1\n2\n3\n";
        assert_eq!(expected_result, res_string);
    }

    #[test]
    fn parsing2() {
        let content =
        b"1\n\
        >>> thisone\n\
        2\n\
        >>> other\n\
        XXX\n\
        >>> *\n\
        XXX\n\
        >>>\n\
        3";
        let parser = Parser::new("thisone",">>>").unwrap();
        let mut res : Vec<u8> = Vec::new();
        parser.process(&content[..], &mut res).unwrap();
        let res_string = String::from_utf8(res).unwrap();
        let expected_result = "1\n2\n3\n";
        assert_eq!(expected_result, res_string);
    }

    #[test]
    fn parsing3() {
        let content =
        b"1\n\
        >>> other\n\
        XXX\n\
        >>> *\n\
        2\n\
        >>>\n\
        3";
        let parser = Parser::new("thisone",">>>").unwrap();
        let mut res : Vec<u8> = Vec::new();
        parser.process(&content[..], &mut res).unwrap();
        let res_string = String::from_utf8(res).unwrap();
        let expected_result = "1\n2\n3\n";
        assert_eq!(expected_result, res_string);
    }

    #[test]
    fn parsing4() {
        let content =
        b"1\n\
        \t\t>>>other\n\
        XXX>>>   \n\
        XXX\n\
        >>>thisone\n\
        2\n\
        >>>\n\
        3
        >>>*\n\
        4";
        let parser = Parser::new("thisone",">>>").unwrap();
        let mut res : Vec<u8> = Vec::new();
        parser.process(&content[..], &mut res).unwrap();
        let res_string = String::from_utf8(res).unwrap();
        let expected_result = "1\n2\n3\n4\n";
        assert_eq!(expected_result, res_string);
    }

    #[test]
    fn parsing5() {
        let content =
        b"1\n\
        >>> another, thisone, andanother\n\
        2\n\
        >>> other\n\
        XXX\n\
        >>>\n\
        3";
        let parser = Parser::new("thisone",">>>").unwrap();
        let mut res : Vec<u8> = Vec::new();
        parser.process(&content[..], &mut res).unwrap();
        let res_string = String::from_utf8(res).unwrap();
        let expected_result = "1\n2\n3\n";
        assert_eq!(expected_result, res_string);
    }
}