aldrin_parser/
schema.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::ast::{Definition, ImportStmt, SchemaName};
use crate::error::{DuplicateDefinition, InvalidSchemaName, InvalidSyntax, IoError};
use crate::grammar::{Grammar, Rule};
use crate::issues::Issues;
use crate::validate::Validate;
use crate::warning::{DuplicateImport, NonSnakeCaseSchemaName};
use pest::Parser;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct Schema {
    name: String,
    path: PathBuf,
    source: Option<String>,
    imports: Vec<ImportStmt>,
    defs: Vec<Definition>,
}

impl Schema {
    pub(crate) fn parse<P>(schema_path: P, issues: &mut Issues) -> Self
    where
        P: AsRef<Path>,
    {
        let schema_path = schema_path.as_ref();

        let mut schema = Self {
            name: Self::parse_file_name(schema_path, issues),
            path: schema_path.to_owned(),
            source: None,
            imports: Vec::new(),
            defs: Vec::new(),
        };

        let source = {
            let mut file = match File::open(schema_path) {
                Ok(file) => file,
                Err(e) => {
                    issues.add_error(IoError::new(&schema.name, e));
                    return schema;
                }
            };

            let mut source = String::new();
            if let Err(e) = file.read_to_string(&mut source) {
                issues.add_error(IoError::new(&schema.name, e));
                return schema;
            }

            source
        };

        let pairs = match Grammar::parse(Rule::file, &source) {
            Ok(pairs) => pairs,
            Err(e) => {
                schema.source = Some(source);
                issues.add_error(InvalidSyntax::new(&schema.name, e));
                return schema;
            }
        };

        for pair in pairs {
            match pair.as_rule() {
                Rule::import_stmt => schema.imports.push(ImportStmt::parse(pair)),
                Rule::def => schema.defs.push(Definition::parse(pair)),
                Rule::EOI => break,
                _ => unreachable!(),
            }
        }

        schema.source = Some(source);
        schema
    }

    fn parse_file_name<P>(path: P, issues: &mut Issues) -> String
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();

        let file_stem = match path.file_stem() {
            Some(file_stem) => file_stem,
            None => {
                let schema_name = path.to_string_lossy().into_owned();
                issues.add_error(InvalidSchemaName::new(&schema_name));
                return schema_name;
            }
        };

        let file_stem_str = match file_stem.to_str() {
            Some(file_stem_str) => file_stem_str,
            None => {
                let schema_name = file_stem.to_string_lossy().into_owned();
                issues.add_error(InvalidSchemaName::new(&schema_name));
                return schema_name;
            }
        };

        let mut schema_name_pairs = match Grammar::parse(Rule::schema_name, file_stem_str) {
            Ok(schema_name_pairs) => schema_name_pairs,
            Err(_) => {
                issues.add_error(InvalidSchemaName::new(file_stem_str));
                return file_stem_str.to_owned();
            }
        };

        if schema_name_pairs.as_str() != file_stem_str {
            issues.add_error(InvalidSchemaName::new(file_stem_str));
            return file_stem_str.to_owned();
        }

        let schema_name = SchemaName::parse(schema_name_pairs.next().unwrap());
        schema_name.value().to_owned()
    }

    pub(crate) fn validate(&self, validate: &mut Validate) {
        if self.source.is_none() {
            return;
        }

        DuplicateDefinition::validate(self, validate);
        NonSnakeCaseSchemaName::validate(&self.name, validate);
        DuplicateImport::validate(self, validate);

        for import in &self.imports {
            import.validate(validate);
        }

        for def in &self.defs {
            def.validate(validate);
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn source(&self) -> Option<&str> {
        self.source.as_deref()
    }

    pub fn imports(&self) -> &[ImportStmt] {
        &self.imports
    }

    pub fn definitions(&self) -> &[Definition] {
        &self.defs
    }
}