fapolicy_rules/parser/
comment.rs

1/*
2 * Copyright Concurrent Technologies Corporation 2021
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use nom::bytes::complete::tag;
10use nom::character::complete::not_line_ending;
11
12use nom::sequence::preceded;
13
14use crate::parser::parse::{StrTrace, TraceResult};
15
16pub fn parse(i: StrTrace) -> TraceResult<String> {
17    match nom::combinator::complete(preceded(tag("#"), not_line_ending))(i) {
18        Ok((remaining, c)) => Ok((remaining, c.current.to_string())),
19        Err(e) => Err(e),
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn simple() {
29        let expected = "im a comment".to_string();
30        assert_eq!(
31            expected,
32            parse(format!("#{}", expected).as_str().into())
33                .ok()
34                .unwrap()
35                .1
36        );
37    }
38
39    #[test]
40    fn empty_line() {
41        assert_eq!("", parse("#".into()).ok().unwrap().1);
42    }
43
44    #[test]
45    fn empty_with_newline() {
46        assert_eq!("", parse("#\n".into()).ok().unwrap().1);
47    }
48}