aldrin_parser/warning/
non_camel_case_service.rs1use super::Warning;
2use crate::ast::{Ident, ServiceDef};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6use heck::ToUpperCamelCase;
7
8#[derive(Debug)]
9pub struct NonCamelCaseService {
10 schema_name: String,
11 camel_case: String,
12 ident: Ident,
13}
14
15impl NonCamelCaseService {
16 pub(crate) fn validate(service_def: &ServiceDef, validate: &mut Validate) {
17 let camel_case = service_def.name().value().to_upper_camel_case();
18 if service_def.name().value() != camel_case {
19 validate.add_warning(Self {
20 schema_name: validate.schema_name().to_owned(),
21 camel_case,
22 ident: service_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 NonCamelCaseService {
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 "service `{}` 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 service `{}` to `{}`",
60 self.ident.value(),
61 self.camel_case
62 ));
63 fmt.format()
64 }
65}
66
67impl From<NonCamelCaseService> for Warning {
68 fn from(w: NonCamelCaseService) -> Self {
69 Self::NonCamelCaseService(w)
70 }
71}