aldrin_parser/warning/
duplicate_import.rs1use super::Warning;
2use crate::ast::ImportStmt;
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::{util, Parsed, Schema, Span};
6
7#[derive(Debug)]
8pub struct DuplicateImport {
9 schema_name: String,
10 duplicate: ImportStmt,
11 first: Span,
12}
13
14impl DuplicateImport {
15 pub(crate) fn validate(schema: &Schema, validate: &mut Validate) {
16 util::find_duplicates(
17 schema.imports(),
18 |import| import.schema_name().value(),
19 |duplicate, first| {
20 validate.add_warning(Self {
21 schema_name: validate.schema_name().to_owned(),
22 duplicate: duplicate.clone(),
23 first: first.span(),
24 });
25 },
26 );
27 }
28
29 pub fn duplicate(&self) -> &ImportStmt {
30 &self.duplicate
31 }
32
33 pub fn first(&self) -> Span {
34 self.first
35 }
36}
37
38impl Diagnostic for DuplicateImport {
39 fn kind(&self) -> DiagnosticKind {
40 DiagnosticKind::Warning
41 }
42
43 fn schema_name(&self) -> &str {
44 &self.schema_name
45 }
46
47 fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
48 let mut fmt = Formatter::new(
49 self,
50 format!(
51 "duplicate import of schema `{}`",
52 self.duplicate.schema_name().value()
53 ),
54 );
55
56 if let Some(schema) = parsed.get_schema(&self.schema_name) {
57 fmt.main_block(
58 schema,
59 self.duplicate.span().from,
60 self.duplicate.span(),
61 "duplicate import",
62 );
63 fmt.info_block(schema, self.first.from, self.first, "first imported here");
64 }
65
66 fmt.help("remove the duplicate import statement");
67 fmt.format()
68 }
69}
70
71impl From<DuplicateImport> for Warning {
72 fn from(w: DuplicateImport) -> Self {
73 Self::DuplicateImport(w)
74 }
75}