aldrin_parser/
parser.rs

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::error::DuplicateServiceUuid;
use crate::issues::Issues;
use crate::validate::Validate;
use crate::{Error, Schema, Warning};
use std::collections::hash_map::{Entry, HashMap};
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub struct Parser {
    schema_paths: Vec<PathBuf>,
}

impl Parser {
    pub fn new() -> Self {
        Self {
            schema_paths: Vec::new(),
        }
    }

    pub fn add_schema_path<P>(&mut self, path: P)
    where
        P: Into<PathBuf>,
    {
        self.schema_paths.push(path.into());
    }

    pub fn parse<P>(&self, schema_path: P) -> Parsed
    where
        P: AsRef<Path>,
    {
        let mut issues = Issues::default();
        let main_schema = Schema::parse(schema_path, &mut issues);

        let mut parsed = Parsed {
            main_schema: main_schema.name().to_owned(),
            schemas: HashMap::new(),
            issues,
        };
        parsed
            .schemas
            .insert(main_schema.name().to_owned(), main_schema);

        let mut imports = parsed
            .main_schema()
            .imports()
            .iter()
            .map(|i| i.schema_name().value().to_owned())
            .collect::<Vec<_>>();
        while let Some(import) = imports.pop() {
            let entry = match parsed.schemas.entry(import) {
                Entry::Occupied(_) => continue,
                Entry::Vacant(entry) => entry,
            };

            let schema_path = match self.find_schema(entry.key()) {
                Some(schema_path) => schema_path,
                None => continue,
            };

            let schema = Schema::parse(schema_path, &mut parsed.issues);
            imports.extend(
                schema
                    .imports()
                    .iter()
                    .map(|i| i.schema_name().value().to_owned()),
            );
            entry.insert(schema);
        }

        parsed.validate(&self.schema_paths);
        parsed
    }

    fn find_schema(&self, schema_name: &str) -> Option<PathBuf> {
        for mut path in self.schema_paths.iter().rev().cloned() {
            path.push(schema_name);
            path.set_extension("aldrin");

            if path.is_file() {
                return Some(path);
            }
        }

        None
    }
}

impl Default for Parser {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
pub struct Parsed {
    main_schema: String,
    schemas: HashMap<String, Schema>,
    issues: Issues,
}

impl Parsed {
    fn validate(&mut self, schema_paths: &[PathBuf]) {
        DuplicateServiceUuid::validate(self.schemas.values(), &mut self.issues);

        for (schema_name, schema) in &self.schemas {
            let is_main_schema = *schema_name == self.main_schema;
            let mut validate = Validate::new(
                schema_name,
                &mut self.issues,
                &self.schemas,
                is_main_schema,
                schema_paths,
            );
            schema.validate(&mut validate);
        }
    }

    pub fn main_schema(&self) -> &Schema {
        self.get_schema(&self.main_schema).unwrap()
    }

    pub fn get_schema(&self, schema_name: &str) -> Option<&Schema> {
        self.schemas.get(schema_name)
    }

    pub fn errors(&self) -> &[Error] {
        self.issues.errors()
    }

    pub fn warnings(&self) -> &[Warning] {
        self.issues.warnings()
    }

    pub fn other_warnings(&self) -> &[Warning] {
        self.issues.other_warnings()
    }
}