aldrin_parser/warning/
non_shouty_snake_case_const.rs1use super::Warning;
2use crate::ast::{ConstDef, Ident};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6use heck::ToShoutySnakeCase;
7
8#[derive(Debug)]
9pub struct NonShoutySnakeCaseConst {
10 schema_name: String,
11 shouty_snake_case: String,
12 ident: Ident,
13}
14
15impl NonShoutySnakeCaseConst {
16 pub(crate) fn validate(const_def: &ConstDef, validate: &mut Validate) {
17 let shouty_snake_case = const_def.name().value().to_shouty_snake_case();
18 if const_def.name().value() != shouty_snake_case {
19 validate.add_warning(Self {
20 schema_name: validate.schema_name().to_owned(),
21 shouty_snake_case,
22 ident: const_def.name().clone(),
23 });
24 }
25 }
26
27 pub fn shouty_snake_case(&self) -> &str {
28 &self.shouty_snake_case
29 }
30
31 pub fn ident(&self) -> &Ident {
32 &self.ident
33 }
34}
35
36impl Diagnostic for NonShoutySnakeCaseConst {
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 "constant `{}` should have an upper-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 constant `{}` to `{}`",
60 self.ident.value(),
61 self.shouty_snake_case
62 ));
63 fmt.format()
64 }
65}
66
67impl From<NonShoutySnakeCaseConst> for Warning {
68 fn from(w: NonShoutySnakeCaseConst) -> Self {
69 Self::NonShoutySnakeCaseConst(w)
70 }
71}