juniper_eager_loading/
association.rs1use crate::{HasMany, HasManyThrough, HasOne, HasOneInner, OptionHasOne};
2
3pub trait Association<T> {
5 fn loaded_child(&mut self, child: T);
7
8 fn assert_loaded_otherwise_failed(&mut self);
11}
12
13impl<T> Association<T> for HasOne<T> {
17 fn loaded_child(&mut self, child: T) {
18 has_one_loaded_child(self, child)
19 }
20
21 fn assert_loaded_otherwise_failed(&mut self) {
22 has_one_assert_loaded_otherwise_failed(self)
23 }
24}
25
26impl<T> Association<T> for HasOne<Box<T>> {
27 fn loaded_child(&mut self, child: T) {
28 has_one_loaded_child(self, Box::new(child))
29 }
30
31 fn assert_loaded_otherwise_failed(&mut self) {
32 has_one_assert_loaded_otherwise_failed(self)
33 }
34}
35
36fn has_one_loaded_child<T>(association: &mut HasOne<T>, child: T) {
37 std::mem::replace(&mut association.0, HasOneInner::Loaded(child));
38}
39
40fn has_one_assert_loaded_otherwise_failed<T>(association: &mut HasOne<T>) {
41 association.0.assert_loaded_otherwise_failed()
42}
43
44impl<T> Association<T> for OptionHasOne<T> {
48 fn loaded_child(&mut self, child: T) {
49 option_has_one_loaded_child(self, Some(child));
50 }
51
52 fn assert_loaded_otherwise_failed(&mut self) {
53 option_has_one_assert_loaded_otherwise_failed(self)
54 }
55}
56
57impl<T> Association<T> for OptionHasOne<Box<T>> {
58 fn loaded_child(&mut self, child: T) {
59 option_has_one_loaded_child(self, Some(Box::new(child)));
60 }
61
62 fn assert_loaded_otherwise_failed(&mut self) {
63 option_has_one_assert_loaded_otherwise_failed(self)
64 }
65}
66
67fn option_has_one_loaded_child<T>(association: &mut OptionHasOne<T>, child: Option<T>) {
68 std::mem::replace(&mut association.0, child);
69}
70
71fn option_has_one_assert_loaded_otherwise_failed<T>(association: &mut OptionHasOne<T>) {
72 match association.0 {
73 Some(_) => {}
74 None => {
75 std::mem::replace(&mut association.0, None);
76 }
77 }
78}
79
80impl<T> Association<T> for HasMany<T> {
84 fn loaded_child(&mut self, child: T) {
85 self.0.push(child);
86 }
87
88 fn assert_loaded_otherwise_failed(&mut self) {
89 }
91}
92
93impl<T> Association<T> for HasManyThrough<T> {
97 fn loaded_child(&mut self, child: T) {
98 self.0.push(child);
99 }
100
101 fn assert_loaded_otherwise_failed(&mut self) {
102 }
104}
105
106