|
@@ -4,9 +4,7 @@ pub struct Unique<T: EntityDatum> {
|
|
|
_ghost: std::marker::PhantomData<T>,
|
|
|
}
|
|
|
|
|
|
-impl<T: EntityDatum> EntityDatum for Unique<T> {
|
|
|
-
|
|
|
-}
|
|
|
+impl<T: EntityDatum> EntityDatum for Unique<T> {}
|
|
|
|
|
|
pub struct IDMap<T: Entity> {
|
|
|
conn: std::cell::OnceCell<std::sync::Arc<sqlite::ConnectionThreadSafe>>,
|
|
@@ -20,22 +18,30 @@ pub struct AssocSet<T: Entity> {
|
|
|
|
|
|
pub struct Index<T: Entity, Key: EntityDatum> {
|
|
|
conn: std::cell::OnceCell<std::sync::Arc<sqlite::ConnectionThreadSafe>>,
|
|
|
- _ghost: std::marker::PhantomData<(T,Key)>,
|
|
|
+ _ghost: std::marker::PhantomData<(T, Key)>,
|
|
|
}
|
|
|
|
|
|
pub trait DatabaseItem {
|
|
|
fn item_key() -> &'static str;
|
|
|
fn dependency_keys() -> &'static [&'static str];
|
|
|
|
|
|
- fn is_index() -> bool { false }
|
|
|
- fn index_over() -> &'static str { unreachable!() }
|
|
|
- fn index_columns() -> &'static [&'static str] { unreachable!() }
|
|
|
+ fn is_index() -> bool {
|
|
|
+ false
|
|
|
+ }
|
|
|
+ fn index_over() -> &'static str {
|
|
|
+ unreachable!()
|
|
|
+ }
|
|
|
+ fn index_columns() -> &'static [&'static str] {
|
|
|
+ unreachable!()
|
|
|
+ }
|
|
|
|
|
|
fn accept_entity_visitor(visitor: &mut impl EntityVisitor);
|
|
|
}
|
|
|
|
|
|
pub trait DatabaseItemVisitor {
|
|
|
- fn visit<DI: DatabaseItem>(&mut self) where Self: Sized;
|
|
|
+ fn visit<DI: DatabaseItem>(&mut self)
|
|
|
+ where
|
|
|
+ Self: Sized;
|
|
|
}
|
|
|
|
|
|
pub trait DatabaseSpec {
|
|
@@ -45,7 +51,10 @@ pub trait DatabaseSpec {
|
|
|
|
|
|
impl<T: Entity> DatabaseSpec for IDMap<T> {
|
|
|
fn give_connection(&mut self, conn: std::sync::Arc<sqlite::ConnectionThreadSafe>) {
|
|
|
- self.conn.set(conn).ok().expect("couldn't set once_cell with sqlite connection!");
|
|
|
+ self.conn
|
|
|
+ .set(conn)
|
|
|
+ .ok()
|
|
|
+ .expect("couldn't set once_cell with sqlite connection!");
|
|
|
}
|
|
|
fn accept_entity_visitor(visitor: &mut impl EntityVisitor) {
|
|
|
visitor.visit::<T>()
|
|
@@ -53,14 +62,15 @@ impl<T: Entity> DatabaseSpec for IDMap<T> {
|
|
|
}
|
|
|
|
|
|
pub trait Database {
|
|
|
- fn accept_item_visitor(visitor: &mut impl DatabaseItemVisitor) where Self: Sized;
|
|
|
+ fn accept_item_visitor(visitor: &mut impl DatabaseItemVisitor)
|
|
|
+ where
|
|
|
+ Self: Sized;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
#[cfg(test)]
|
|
|
mod simple_test {
|
|
|
use super::*;
|
|
|
- use crate::entity::{EntityPart,EntityPartVisitor};
|
|
|
+ use crate::entity::{EntityPart, EntityPartVisitor};
|
|
|
// simple hand-built database example
|
|
|
|
|
|
struct SimpleEntity {
|
|
@@ -74,12 +84,18 @@ mod simple_test {
|
|
|
struct SimpleEntityName;
|
|
|
impl EntityPart for SimpleEntityName {
|
|
|
type Datum = String;
|
|
|
- fn part_name() -> &'static str { "name" }
|
|
|
- fn is_unique() -> bool { true }
|
|
|
+ fn part_name() -> &'static str {
|
|
|
+ "name"
|
|
|
+ }
|
|
|
+ fn is_unique() -> bool {
|
|
|
+ true
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl Entity for SimpleEntity {
|
|
|
- fn entity_name() -> &'static str { "simple_entity" }
|
|
|
+ fn entity_name() -> &'static str {
|
|
|
+ "simple_entity"
|
|
|
+ }
|
|
|
fn accept_part_visitor(visitor: &mut impl EntityPartVisitor) {
|
|
|
visitor.visit::<SimpleEntityName>();
|
|
|
}
|
|
@@ -90,11 +106,18 @@ mod simple_test {
|
|
|
}
|
|
|
|
|
|
impl Database for SimpleDatabase {
|
|
|
- fn accept_item_visitor(visitor: &mut impl DatabaseItemVisitor) where Self: Sized {
|
|
|
+ fn accept_item_visitor(visitor: &mut impl DatabaseItemVisitor)
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
struct SimpleDatabaseStringsItem;
|
|
|
impl DatabaseItem for SimpleDatabaseStringsItem {
|
|
|
- fn item_key() -> &'static str { "strings" }
|
|
|
- fn dependency_keys() -> &'static [&'static str] { &[] }
|
|
|
+ fn item_key() -> &'static str {
|
|
|
+ "strings"
|
|
|
+ }
|
|
|
+ fn dependency_keys() -> &'static [&'static str] {
|
|
|
+ &[]
|
|
|
+ }
|
|
|
|
|
|
fn accept_entity_visitor(visitor: &mut impl EntityVisitor) {
|
|
|
visitor.visit::<SimpleEntity>();
|
|
@@ -107,7 +130,7 @@ mod simple_test {
|
|
|
|
|
|
#[test]
|
|
|
fn part_visitor() {
|
|
|
- struct V {
|
|
|
+ struct V {
|
|
|
v: Vec<std::any::TypeId>,
|
|
|
}
|
|
|
impl EntityPartVisitor for V {
|
|
@@ -118,7 +141,10 @@ mod simple_test {
|
|
|
|
|
|
let mut vis = V { v: vec![] };
|
|
|
SimpleEntity::accept_part_visitor(&mut vis);
|
|
|
- assert_eq!(vis.v.as_slice(), &[std::any::TypeId::of::<SimpleEntityName>()]);
|
|
|
+ assert_eq!(
|
|
|
+ vis.v.as_slice(),
|
|
|
+ &[std::any::TypeId::of::<SimpleEntityName>()]
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -129,12 +155,12 @@ mod derive_test {
|
|
|
use super::IDMap;
|
|
|
#[derive(Entity)]
|
|
|
struct SimpleEntity {
|
|
|
- name: super::Unique<String>
|
|
|
+ name: super::Unique<String>,
|
|
|
}
|
|
|
|
|
|
#[derive(Database)]
|
|
|
struct SimpleDB {
|
|
|
- entities: IDMap<SimpleEntity>
|
|
|
+ entities: IDMap<SimpleEntity>,
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -143,10 +169,8 @@ mod derive_test {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/*
|
|
|
-// pub trait
|
|
|
+// pub trait
|
|
|
|
|
|
pub struct Skill { }
|
|
|
impl Entity for Skill {}
|