use std::collections::HashMap;
use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ColumnType {
Id,
Text,
Bool,
Int,
Timestamp,
Json,
Bytes,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DefaultValue {
Bool(bool),
Int(i64),
Text(&'static str),
Now,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OnDelete {
Cascade,
SetNull,
Restrict,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Reference {
pub table: &'static str,
pub column: &'static str,
pub on_delete: OnDelete,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ColumnDef {
pub name: &'static str,
pub ty: ColumnType,
pub nullable: bool,
pub primary_key: bool,
pub unique: bool,
pub index: bool,
pub default: Option<DefaultValue>,
pub references: Option<Reference>,
pub input: bool,
pub returned: bool,
pub owner: &'static str,
}
impl ColumnDef {
pub const fn new(name: &'static str, ty: ColumnType) -> Self {
Self {
name,
ty,
nullable: false,
primary_key: false,
unique: false,
index: false,
default: None,
references: None,
input: false,
returned: true,
owner: "",
}
}
pub const fn primary_key(mut self) -> Self {
self.primary_key = true;
self
}
pub const fn nullable(mut self) -> Self {
self.nullable = true;
self
}
pub const fn unique(mut self) -> Self {
self.unique = true;
self
}
pub const fn index(mut self) -> Self {
self.index = true;
self
}
pub const fn default(mut self, value: DefaultValue) -> Self {
self.default = Some(value);
self
}
pub const fn references(
mut self,
table: &'static str,
column: &'static str,
on_delete: OnDelete,
) -> Self {
self.references = Some(Reference {
table,
column,
on_delete,
});
self
}
pub const fn input(mut self) -> Self {
self.input = true;
self
}
pub const fn hidden(mut self) -> Self {
self.returned = false;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableDef {
pub name: &'static str,
pub owner: &'static str,
pub columns: Vec<ColumnDef>,
positions: HashMap<&'static str, usize>,
}
impl TableDef {
fn new(name: &'static str, owner: &'static str) -> Self {
Self {
name,
owner,
columns: Vec::new(),
positions: HashMap::new(),
}
}
pub fn position(&self, column: &str) -> Option<usize> {
self.positions.get(column).copied()
}
pub fn column(&self, column: &str) -> Option<&ColumnDef> {
self.position(column).map(|i| &self.columns[i])
}
pub fn primary_key(&self) -> Option<&ColumnDef> {
self.columns.iter().find(|c| c.primary_key)
}
}
#[derive(Clone, Debug, Default)]
pub struct Schema {
tables: Vec<Arc<TableDef>>,
by_name: HashMap<&'static str, usize>,
}
impl Schema {
pub fn tables(&self) -> &[Arc<TableDef>] {
&self.tables
}
pub fn table(&self, name: &str) -> Option<&Arc<TableDef>> {
self.by_name.get(name).map(|&i| &self.tables[i])
}
pub fn expect(&self, name: &str) -> &Arc<TableDef> {
self.table(name)
.unwrap_or_else(|| panic!("fastauth: table `{name}` is not in the schema"))
}
}
#[derive(Debug, Default)]
pub struct SchemaBuilder {
tables: Vec<TableDef>,
pub(crate) owner: &'static str,
pub(crate) errors: Vec<String>,
}
impl SchemaBuilder {
pub(crate) fn new() -> Self {
Self {
owner: "core",
..Self::default()
}
}
pub fn table(&mut self, name: &'static str, columns: &[ColumnDef]) -> &mut Self {
if let Some(existing) = self.tables.iter().find(|t| t.name == name) {
self.errors.push(format!(
"table `{name}` is defined by both `{}` and `{}`",
existing.owner, self.owner
));
return self;
}
self.tables.push(TableDef::new(name, self.owner));
self.extend(name, columns)
}
pub fn extend(&mut self, table: &'static str, columns: &[ColumnDef]) -> &mut Self {
let owner = self.owner;
let Some(def) = self.tables.iter_mut().find(|t| t.name == table) else {
self.errors
.push(format!("`{owner}` adds columns to unknown table `{table}`"));
return self;
};
for column in columns {
if let Some(clash) = def.column(column.name) {
self.errors.push(format!(
"column `{table}.{}` is defined by both `{}` and `{owner}`",
column.name, clash.owner
));
continue;
}
let mut column = *column;
column.owner = owner;
def.positions.insert(column.name, def.columns.len());
def.columns.push(column);
}
self
}
pub fn model<M: crate::model::Model>(&mut self) -> &mut Self {
self.table(M::TABLE, M::columns())
}
pub(crate) fn finish(self) -> Result<Schema, Vec<String>> {
let mut errors = self.errors;
for table in &self.tables {
for column in &table.columns {
if let Some(r) = column.references {
let target = self.tables.iter().find(|t| t.name == r.table);
if target.and_then(|t| t.column(r.column)).is_none() {
errors.push(format!(
"column `{}.{}` references missing `{}.{}`",
table.name, column.name, r.table, r.column
));
}
}
}
}
if !errors.is_empty() {
return Err(errors);
}
let by_name = self
.tables
.iter()
.enumerate()
.map(|(i, t)| (t.name, i))
.collect();
Ok(Schema {
tables: self.tables.into_iter().map(Arc::new).collect(),
by_name,
})
}
}