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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use super::*;

/// ## Many-to-Many relationships option builder
#[derive(Debug, Clone, Getters, PartialEq)]
#[getset(get = "pub")]
pub struct CibouletteRelationshipManyToManyOptionBuilder {
    /// The bucket resource
    bucket_resource: CibouletteResourceType,
    /// The keys associated type and their matching field in bucket
    keys: [(CibouletteResourceType, ArcStr); 2],
}

/// ## One-to-Many/Many-to-one relationships option builder
#[derive(Debug, Clone, Getters, PartialEq)]
#[getset(get = "pub")]
pub struct CibouletteRelationshipOneToManyOptionBuilder {
    /// The "one" resource
    one_resource: CibouletteResourceType,
    /// The field in the "one" resource that points to the "many" resource
    one_resource_key: ArcStr,
    /// The "many" resource
    many_resource: CibouletteResourceType,
    /// The field in the "many" resource that points to the "one" resource
    many_resource_key: ArcStr,
    /// True if that relationships is optional
    optional: bool,
    /// True if this relationships is part of Many-to-Many relationships
    part_of_many_to_many: Option<petgraph::graph::EdgeIndex<u16>>,
}

impl CibouletteRelationshipManyToManyOptionBuilder {
    pub fn new(
        bucket_resource: CibouletteResourceType,
        keys: [(CibouletteResourceType, ArcStr); 2],
    ) -> Self {
        CibouletteRelationshipManyToManyOptionBuilder {
            bucket_resource,
            keys,
        }
    }

    /// Get the field for the resource in a Many-to-Many relationships
    pub fn keys_for_type(&self, type_: &CibouletteResourceType) -> Result<ArcStr, CibouletteError> {
        self.keys
            .iter()
            .find(|(k, _)| k == type_)
            .map(|x| x.1.clone())
            .ok_or_else(|| {
                CibouletteError::UnknownRelationship(
                    self.bucket_resource().name().to_string(),
                    type_.name().to_string(),
                )
            })
    }

    /// Build into [Arc<CibouletteRelationshipManyToManyOption>](CibouletteRelationshipManyToManyOption)
    pub(crate) fn build(
        &self,
        store_builder: &CibouletteStoreBuilder,
        graph: &petgraph::graph::Graph<
            Arc<CibouletteResourceType>,
            CibouletteRelationshipOption,
            petgraph::Directed,
            u16,
        >,
    ) -> Result<Arc<CibouletteRelationshipManyToManyOption>, CibouletteError> {
        let bucket_table = store_builder
            .get_type_index(self.bucket_resource().name())
            .ok_or_else(|| {
                CibouletteError::TypeNotInGraph(self.bucket_resource().name().to_string())
            })?;
        let table1 = store_builder
            .get_type_index(self.keys()[0].0.name())
            .ok_or_else(|| CibouletteError::TypeNotInGraph(self.keys()[0].0.name().to_string()))?;
        let table2 = store_builder
            .get_type_index(self.keys()[1].0.name())
            .ok_or_else(|| CibouletteError::TypeNotInGraph(self.keys()[1].0.name().to_string()))?;

        Ok(Arc::from(CibouletteRelationshipManyToManyOption {
            bucket_resource: graph
                .node_weight(*bucket_table)
                .ok_or_else(|| {
                    CibouletteError::TypeNotInGraph(self.bucket_resource().name().to_string())
                })?
                .clone(),
            keys: [
                (
                    graph
                        .node_weight(*table1)
                        .ok_or_else(|| {
                            CibouletteError::TypeNotInGraph(self.keys()[0].0.name().to_string())
                        })?
                        .clone(),
                    self.keys[0].1.clone(),
                ),
                (
                    graph
                        .node_weight(*table2)
                        .ok_or_else(|| {
                            CibouletteError::TypeNotInGraph(self.keys()[1].0.name().to_string())
                        })?
                        .clone(),
                    self.keys[1].1.clone(),
                ),
            ],
        }))
    }
}

