1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
use crate::{HasMany, HasManyThrough, HasOne, HasOneInner, OptionHasOne};

/// Methods available for all association types.
pub trait Association<T> {
    /// Store the loaded child on the association.
    fn loaded_child(&mut self, child: T);

    /// The association should have been loaded by now, if not store an error inside the
    /// association (if applicable for the particular association).
    fn assert_loaded_otherwise_failed(&mut self);
}

// --
// -- impl for HasOne
// --
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 for OptionHasOne
// --
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 for HasMany
// --
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) {
        // cannot fail, defaults to an empty vec
    }
}

// --
// -- impl for HasManyThrough
// --
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) {
        // cannot fail, defaults to an empty vec
    }
}

// NOTE: We don't have to implement Association for HasMany<Box<T>> or HasManyThrough<Box<T>>
// because they already have indirection through the inner Vec. So recursive types are supported.