aldrin_parser/error/
import_not_found.rs1use super::Error;
2use crate::ast::ImportStmt;
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6use std::path::PathBuf;
7
8#[derive(Debug)]
9pub struct ImportNotFound {
10 schema_name: String,
11 import: ImportStmt,
12 tried: Vec<PathBuf>,
13}
14
15impl ImportNotFound {
16 pub(crate) fn validate(import_stmt: &ImportStmt, validate: &mut Validate) {
17 if validate
18 .get_schema(import_stmt.schema_name().value())
19 .is_some()
20 {
21 return;
22 }
23
24 let tried = validate
25 .schema_paths()
26 .iter()
27 .map(|p| {
28 let mut tried = p.clone();
29 tried.push(import_stmt.schema_name().value());
30 tried.set_extension("aldrin");
31 tried
32 })
33 .collect();
34
35 validate.add_error(Self {
36 schema_name: validate.schema_name().to_owned(),
37 import: import_stmt.clone(),
38 tried,
39 });
40 }
41
42 pub fn import(&self) -> &ImportStmt {
43 &self.import
44 }
45
46 pub fn tried(&self) -> &[PathBuf] {
47 &self.tried
48 }
49}
50
51impl Diagnostic for ImportNotFound {
52 fn kind(&self) -> DiagnosticKind {
53 DiagnosticKind::Error
54 }
55
56 fn schema_name(&self) -> &str {
57 &self.schema_name
58 }
59
60 fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
61 let mut fmt = Formatter::new(
62 self,
63 format!(
64 "file not found for schema `{}`",
65 self.import.schema_name().value()
66 ),
67 );
68
69 if let Some(schema) = parsed.get_schema(&self.schema_name) {
70 fmt.main_block(schema, self.import.span().from, self.import.span(), "");
71 }
72
73 for tried in &self.tried {
74 fmt.note(format!("tried `{}`", tried.display()));
75 }
76
77 if self.tried.is_empty() {
78 fmt.note("an include directory may be missing");
79 } else {
80 fmt.note("an include directory may be missing or incorrect");
81 }
82 fmt.format()
83 }
84}
85
86impl From<ImportNotFound> for Error {
87 fn from(e: ImportNotFound) -> Self {
88 Self::ImportNotFound(e)
89 }
90}