aldrin_parser/error/
empty_enum.rs

1use super::Error;
2use crate::ast::{EnumVariant, Ident};
3use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
4use crate::validate::Validate;
5use crate::{Parsed, Span};
6
7#[derive(Debug)]
8pub struct EmptyEnum {
9    schema_name: String,
10    span: Span,
11    ident: Option<Ident>,
12}
13
14impl EmptyEnum {
15    pub(crate) fn validate(
16        vars: &[EnumVariant],
17        fallback: Option<&Ident>,
18        span: Span,
19        ident: Option<&Ident>,
20        validate: &mut Validate,
21    ) {
22        if !vars.is_empty() || fallback.is_some() {
23            return;
24        }
25
26        validate.add_error(Self {
27            schema_name: validate.schema_name().to_owned(),
28            span,
29            ident: ident.cloned(),
30        });
31    }
32
33    pub fn span(&self) -> Span {
34        self.span
35    }
36
37    pub fn ident(&self) -> Option<&Ident> {
38        self.ident.as_ref()
39    }
40}
41
42impl Diagnostic for EmptyEnum {
43    fn kind(&self) -> DiagnosticKind {
44        DiagnosticKind::Error
45    }
46
47    fn schema_name(&self) -> &str {
48        &self.schema_name
49    }
50
51    fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
52        let mut fmt = if let Some(ref ident) = self.ident {
53            Formatter::new(self, format!("empty enum `{}`", ident.value()))
54        } else {
55            Formatter::new(self, "empty inline enum")
56        };
57
58        if let Some(schema) = parsed.get_schema(&self.schema_name) {
59            fmt.main_block(schema, self.span.from, self.span, "");
60        }
61
62        fmt.note("empty enums are not supported")
63            .help("add at least one variant to the enum");
64        fmt.format()
65    }
66}
67
68impl From<EmptyEnum> for Error {
69    fn from(e: EmptyEnum) -> Self {
70        Self::EmptyEnum(e)
71    }
72}