More repo code
This commit is contained in:
1 parent
807f398d81
commit
d99fd337da
4 files changed
+359
-63
No files matched your search
+17
-40
@@ -20,6 +20,17 @@ pub struct WordData {
|
||||
pub additional: HashMap<String, String>,
|
||||
pub group_id: Id,
|
||||
}
|
||||
|
||||
impl EntityData for WordData {
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WordGroup {
|
||||
pub id: Id,
|
||||
@@ -168,49 +179,15 @@ impl CardStatistics {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Entity<E, R: Repository<E>> {
|
||||
pub(crate) repo: R,
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct EntityHandle<E> {
|
||||
pub(crate) _phantom: PhantomData<E>,
|
||||
pub(crate) inner: EntityInner,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct EntityInner {
|
||||
pub(crate) id: Id,
|
||||
pub(crate) real: bool,
|
||||
pub(crate) index: u32,
|
||||
}
|
||||
|
||||
impl<E, R: Repository<E>> Entity<E, R> {
|
||||
pub fn load(&self) -> EntityAccess<E, R> {
|
||||
let clone = *self.clone();
|
||||
let repo = &self.repo;
|
||||
let value = repo.get(self.inner.id).unwrap();
|
||||
EntityAccess { value, entity: clone, }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EntityAccess<E, R: Repository<E>> {
|
||||
entity: Entity<E, R>,
|
||||
value: E,
|
||||
}
|
||||
|
||||
impl<E, R: Repository<E>> Deref for EntityAccess<E, R> {
|
||||
type Target = E;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, R: Repository<E>> DerefMut for EntityAccess<E, R> {
|
||||
fn deref_mut(&mut self) -> &mut E {
|
||||
&mut self.value
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Repository<T> {
|
||||
fn get(&self, id: Id) -> Option<T>;
|
||||
fn create(&mut self, new: T) -> Entity<T, impl Repository<T>>;
|
||||
fn save(&mut self, entity: EntityInner) -> MaybeError;
|
||||
pub trait EntityData{
|
||||
fn id(&self) -> Id;
|
||||
fn set_id(&mut self, id: Id);
|
||||
}
|
||||
Reference in new issue
Block a user