use crate::{
cache::ModuleCache,
load::ModuleLoader,
model::{
annotations::{Annotation, AnnotationOnlyBody, HasAnnotations},
check::Validate,
definitions::HasVariants,
identifiers::{Identifier, IdentifierReference},
modules::Module,
References, Span,
},
};
use std::{collections::HashSet, fmt::Debug};
use tracing::warn;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct EnumDef {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
name: Identifier,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
body: Option<EnumBody>,
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct EnumBody {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))]
annotations: Vec<Annotation>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))]
variants: Vec<ValueVariant>, }
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct ValueVariant {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
name: Identifier,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
body: Option<AnnotationOnlyBody>,
}
impl_has_name_for!(EnumDef);
impl_has_optional_body_for!(EnumDef, EnumBody);
impl_has_source_span_for!(EnumDef);
impl_maybe_invalid_for!(EnumDef; exists body);
impl_validate_for!(EnumDef => delegate optional body);
impl_annotation_builder!(EnumDef, optional body);
impl References for EnumDef {
fn referenced_annotations<'a>(&'a self, names: &mut HashSet<&'a IdentifierReference>) {
self.body
.as_ref()
.map(|b| b.referenced_annotations(names))
.unwrap_or_default()
}
}
impl EnumDef {
pub fn new(name: Identifier) -> Self {
Self {
span: None,
name,
body: None,
}
}
}
impl_has_annotations_for!(EnumBody);
impl_has_source_span_for!(EnumBody);
impl_has_variants_for!(EnumBody, ValueVariant);
impl References for EnumBody {
fn referenced_annotations<'a>(&'a self, names: &mut HashSet<&'a IdentifierReference>) {
self.variants
.iter()
.for_each(|v| v.referenced_annotations(names));
}
}
impl_validate_for_annotations_and_variants!(EnumBody);
impl_has_name_for!(ValueVariant);
impl_has_optional_body_for!(ValueVariant);
impl_has_source_span_for!(ValueVariant);
impl_annotation_builder!(ValueVariant, optional body);
impl Validate for ValueVariant {
fn validate(
&self,
_top: &Module,
_cache: &ModuleCache,
_loader: &impl ModuleLoader,
_check_constraints: bool,
) {
warn!("Missing validation for ValueVariant values.");
}
}
impl References for ValueVariant {
fn referenced_annotations<'a>(&'a self, names: &mut HashSet<&'a IdentifierReference>) {
self.body
.as_ref()
.map(|b| b.referenced_annotations(names))
.unwrap_or_default()
}
}
impl ValueVariant {
pub fn new(name: Identifier) -> Self {
Self {
span: None,
name,
body: None,
}
}
}