aldrin_parser/error/
const_int_not_found.rs1use super::Error;
2use crate::ast::{NamedRef, NamedRefKind};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::{util, Parsed};
6
7#[derive(Debug)]
8pub struct ConstIntNotFound {
9 schema_name: String,
10 named_ref: NamedRef,
11 candidate: Option<String>,
12}
13
14impl ConstIntNotFound {
15 pub(crate) fn validate(named_ref: &NamedRef, validate: &mut Validate) {
16 let (schema, ident) = match named_ref.kind() {
17 NamedRefKind::Intern(ident) => (validate.get_current_schema(), ident),
18
19 NamedRefKind::Extern(schema, ident) => {
20 let Some(schema) = validate.get_schema(schema.value()) else {
21 return;
22 };
23
24 (schema, ident)
25 }
26 };
27
28 for def in schema.definitions() {
29 if def.name().value() == ident.value() {
30 return;
31 }
32 }
33
34 let candidate = util::did_you_mean_const_int(schema, ident.value()).map(ToOwned::to_owned);
35
36 validate.add_error(Self {
37 schema_name: validate.schema_name().to_owned(),
38 named_ref: named_ref.clone(),
39 candidate,
40 });
41 }
42
43 pub fn named_ref(&self) -> &NamedRef {
44 &self.named_ref
45 }
46
47 pub fn candidate(&self) -> Option<&str> {
48 self.candidate.as_deref()
49 }
50}
51
52impl Diagnostic for ConstIntNotFound {
53 fn kind(&self) -> DiagnosticKind {
54 DiagnosticKind::Error
55 }
56
57 fn schema_name(&self) -> &str {
58 &self.schema_name
59 }
60
61 fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
62 let (mut fmt, schema) = match self.named_ref.kind() {
63 NamedRefKind::Intern(ident) => (
64 Formatter::new(
65 self,
66 format!("integer constant `{}` not found", ident.value()),
67 ),
68 None,
69 ),
70
71 NamedRefKind::Extern(schema, ident) => (
72 Formatter::new(
73 self,
74 format!(
75 "integer constant `{}::{}` not found",
76 schema.value(),
77 ident.value()
78 ),
79 ),
80 Some(schema),
81 ),
82 };
83
84 if let Some(schema) = parsed.get_schema(&self.schema_name) {
85 fmt.main_block(
86 schema,
87 self.named_ref.span().from,
88 self.named_ref.span(),
89 "integer constant used here",
90 );
91 }
92
93 if let Some(ref candidate) = self.candidate {
94 match schema {
95 Some(schema) => {
96 fmt.help(format!("did you mean `{}::{candidate}`?", schema.value()));
97 }
98
99 None => {
100 fmt.help(format!("did you mean `{candidate}`?"));
101 }
102 }
103 }
104
105 fmt.format()
106 }
107}
108
109impl From<ConstIntNotFound> for Error {
110 fn from(e: ConstIntNotFound) -> Self {
111 Self::ConstIntNotFound(e)
112 }
113}