aldrin_parser/error/
invalid_struct_field_id.rs1use super::Error;
2use crate::ast::{Ident, LitPosInt, StructField};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6
7#[derive(Debug)]
8pub struct InvalidStructFieldId {
9 schema_name: String,
10 id: LitPosInt,
11 field_ident: Ident,
12}
13
14impl InvalidStructFieldId {
15 pub(crate) fn validate(field: &StructField, validate: &mut Validate) {
16 if field.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: field.id().clone(),
23 field_ident: field.name().clone(),
24 });
25 }
26
27 pub fn id(&self) -> &LitPosInt {
28 &self.id
29 }
30
31 pub fn field_ident(&self) -> &Ident {
32 &self.field_ident
33 }
34}
35
36impl Diagnostic for InvalidStructFieldId {
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 struct field `{}`",
50 self.id.value(),
51 self.field_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<InvalidStructFieldId> for Error {
70 fn from(e: InvalidStructFieldId) -> Self {
71 Self::InvalidStructFieldId(e)
72 }
73}