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
//! Module for parsing WGSL constants using Pest and converting them into [`Constant`] model.
use super::{error::ParsingError, FromPest, Rule};
use crate::models::{constant::Constant, types::Type};
use pest::iterators::Pair;
impl FromPest for Constant {
fn from_pest(element: Pair<'_, Rule>) -> Result<Self, ParsingError>
where
Self: Sized,
{
match element.as_rule() {
Rule::CONST => {
let mut docs = None;
let mut name = String::new();
let mut ty = None;
let mut value = String::new();
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 = Some(Type::from_pest(const_element)?);
}
Rule::CONST_VALUE => {
value = const_element.as_span().as_str().to_owned();
}
_ => {}
}
}
Ok(Constant::new(docs, name, ty, value))
}
_ => Err(ParsingError::InvalidPestRule {
expected: Rule::CONST,
found: element.as_rule(),
}),
}
}
}