aldrin_parser/error/
invalid_service_uuid.rs

1use super::Error;
2use crate::ast::{Ident, LitUuid, ServiceDef};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::Parsed;
6use uuid::Uuid;
7
8#[derive(Debug)]
9pub struct InvalidServiceUuid {
10    schema_name: String,
11    uuid: LitUuid,
12    svc_ident: Ident,
13}
14
15impl InvalidServiceUuid {
16    pub(crate) fn validate(service_def: &ServiceDef, validate: &mut Validate) {
17        if !service_def.uuid().value().is_nil() {
18            return;
19        }
20
21        validate.add_error(Self {
22            schema_name: validate.schema_name().to_owned(),
23            uuid: service_def.uuid().clone(),
24            svc_ident: service_def.name().clone(),
25        });
26    }
27
28    pub fn uuid(&self) -> &LitUuid {
29        &self.uuid
30    }
31
32    pub fn service_ident(&self) -> &Ident {
33        &self.svc_ident
34    }
35}
36
37impl Diagnostic for InvalidServiceUuid {
38    fn kind(&self) -> DiagnosticKind {
39        DiagnosticKind::Error
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                "invalid uuid `{}` for service `{}`",
51                Uuid::nil(),
52                self.svc_ident.value()
53            ),
54        );
55
56        if let Some(schema) = parsed.get_schema(&self.schema_name) {
57            fmt.main_block(schema, self.uuid.span().from, self.uuid.span(), "nil uuid");
58        }
59
60        fmt.note("the nil uuid cannot be used for services");
61        fmt.help(format!("use e.g. `{}`", Uuid::new_v4()));
62
63        fmt.format()
64    }
65}
66
67impl From<InvalidServiceUuid> for Error {
68    fn from(e: InvalidServiceUuid) -> Self {
69        Self::InvalidServiceUuid(e)
70    }
71}