use std::borrow::Cow;
use crate::{
datatype::DataType, DeprecatedType, GenericType, NamedDataType, NamedFields, SpectaID,
UnnamedFields,
};
#[derive(Debug, Clone, PartialEq)]
pub struct EnumType {
pub(crate) name: Cow<'static, str>,
pub(crate) sid: Option<SpectaID>,
pub(crate) skip_bigint_checks: bool,
pub(crate) repr: EnumRepr,
pub(crate) generics: Vec<GenericType>,
pub(crate) variants: Vec<(Cow<'static, str>, EnumVariant)>,
}
impl EnumType {
pub fn to_anonymous(self) -> DataType {
DataType::Enum(self)
}
pub fn to_named(self, name: impl Into<Cow<'static, str>>) -> NamedDataType {
NamedDataType {
name: name.into(),
docs: Cow::Borrowed(""),
deprecated: None,
ext: None,
inner: DataType::Enum(self),
}
}
pub fn name(&self) -> &Cow<'static, str> {
&self.name
}
pub fn repr(&self) -> &EnumRepr {
&self.repr
}
pub fn variants(&self) -> &Vec<(Cow<'static, str>, EnumVariant)> {
&self.variants
}
pub fn generics(&self) -> &Vec<GenericType> {
&self.generics
}
}
impl From<EnumType> for DataType {
fn from(t: EnumType) -> Self {
Self::Enum(t)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum EnumRepr {
Untagged,
External,
Internal {
tag: Cow<'static, str>,
},
Adjacent {
tag: Cow<'static, str>,
content: Cow<'static, str>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumVariant {
pub(crate) skip: bool,
pub(crate) docs: Cow<'static, str>,
pub(crate) deprecated: Option<DeprecatedType>,
pub(crate) inner: EnumVariants,
}
impl EnumVariant {
pub fn skip(&self) -> bool {
self.skip
}
pub fn docs(&self) -> &Cow<'static, str> {
&self.docs
}
pub fn deprecated(&self) -> Option<&DeprecatedType> {
self.deprecated.as_ref()
}
pub fn inner(&self) -> &EnumVariants {
&self.inner
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum EnumVariants {
Unit,
Named(NamedFields),
Unnamed(UnnamedFields),
}