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
use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;
use std::ops::Deref;
use std::ops::DerefMut;
use pest::iterators::Pair;
use crate::ast::*;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;
#[derive(Clone, Debug, Eq, Hash, OpaqueTypedef, Ord, PartialEq, PartialOrd)]
#[opaque_typedef(allow_mut_ref)]
#[opaque_typedef(derive(
AsRef(Inner, Self),
AsMut(Inner, Self),
Deref,
DerefMut,
Into(Inner),
FromInner,
PartialEq(Inner),
))]
pub struct Comment {
value: String,
}
impl Comment {
pub fn new<S>(value: S) -> Self
where
S: Into<String>,
{
Comment { value: value.into() }
}
}
impl Display for Comment {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
f.write_str("! ").and(self.value.fmt(f))
}
}
impl<'i> FromPair<'i> for Comment {
const RULE: Rule = Rule::HiddenComment;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
Ok(Comment::new(pair.as_str()[1..].trim().to_string()))
}
}
impl_fromstr!(Comment);
#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::string::ToString;
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn from_str() {
let comment = Comment::from_str("! something").unwrap();
assert_eq!(comment, Comment::new("something"));
}
#[test]
fn to_string() {
let comment = Comment::new("something");
assert_eq!(comment.to_string(), "! something");
}
}