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
//! Module for parsing WGSL bindings using Pest and converting them into [`Binding`] model.
use super::{error::ParsingError, FromPest, Rule};
use crate::models::{binding::Binding, types::Type};
use pest::iterators::Pair;
impl FromPest for Binding {
fn from_pest(element: Pair<'_, Rule>) -> Result<Self, ParsingError>
where
Self: Sized,
{
match element.as_rule() {
Rule::RESOURCE_BINDING => {
let mut docs = None;
let mut attr_group = 0;
let mut attr_binding = 0;
let mut name = String::new();
let mut ty = Type::default();
for const_element in element.into_inner() {
match const_element.as_rule() {
Rule::DOCS => {
for docs_element in const_element.into_inner() {
if docs.is_none() {
docs = Some(String::new());
}
if let Some(docs) = &mut docs {
if !docs.is_empty() {
docs.push('\n');
}
docs.push_str(docs_element.as_span().as_str());
}
docs = docs.filter(|s| !s.is_empty());
}
}
Rule::IDENT => {
name = const_element.as_span().as_str().to_owned();
}
Rule::TYPE => {
ty = Type::from_pest(const_element)?;
}
Rule::BINDING_ATTRS => {
for binding_attr_element in const_element.into_inner() {
match binding_attr_element.as_rule() {
Rule::ATTR_GROUP => {
let group_str = binding_attr_element
.into_inner()
.next()
.unwrap()
.as_span()
.as_str();
attr_group = group_str.parse::<u16>().unwrap_or(0);
}
Rule::ATTR_BINDING => {
let binding_str = binding_attr_element
.into_inner()
.next()
.unwrap()
.as_span()
.as_str();
attr_binding = binding_str.parse::<u16>().unwrap_or(0);
}
_ => {}
}
}
}
_ => {}
}
}
Ok(Binding::new(docs, attr_group, attr_binding, name, ty))
}
_ => Err(ParsingError::InvalidPestRule {
expected: Rule::RESOURCE_BINDING,
found: element.as_rule(),
}),
}
}
}