aldrin_parser/error/
invalid_const_value.rs

1use super::Error;
2use crate::ast::ConstValue;
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6
7#[derive(Debug)]
8pub struct InvalidConstValue {
9    schema_name: String,
10    const_value: ConstValue,
11}
12
13impl InvalidConstValue {
14    pub(crate) fn validate(const_value: &ConstValue, validate: &mut Validate) {
15        let is_err = match const_value {
16            ConstValue::U8(v) => v.value().parse::<u8>().is_err(),
17            ConstValue::I8(v) => v.value().parse::<i8>().is_err(),
18            ConstValue::U16(v) => v.value().parse::<u16>().is_err(),
19            ConstValue::I16(v) => v.value().parse::<i16>().is_err(),
20            ConstValue::U32(v) => v.value().parse::<u32>().is_err(),
21            ConstValue::I32(v) => v.value().parse::<i32>().is_err(),
22            ConstValue::U64(v) => v.value().parse::<u64>().is_err(),
23            ConstValue::I64(v) => v.value().parse::<i64>().is_err(),
24            ConstValue::String(_) | ConstValue::Uuid(_) => false,
25        };
26
27        if is_err {
28            validate.add_error(Self {
29                schema_name: validate.schema_name().to_owned(),
30                const_value: const_value.clone(),
31            });
32        }
33    }
34
35    pub fn const_value(&self) -> &ConstValue {
36        &self.const_value
37    }
38}
39
40impl Diagnostic for InvalidConstValue {
41    fn kind(&self) -> DiagnosticKind {
42        DiagnosticKind::Error
43    }
44
45    fn schema_name(&self) -> &str {
46        &self.schema_name
47    }
48
49    fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
50        let (kind, value, min, max) = match self.const_value {
51            ConstValue::U8(ref v) => ("u8", v, u8::MIN as i64, u8::MAX as u64),
52            ConstValue::I8(ref v) => ("i8", v, i8::MIN as i64, i8::MAX as u64),
53            ConstValue::U16(ref v) => ("u16", v, u16::MIN as i64, u16::MAX as u64),
54            ConstValue::I16(ref v) => ("i16", v, i16::MIN as i64, i16::MAX as u64),
55            ConstValue::U32(ref v) => ("u32", v, u32::MIN as i64, u32::MAX as u64),
56            ConstValue::I32(ref v) => ("i32", v, i32::MIN as i64, i32::MAX as u64),
57            ConstValue::U64(ref v) => ("u64", v, u64::MIN as i64, u64::MAX),
58            ConstValue::I64(ref v) => ("i64", v, i64::MIN, i64::MAX as u64),
59            ConstValue::String(_) | ConstValue::Uuid(_) => unreachable!(),
60        };
61
62        let mut fmt = Formatter::new(
63            self,
64            format!("invalid constant {kind} value `{}`", value.value()),
65        );
66
67        if let Some(schema) = parsed.get_schema(&self.schema_name) {
68            fmt.main_block(
69                schema,
70                value.span().from,
71                value.span(),
72                "constant value defined here",
73            );
74        }
75
76        fmt.help(format!(
77            "{kind} values must be in the range from {min} to {max}"
78        ));
79        fmt.format()
80    }
81}
82
83impl From<InvalidConstValue> for Error {
84    fn from(e: InvalidConstValue) -> Self {
85        Self::InvalidConstValue(e)
86    }
87}