use crate::{HasMany, HasManyThrough, HasOne, HasOneInner, OptionHasOne};
pub trait Association<T> {
fn loaded_child(&mut self, child: T);
fn assert_loaded_otherwise_failed(&mut self);
}
impl<T> Association<T> for HasOne<T> {
fn loaded_child(&mut self, child: T) {
has_one_loaded_child(self, child)
}
fn assert_loaded_otherwise_failed(&mut self) {
has_one_assert_loaded_otherwise_failed(self)
}
}
impl<T> Association<T> for HasOne<Box<T>> {
fn loaded_child(&mut self, child: T) {
has_one_loaded_child(self, Box::new(child))
}
fn assert_loaded_otherwise_failed(&mut self) {
has_one_assert_loaded_otherwise_failed(self)
}
}
fn has_one_loaded_child<T>(association: &mut HasOne<T>, child: T) {
std::mem::replace(&mut association.0, HasOneInner::Loaded(child));
}
fn has_one_assert_loaded_otherwise_failed<T>(association: &mut HasOne<T>) {
association.0.assert_loaded_otherwise_failed()
}
impl<T> Association<T> for OptionHasOne<T> {
fn loaded_child(&mut self, child: T) {
option_has_one_loaded_child(self, Some(child));
}
fn assert_loaded_otherwise_failed(&mut self) {
option_has_one_assert_loaded_otherwise_failed(self)
}
}
impl<T> Association<T> for OptionHasOne<Box<T>> {
fn loaded_child(&mut self, child: T) {
option_has_one_loaded_child(self, Some(Box::new(child)));
}
fn assert_loaded_otherwise_failed(&mut self) {
option_has_one_assert_loaded_otherwise_failed(self)
}
}
fn option_has_one_loaded_child<T>(association: &mut OptionHasOne<T>, child: Option<T>) {
std::mem::replace(&mut association.0, child);
}
fn option_has_one_assert_loaded_otherwise_failed<T>(association: &mut OptionHasOne<T>) {
match association.0 {
Some(_) => {}
None => {
std::mem::replace(&mut association.0, None);
}
}
}
impl<T> Association<T> for HasMany<T> {
fn loaded_child(&mut self, child: T) {
self.0.push(child);
}
fn assert_loaded_otherwise_failed(&mut self) {
}
}
impl<T> Association<T> for HasManyThrough<T> {
fn loaded_child(&mut self, child: T) {
self.0.push(child);
}
fn assert_loaded_otherwise_failed(&mut self) {
}
}