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