impl CibouletteRelationshipOneToManyOptionBuilder {
    pub fn new(
        one_resource: CibouletteResourceType,
        one_resource_key: ArcStr,
        many_resource: CibouletteResourceType,
        many_resource_key: ArcStr,
        optional: bool,
    ) -> Self {
        CibouletteRelationshipOneToManyOptionBuilder {
            one_resource,
            many_resource,
            one_resource_key,
            many_resource_key,
            part_of_many_to_many: None,
            optional,
        }
    }

    /// Build a new O2M/M2O relationships in the process of creating a new M2M relationships
    pub(crate) fn new_from_many_to_many(
        one_resource: CibouletteResourceType,
        one_resource_key: ArcStr,
        many_resource: CibouletteResourceType,
        many_resource_key: ArcStr,
        optional: bool,
        part_of_many_to_many: petgraph::graph::EdgeIndex<u16>,
    ) -> Self {
        CibouletteRelationshipOneToManyOptionBuilder {
            one_resource,
            one_resource_key,
            many_resource,
            many_resource_key,
            part_of_many_to_many: Some(part_of_many_to_many),
            optional,
        }
    }

    /// Build into [Arc<CibouletteRelationshipOneToManyOption>](CibouletteRelationshipOneToManyOption)
    pub(crate) fn build(
        &self,
        store_builder: &CibouletteStoreBuilder,
        graph: &petgraph::graph::Graph<
            Arc<CibouletteResourceType>,
            CibouletteRelationshipOption,
            petgraph::Directed,
            u16,
        >,
    ) -> Result<Arc<CibouletteRelationshipOneToManyOption>, CibouletteError> {
        let one_resource = store_builder
            .get_type_index(self.one_resource().name())
            .ok_or_else(|| {
                CibouletteError::TypeNotInGraph(self.one_resource().name().to_string())
            })?;
        let many_resource = store_builder
            .get_type_index(self.many_resource().name())
            .ok_or_else(|| {
                CibouletteError::TypeNotInGraph(self.many_resource().name().to_string())
            })?;

        Ok(Arc::from(CibouletteRelationshipOneToManyOption {
            one_resource: graph
                .node_weight(*one_resource)
                .ok_or_else(|| {
                    CibouletteError::TypeNotInGraph(self.one_resource().name().to_string())
                })?
                .clone(),
            one_resource_key: self.one_resource_key.clone(),
            many_resource: graph
                .node_weight(*many_resource)
                .ok_or_else(|| {
                    CibouletteError::TypeNotInGraph(self.many_resource().name().to_string())
                })?
                .clone(),
            many_resource_key: ArcStr::from(self.many_resource_key()),
            optional: self.optional,
            part_of_many_to_many: self.part_of_many_to_many,
        }))
    }
}

/// ## Relationship options builder
#[derive(Debug, Clone)]
pub enum CibouletteRelationshipOptionBuilder {
    /// One to many relationship, without the intermediate node
    OneToMany(CibouletteRelationshipOneToManyOptionBuilder),
    /// One to many relationship, without the intermediate node
    ManyToOne(CibouletteRelationshipOneToManyOptionBuilder),
    /// One to many relationship
    ManyToMany(CibouletteRelationshipManyToManyOptionBuilder),
}

impl CibouletteRelationshipOptionBuilder {
    /// Build into [CibouletteRelationshipOption](CibouletteRelationshipOption)
    pub(crate) fn build(
        &self,
        store_builder: &CibouletteStoreBuilder,
        graph: &petgraph::graph::Graph<
            Arc<CibouletteResourceType>,
            CibouletteRelationshipOption,
            petgraph::Directed,
            u16,
        >,
    ) -> Result<CibouletteRelationshipOption, CibouletteError> {
        match self {
            CibouletteRelationshipOptionBuilder::OneToMany(x) => Ok(
                CibouletteRelationshipOption::OneToMany(x.build(store_builder, graph)?),
            ),
            CibouletteRelationshipOptionBuilder::ManyToOne(x) => Ok(
                CibouletteRelationshipOption::ManyToOne(x.build(store_builder, graph)?),
            ),
            CibouletteRelationshipOptionBuilder::ManyToMany(x) => Ok(
                CibouletteRelationshipOption::ManyToMany(x.build(store_builder, graph)?),
            ),
        }
    }
}