use crate::meta_type::MetaType;
use crate::{de::Deserialize, ser::Serialize};
use crate::{AlgebraicType, WithTypespace};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[sats(crate = crate)]
pub struct ProductTypeElement {
pub name: Option<String>,
pub algebraic_type: AlgebraicType,
}
impl ProductTypeElement {
pub const fn new(algebraic_type: AlgebraicType, name: Option<String>) -> Self {
Self { algebraic_type, name }
}
pub fn new_named(algebraic_type: AlgebraicType, name: impl Into<String>) -> Self {
Self::new(algebraic_type, Some(name.into()))
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn has_name(&self, name: &str) -> bool {
self.name() == Some(name)
}
}
impl MetaType for ProductTypeElement {
fn meta_type() -> AlgebraicType {
AlgebraicType::product([
("name", AlgebraicType::option(AlgebraicType::String)),
("algebraic_type", AlgebraicType::ZERO_REF),
])
}
}
impl From<AlgebraicType> for ProductTypeElement {
fn from(value: AlgebraicType) -> Self {
ProductTypeElement::new(value, None)
}
}
impl<'a> WithTypespace<'a, ProductTypeElement> {
#[inline]
pub fn algebraic_type(&self) -> WithTypespace<'a, AlgebraicType> {
self.with(&self.ty().algebraic_type)
}
}