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
use nom::{
bytes::complete::tag,
combinator::{map, opt},
sequence::tuple,
IResult,
};
use super::{annotations::Annotations, blank, ident::Ident, list_separator, ty::Type, Parser};
#[derive(Debug, Clone)]
pub struct Field {
pub name: Ident,
pub ty: Type,
pub annotations: Annotations,
}
impl Parser for Field {
fn parse(input: &str) -> IResult<&str, Self> {
map(
tuple((
opt(Annotations::parse),
opt(blank),
Ident::parse,
tag(":"),
opt(blank),
Type::parse,
opt(blank),
opt(list_separator),
)),
|(annotations, _, name, _, _, ty, _, _)| Field {
name,
ty,
annotations: annotations.unwrap_or_default(),
},
)(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::ty::Type;
#[test]
fn test_field() {
let input = r#"#[default = "Bar::new"]
foo: Bar"#;
match super::Field::parse(input) {
Ok((remain, field)) => {
assert_eq!(remain, "");
assert_eq!(field.name.0, "foo");
match field.ty {
Type::Path(path) => {
assert_eq!(path.segments.len(), 1);
assert_eq!(path.segments[0].0, "Bar");
}
_ => panic!("Expected Path"),
}
assert_eq!(field.annotations.len(), 1);
assert_eq!(field.annotations[0].key, "default");
assert_eq!(field.annotations[0].value.0, "Bar::new");
}
Err(e) => panic!("{e:?}"),
}
}
}