use std::{borrow::Cow, collections::BTreeMap};
mod r#enum;
mod literal;
mod object;
mod primitive;
mod tuple;
pub use literal::*;
pub use object::*;
pub use primitive::*;
pub use r#enum::*;
pub use tuple::*;
use crate::{ImplLocation, TypeSid};
pub type TypeDefs = BTreeMap<TypeSid, Option<NamedDataType>>;
pub struct DefOpts<'a> {
pub parent_inline: bool,
pub type_map: &'a mut TypeDefs,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub enum DataType {
Any,
Primitive(PrimitiveType),
Literal(LiteralType),
List(Box<DataType>),
Nullable(Box<DataType>),
Record(Box<(DataType, DataType)>),
Named(NamedDataType),
Object(ObjectType),
Enum(EnumType),
Tuple(TupleType),
Reference(DataTypeReference),
Generic(GenericType),
}
#[derive(Debug, Clone, PartialEq)]
pub struct NamedDataType {
pub name: &'static str,
pub sid: Option<TypeSid>,
pub impl_location: Option<ImplLocation>,
pub comments: &'static [&'static str],
pub export: Option<bool>,
pub deprecated: Option<&'static str>,
pub item: NamedDataTypeItem,
}
impl From<NamedDataType> for DataType {
fn from(t: NamedDataType) -> Self {
Self::Named(t)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum NamedDataTypeItem {
Object(ObjectType),
Enum(EnumType),
Tuple(TupleType),
}
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct DataTypeReference {
pub name: &'static str,
pub sid: TypeSid,
pub generics: Vec<DataType>,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct GenericType(pub &'static str);
impl From<GenericType> for DataType {
fn from(t: GenericType) -> Self {
Self::Generic(t)
}
}
impl<T: Into<DataType> + 'static> From<Vec<T>> for DataType {
fn from(t: Vec<T>) -> Self {
DataType::Enum(EnumType::Untagged {
variants: t
.into_iter()
.map(|t| {
EnumVariant::Unnamed(TupleType {
fields: vec![t.into()],
generics: vec![],
})
})
.collect(),
generics: vec![],
})
}
}
impl<T: Into<DataType> + 'static> From<Option<T>> for DataType {
fn from(t: Option<T>) -> Self {
t.map(Into::into)
.unwrap_or_else(|| LiteralType::None.into())
}
}
impl<'a> From<&'a str> for DataType {
fn from(t: &'a str) -> Self {
LiteralType::String(t.to_string()).into()
}
}
impl From<String> for DataType {
fn from(t: String) -> Self {
LiteralType::String(t).into()
}
}
impl From<Cow<'static, str>> for DataType {
fn from(t: Cow<'static, str>) -> Self {
LiteralType::String(t.to_string()).into()
}
}