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
use std::borrow::Cow;
use nom::{
error::{make_error, ErrorKind},
Err, IResult,
};
use crate::parse::combinators::{blank_lines_count, lines_while};
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]
pub struct Comment<'a> {
pub value: Cow<'a, str>,
pub post_blank: usize,
}
impl Comment<'_> {
pub(crate) fn parse(input: &str) -> Option<(&str, Comment)> {
parse_internal(input).ok()
}
pub fn into_owned(self) -> Comment<'static> {
Comment {
value: self.value.into_owned().into(),
post_blank: self.post_blank,
}
}
}
fn parse_internal(input: &str) -> IResult<&str, Comment, ()> {
let (input, value) = lines_while(|line| {
let line = line.trim_start();
line == "#" || line.starts_with("# ")
})(input)?;
if value.is_empty() {
return Err(Err::Error(make_error(input, ErrorKind::Many0)));
}
let (input, post_blank) = blank_lines_count(input)?;
Ok((
input,
Comment {
value: value.into(),
post_blank,
},
))
}