aldrin_parser/error/
invalid_enum_variant_id.rs

1use super::Error;
2use crate::ast::{EnumVariant, Ident, LitPosInt};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6
7#[derive(Debug)]
8pub struct InvalidEnumVariantId {
9    schema_name: String,
10    id: LitPosInt,
11    var_ident: Ident,
12}
13
14impl InvalidEnumVariantId {
15    pub(crate) fn validate(var: &EnumVariant, validate: &mut Validate) {
16        if var.id().value().parse::<u32>().is_ok() {
17            return;
18        }
19
20        validate.add_error(Self {
21            schema_name: validate.schema_name().to_owned(),
22            id: var.id().clone(),
23            var_ident: var.name().clone(),
24        });
25    }
26
27    pub fn id(&self) -> &LitPosInt {
28        &self.id
29    }
30
31    pub fn variant_ident(&self) -> &Ident {
32        &self.var_ident
33    }
34}
35
36impl Diagnostic for InvalidEnumVariantId {
37    fn kind(&self) -> DiagnosticKind {
38        DiagnosticKind::Error
39    }
40
41    fn schema_name(&self) -> &str {
42        &self.schema_name
43    }
44
45    fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
46        let mut fmt = Formatter::new(
47            self,
48            format!(
49                "invalid id `{}` for enum variant `{}`",
50                self.id.value(),
51                self.var_ident.value(),
52            ),
53        );
54
55        if let Some(schema) = parsed.get_schema(&self.schema_name) {
56            fmt.main_block(
57                schema,
58                self.id.span().from,
59                self.id.span(),
60                "id defined here",
61            );
62        }
63
64        fmt.help("ids must be u32 values in the range from 0 to 4294967295");
65        fmt.format()
66    }
67}
68
69impl From<InvalidEnumVariantId> for Error {
70    fn from(e: InvalidEnumVariantId) -> Self {
71        Self::InvalidEnumVariantId(e)
72    }
73}