use crate::ParserError;
use crate::validator::ValidationError;
use crate::validator::imports::attest_imports;
use crate::validator::type_resolver::resolve_data_type;
use misty_ast::{Definition, File};
use std::collections::HashMap;
pub struct Workspace {
package_local_modules: HashMap<String, File>,
validated: bool,
}
impl Default for Workspace {
fn default() -> Self {
Self::new()
}
}
impl Workspace {
pub fn new() -> Self {
Self {
package_local_modules: HashMap::new(),
validated: false,
}
}
pub fn add_local_module(&mut self, module_path: &str, module: File) {
self.package_local_modules
.insert(module_path.to_string(), module);
}
#[tracing::instrument(skip(self))]
pub fn validate(&mut self) -> Result<(), ParserError> {
for file in self.package_local_modules.values() {
self.validate_file(file)?;
}
self.validated = true;
Ok(())
}
#[tracing::instrument(skip(self, file))]
fn validate_file(&self, file: &File) -> Result<(), ValidationError> {
attest_imports(&file.imports, &self.package_local_modules)?;
for definition in &file.definitions {
match definition {
Definition::Enum(_) => (),
Definition::Interface(interface) => {
for function in &interface.functions {
resolve_data_type(&self.package_local_modules, file, &function.input.1)?;
if let Some((_, argument)) = &function.output {
resolve_data_type(&self.package_local_modules, file, argument)?;
}
}
}
Definition::Schema(schema) => {
for field in &schema.fields {
resolve_data_type(&self.package_local_modules, file, &field.field_type)?;
}
}
}
}
Ok(())
}
pub fn package_local_modules(&self) -> &HashMap<String, File> {
&self.package_local_modules
}
pub fn validated(&self) -> bool {
self.validated
}
}