aldrin_parser/error/
const_int_not_found.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
use super::Error;
use crate::ast::{NamedRef, NamedRefKind};
use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
use crate::validate::Validate;
use crate::{util, Parsed};

#[derive(Debug)]
pub struct ConstIntNotFound {
    schema_name: String,
    named_ref: NamedRef,
    candidate: Option<String>,
}

impl ConstIntNotFound {
    pub(crate) fn validate(named_ref: &NamedRef, validate: &mut Validate) {
        let (schema, ident) = match named_ref.kind() {
            NamedRefKind::Intern(ident) => (validate.get_current_schema(), ident),

            NamedRefKind::Extern(schema, ident) => {
                let Some(schema) = validate.get_schema(schema.value()) else {
                    return;
                };

                (schema, ident)
            }
        };

        for def in schema.definitions() {
            if def.name().value() == ident.value() {
                return;
            }
        }

        let candidate = util::did_you_mean_const_int(schema, ident.value()).map(ToOwned::to_owned);

        validate.add_error(Self {
            schema_name: validate.schema_name().to_owned(),
            named_ref: named_ref.clone(),
            candidate,
        });
    }

    pub fn named_ref(&self) -> &NamedRef {
        &self.named_ref
    }

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

impl Diagnostic for ConstIntNotFound {
    fn kind(&self) -> DiagnosticKind {
        DiagnosticKind::Error
    }

    fn schema_name(&self) -> &str {
        &self.schema_name
    }

    fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
        let (mut fmt, schema) = match self.named_ref.kind() {
            NamedRefKind::Intern(ident) => (
                Formatter::new(
                    self,
                    format!("integer constant `{}` not found", ident.value()),
                ),
                None,
            ),

            NamedRefKind::Extern(schema, ident) => (
                Formatter::new(
                    self,
                    format!(
                        "integer constant `{}::{}` not found",
                        schema.value(),
                        ident.value()
                    ),
                ),
                Some(schema),
            ),
        };

        if let Some(schema) = parsed.get_schema(&self.schema_name) {
            fmt.main_block(
                schema,
                self.named_ref.span().from,
                self.named_ref.span(),
                "integer constant used here",
            );
        }

        if let Some(ref candidate) = self.candidate {
            match schema {
                Some(schema) => {
                    fmt.help(format!("did you mean `{}::{candidate}`?", schema.value()));
                }

                None => {
                    fmt.help(format!("did you mean `{candidate}`?"));
                }
            }
        }

        fmt.format()
    }
}

impl From<ConstIntNotFound> for Error {
    fn from(e: ConstIntNotFound) -> Self {
        Self::ConstIntNotFound(e)
    }
}