use std::collections::{BTreeMap, HashSet};
use derive_more::{Deref, Display};
use serde::{Deserialize, Serialize};
use tempest_core::tempest_str::TempestStr;
use crate::types::TempestType;
#[derive(Debug, Clone)]
pub(crate) struct FlatField {
pub name: TempestStr<'static>,
pub ty: TempestType,
pub type_args: Vec<TypeExpr>,
}
#[derive(
Debug,
Default,
Display,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Deref,
Serialize,
Deserialize,
)]
pub struct FieldId(pub u32);
#[derive(
Debug,
Default,
Display,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Deref,
Serialize,
Deserialize,
)]
pub struct TypeId(pub u32);
#[derive(
Debug,
Default,
Display,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Deref,
Serialize,
Deserialize,
)]
pub struct TableId(pub u32);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TypeExpr {
Primitive(TempestType),
Ref(TypeId, Vec<TypeExpr>),
GenericParam(u32),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldDef {
pub name: TempestStr<'static>,
pub ty: TypeExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructSchema {
pub database_id: Option<DatabaseId>,
pub name: TempestStr<'static>,
pub generic_params: Vec<TempestStr<'static>>,
pub fields: BTreeMap<FieldId, FieldDef>,
}
#[derive(
Debug,
Default,
Display,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Deref,
Serialize,
Deserialize,
)]
pub struct VariantId(pub u32);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnumVariantDef {
pub name: TempestStr<'static>,
pub fields: Vec<TypeExpr>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnumSchema {
pub database_id: Option<DatabaseId>,
pub name: TempestStr<'static>,
pub generic_params: Vec<TempestStr<'static>>,
pub variants: BTreeMap<VariantId, EnumVariantDef>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TypeSchema {
Struct(StructSchema),
Enum(EnumSchema),
}
impl TypeSchema {
pub fn database_id(&self) -> Option<DatabaseId> {
match self {
Self::Struct(s) => s.database_id,
Self::Enum(e) => e.database_id,
}
}
pub fn name(&self) -> &TempestStr<'static> {
match self {
Self::Struct(s) => &s.name,
Self::Enum(e) => &e.name,
}
}
pub fn as_struct(&self) -> Option<&StructSchema> {
if let Self::Struct(s) = self { Some(s) } else { None }
}
pub fn as_enum(&self) -> Option<&EnumSchema> {
if let Self::Enum(e) = self { Some(e) } else { None }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableSchema {
pub database_id: DatabaseId,
pub name: TempestStr<'static>,
pub type_id: TypeId,
pub generic_args: Vec<TypeExpr>,
pub primary_key: Vec<Vec<FieldId>>,
}
#[derive(
Debug,
Default,
Display,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Deref,
Serialize,
Deserialize,
)]
pub struct DatabaseId(pub u32);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseSchema {
pub name: TempestStr<'static>,
pub tables: HashSet<TableId>,
pub types: HashSet<TypeId>,
}
impl DatabaseSchema {
pub(crate) fn new(name: TempestStr<'static>) -> Self {
Self {
name,
tables: HashSet::new(),
types: HashSet::new(),
}
}
}