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
//! Module for parsing WGSL imports using Pest and converting them into [`Import`] model.
use super::{error::ParsingError, FromPest, Rule};
use crate::models::import::Import;
use pest::iterators::Pair;
use std::path::PathBuf;
impl FromPest for Import {
fn from_pest(element: Pair<'_, Rule>) -> Result<Self, ParsingError>
where
Self: Sized,
{
match element.as_rule() {
Rule::IMPORT => {
let mut docs = None;
let mut path = PathBuf::new();
let mut name = String::new();
for import_element in element.into_inner() {
match import_element.as_rule() {
Rule::DOCS => {
for docs_element in import_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::IMPORT_PATH => {
path = PathBuf::from(import_element.as_span().as_str());
}
Rule::MODULE_NAME => {
name = import_element.as_span().as_str().to_owned();
}
_ => {}
}
}
Ok(Import::new(docs, path, name))
}
_ => Err(ParsingError::InvalidPestRule {
expected: Rule::IMPORT,
found: element.as_rule(),
}),
}
}
}