aldrin_parser/warning/
non_camel_case_enum.rs

1use super::Warning;
2use crate::ast::{EnumDef, Ident};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6use heck::ToUpperCamelCase;
7
8#[derive(Debug)]
9pub struct NonCamelCaseEnum {
10    schema_name: String,
11    camel_case: String,
12    ident: Ident,
13}
14
15impl NonCamelCaseEnum {
16    pub(crate) fn validate(enum_def: &EnumDef, validate: &mut Validate) {
17        let camel_case = enum_def.name().value().to_upper_camel_case();
18        if enum_def.name().value() != camel_case {
19            validate.add_warning(Self {
20                schema_name: validate.schema_name().to_owned(),
21                camel_case,
22                ident: enum_def.name().clone(),
23            });
24        }
25    }
26
27    pub fn camel_case(&self) -> &str {
28        &self.camel_case
29    }
30
31    pub fn ident(&self) -> &Ident {
32        &self.ident
33    }
34}
35
36impl Diagnostic for NonCamelCaseEnum {
37    fn kind(&self) -> DiagnosticKind {
38        DiagnosticKind::Warning
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                "enum `{}` should have a camel-case name",
50                self.ident.value()
51            ),
52        );
53
54        if let Some(schema) = parsed.get_schema(&self.schema_name) {
55            fmt.main_block(schema, self.ident.span().from, self.ident.span(), "");
56        }
57
58        fmt.help(format!(
59            "consider renaming enum `{}` to `{}`",
60            self.ident.value(),
61            self.camel_case
62        ));
63        fmt.format()
64    }
65}
66
67impl From<NonCamelCaseEnum> for Warning {
68    fn from(w: NonCamelCaseEnum) -> Self {
69        Self::NonCamelCaseEnum(w)
70    }
71}