pub struct Builder<L, Left>where
L: Tuple,
Left: Expression<L>,{ /* private fields */ }Expand description
Is a builder for building Expression values.
Implementations§
Source§impl<L, Left> Builder<L, Left>where
L: Tuple,
Left: Expression<L>,
impl<L, Left> Builder<L, Left>where
L: Tuple,
Left: Expression<L>,
Sourcepub fn project<T>(
self,
f: impl FnMut(&L) -> T + 'static,
) -> Builder<T, Project<L, T, Left>>where
T: Tuple,
pub fn project<T>(
self,
f: impl FnMut(&L) -> T + 'static,
) -> Builder<T, Project<L, T, Left>>where
T: Tuple,
Builds a Project expression over the receiver’s expression.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let fruit = db.add_relation::<String>("R").unwrap();
db.insert(&fruit, vec!["Apple".to_string(), "BANANA".into(), "cherry".into()].into());
let lower = fruit.builder().project(|t| t.to_lowercase()).build();
assert_eq!(vec!["apple", "banana", "cherry"], db.evaluate(&lower).unwrap().into_tuples());Examples found in repository?
34fn main() -> Result<(), Error> {
35 let mut music = Database::new();
36 let musician = music.add_relation("musician")?;
37 let band = music.add_relation("band")?;
38 let song = music.add_relation("song")?;
39 music.insert(
40 &musician,
41 vec![
42 Musician {
43 name: "John Petrucci".into(),
44 band: Some("Dream Theater".into()),
45 instruments: vec![Guitar],
46 },
47 Musician {
48 name: "Taylor Swift".into(),
49 band: None,
50 instruments: vec![Vocals],
51 },
52 Musician {
53 name: "Conor Mason".into(),
54 band: Some("Nothing But Thieves".into()),
55 instruments: vec![Vocals, Guitar],
56 },
57 Musician {
58 name: "Stevie Wonder".into(),
59 band: None,
60 instruments: vec![Vocals, Piano],
61 },
62 Musician {
63 name: "Jordan Rudess".into(),
64 band: Some("Dream Theater".into()),
65 instruments: vec![Keyboard],
66 },
67 Musician {
68 name: "Alex Turner".into(),
69 band: Some("Arctic Monkeys".into()),
70 instruments: vec![Vocals, Guitar, Piano],
71 },
72 Musician {
73 name: "Billie Eilish".into(),
74 band: None,
75 instruments: vec![Vocals, Piano],
76 },
77 Musician {
78 name: "Lars Ulrich".into(),
79 band: Some("Metallica".into()),
80 instruments: vec![Drums],
81 },
82 ]
83 .into(),
84 )?;
85
86 music.insert(
87 &band,
88 vec![
89 Band {
90 name: "Dream Theater".into(),
91 genre: "Progressive Metal".into(),
92 },
93 Band {
94 name: "Nothing But Thieves".into(),
95 genre: "Alternative Rock".into(),
96 },
97 Band {
98 name: "Metallica".into(),
99 genre: "Heavy Metal".into(),
100 },
101 Band {
102 name: "Arctic Monkeys".into(),
103 genre: "Indie Rock".into(),
104 },
105 ]
106 .into(),
107 )?;
108
109 music.insert(
110 &song,
111 vec![
112 Song {
113 title: "pull me under".into(),
114 artist: Either::Right("Dream Theater".into()),
115 },
116 Song {
117 title: "bad guy".into(),
118 artist: Either::Left("Billie Eilish".into()),
119 },
120 Song {
121 title: "excuse me".into(),
122 artist: Either::Left("Nothing But Thieves".into()),
123 },
124 Song {
125 title: "enter sandman".into(),
126 artist: Either::Right("Metallica".into()),
127 },
128 Song {
129 title: "panic attack".into(),
130 artist: Either::Right("Dream Theater".into()),
131 },
132 Song {
133 title: "shake it off".into(),
134 artist: Either::Left("Taylor Swift".into()),
135 },
136 Song {
137 title: "r u mine".into(),
138 artist: Either::Right("Artcic Monkeys".into()),
139 },
140 Song {
141 title: "as I am".into(),
142 artist: Either::Right("Dream Theater".into()),
143 },
144 ]
145 .into(),
146 )?;
147
148 let guitarist_name = musician
149 .builder()
150 .select(|m| m.instruments.contains(&Guitar))
151 .project(|g| g.name.to_string())
152 .build();
153
154 assert_eq!(
155 vec![
156 "Alex Turner".to_string(),
157 "Conor Mason".into(),
158 "John Petrucci".into(),
159 ],
160 music.evaluate(&guitarist_name)?.into_tuples()
161 );
162
163 let dt_member = musician
164 .builder()
165 .with_key(|m| m.band.clone())
166 .join(band.builder().with_key(|b| Some(b.name.clone())))
167 .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168 .select(|m| m.1 == "Dream Theater")
169 .project(|m| m.0.to_string())
170 .build();
171
172 assert_eq!(
173 vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174 music.evaluate(&dt_member)?.into_tuples()
175 );
176
177 let dt_member_view = music.store_view(dt_member)?;
178 let drummer_view = music.store_view(
179 musician
180 .builder()
181 .select(|m| m.instruments.contains(&Drums))
182 .build(),
183 )?;
184
185 music.insert(
186 &musician,
187 vec![
188 Musician {
189 name: "John Myung".into(),
190 band: Some("Dream Theater".into()),
191 instruments: vec![Guitar],
192 },
193 Musician {
194 name: "Mike Mangini".into(),
195 band: Some("Dream Theater".into()),
196 instruments: vec![Drums],
197 },
198 ]
199 .into(),
200 )?;
201
202 assert_eq!(
203 vec![
204 Musician {
205 name: "Lars Ulrich".into(),
206 band: Some("Metallica".into()),
207 instruments: vec![Drums]
208 },
209 Musician {
210 name: "Mike Mangini".into(),
211 band: Some("Dream Theater".into()),
212 instruments: vec![Drums]
213 }
214 ],
215 music.evaluate(&drummer_view)?.into_tuples()
216 );
217 assert_eq!(
218 vec![
219 "John Myung".to_string(),
220 "John Petrucci".into(),
221 "Jordan Rudess".into(),
222 "Mike Mangini".into()
223 ],
224 music.evaluate(&dt_member_view)?.into_tuples()
225 );
226
227 Ok(())
228}Sourcepub fn select(
self,
f: impl FnMut(&L) -> bool + 'static,
) -> Builder<L, Select<L, Left>>
pub fn select( self, f: impl FnMut(&L) -> bool + 'static, ) -> Builder<L, Select<L, Left>>
Builds a Select expression over the receiver’s expression.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let fruit = db.add_relation::<String>("Fruit").unwrap();
db.insert(&fruit, vec!["Apple".to_string(), "BANANA".into(), "cherry".into()].into());
let select = fruit.builder().select(|t| t.contains('A')).build();
assert_eq!(vec!["Apple", "BANANA"], db.evaluate(&select).unwrap().into_tuples());Examples found in repository?
34fn main() -> Result<(), Error> {
35 let mut music = Database::new();
36 let musician = music.add_relation("musician")?;
37 let band = music.add_relation("band")?;
38 let song = music.add_relation("song")?;
39 music.insert(
40 &musician,
41 vec![
42 Musician {
43 name: "John Petrucci".into(),
44 band: Some("Dream Theater".into()),
45 instruments: vec![Guitar],
46 },
47 Musician {
48 name: "Taylor Swift".into(),
49 band: None,
50 instruments: vec![Vocals],
51 },
52 Musician {
53 name: "Conor Mason".into(),
54 band: Some("Nothing But Thieves".into()),
55 instruments: vec![Vocals, Guitar],
56 },
57 Musician {
58 name: "Stevie Wonder".into(),
59 band: None,
60 instruments: vec![Vocals, Piano],
61 },
62 Musician {
63 name: "Jordan Rudess".into(),
64 band: Some("Dream Theater".into()),
65 instruments: vec![Keyboard],
66 },
67 Musician {
68 name: "Alex Turner".into(),
69 band: Some("Arctic Monkeys".into()),
70 instruments: vec![Vocals, Guitar, Piano],
71 },
72 Musician {
73 name: "Billie Eilish".into(),
74 band: None,
75 instruments: vec![Vocals, Piano],
76 },
77 Musician {
78 name: "Lars Ulrich".into(),
79 band: Some("Metallica".into()),
80 instruments: vec![Drums],
81 },
82 ]
83 .into(),
84 )?;
85
86 music.insert(
87 &band,
88 vec![
89 Band {
90 name: "Dream Theater".into(),
91 genre: "Progressive Metal".into(),
92 },
93 Band {
94 name: "Nothing But Thieves".into(),
95 genre: "Alternative Rock".into(),
96 },
97 Band {
98 name: "Metallica".into(),
99 genre: "Heavy Metal".into(),
100 },
101 Band {
102 name: "Arctic Monkeys".into(),
103 genre: "Indie Rock".into(),
104 },
105 ]
106 .into(),
107 )?;
108
109 music.insert(
110 &song,
111 vec![
112 Song {
113 title: "pull me under".into(),
114 artist: Either::Right("Dream Theater".into()),
115 },
116 Song {
117 title: "bad guy".into(),
118 artist: Either::Left("Billie Eilish".into()),
119 },
120 Song {
121 title: "excuse me".into(),
122 artist: Either::Left("Nothing But Thieves".into()),
123 },
124 Song {
125 title: "enter sandman".into(),
126 artist: Either::Right("Metallica".into()),
127 },
128 Song {
129 title: "panic attack".into(),
130 artist: Either::Right("Dream Theater".into()),
131 },
132 Song {
133 title: "shake it off".into(),
134 artist: Either::Left("Taylor Swift".into()),
135 },
136 Song {
137 title: "r u mine".into(),
138 artist: Either::Right("Artcic Monkeys".into()),
139 },
140 Song {
141 title: "as I am".into(),
142 artist: Either::Right("Dream Theater".into()),
143 },
144 ]
145 .into(),
146 )?;
147
148 let guitarist_name = musician
149 .builder()
150 .select(|m| m.instruments.contains(&Guitar))
151 .project(|g| g.name.to_string())
152 .build();
153
154 assert_eq!(
155 vec![
156 "Alex Turner".to_string(),
157 "Conor Mason".into(),
158 "John Petrucci".into(),
159 ],
160 music.evaluate(&guitarist_name)?.into_tuples()
161 );
162
163 let dt_member = musician
164 .builder()
165 .with_key(|m| m.band.clone())
166 .join(band.builder().with_key(|b| Some(b.name.clone())))
167 .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168 .select(|m| m.1 == "Dream Theater")
169 .project(|m| m.0.to_string())
170 .build();
171
172 assert_eq!(
173 vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174 music.evaluate(&dt_member)?.into_tuples()
175 );
176
177 let dt_member_view = music.store_view(dt_member)?;
178 let drummer_view = music.store_view(
179 musician
180 .builder()
181 .select(|m| m.instruments.contains(&Drums))
182 .build(),
183 )?;
184
185 music.insert(
186 &musician,
187 vec![
188 Musician {
189 name: "John Myung".into(),
190 band: Some("Dream Theater".into()),
191 instruments: vec![Guitar],
192 },
193 Musician {
194 name: "Mike Mangini".into(),
195 band: Some("Dream Theater".into()),
196 instruments: vec![Drums],
197 },
198 ]
199 .into(),
200 )?;
201
202 assert_eq!(
203 vec![
204 Musician {
205 name: "Lars Ulrich".into(),
206 band: Some("Metallica".into()),
207 instruments: vec![Drums]
208 },
209 Musician {
210 name: "Mike Mangini".into(),
211 band: Some("Dream Theater".into()),
212 instruments: vec![Drums]
213 }
214 ],
215 music.evaluate(&drummer_view)?.into_tuples()
216 );
217 assert_eq!(
218 vec![
219 "John Myung".to_string(),
220 "John Petrucci".into(),
221 "Jordan Rudess".into(),
222 "Mike Mangini".into()
223 ],
224 music.evaluate(&dt_member_view)?.into_tuples()
225 );
226
227 Ok(())
228}Sourcepub fn intersect<Right, I>(
self,
other: I,
) -> Builder<L, Intersect<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
pub fn intersect<Right, I>(
self,
other: I,
) -> Builder<L, Intersect<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
Builds an Intersect expression with the receiver’s expression on left and other on right.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let r = db.add_relation::<i32>("R").unwrap();
let s = db.add_relation::<i32>("S").unwrap();
db.insert(&r, vec![0, 1, 2].into());
db.insert(&s, vec![2, 4].into());
let intersect = r.builder().intersect(s).build();
assert_eq!(vec![2], db.evaluate(&intersect).unwrap().into_tuples());Sourcepub fn difference<Right, I>(
self,
other: I,
) -> Builder<L, Difference<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
pub fn difference<Right, I>(
self,
other: I,
) -> Builder<L, Difference<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
Builds a Difference expression with the receiver’s expression on left and other on right.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let r = db.add_relation::<i32>("R").unwrap();
let s = db.add_relation::<i32>("S").unwrap();
db.insert(&r, vec![0, 1, 2].into());
db.insert(&s, vec![2, 4].into());
let r_s = r.builder().difference(&s).build();
let s_r = s.builder().difference(r).build();
assert_eq!(vec![0, 1], db.evaluate(&r_s).unwrap().into_tuples());
assert_eq!(vec![4], db.evaluate(&s_r).unwrap().into_tuples());Sourcepub fn union<Right, I>(self, other: I) -> Builder<L, Union<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
pub fn union<Right, I>(self, other: I) -> Builder<L, Union<L, Left, Right>>where
Right: Expression<L>,
I: IntoExpression<L, Right>,
Builds a Union expression with the receiver’s expression on left and other on right.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let r = db.add_relation::<i32>("R").unwrap();
let s = db.add_relation::<i32>("S").unwrap();
db.insert(&r, vec![0, 1, 2].into());
db.insert(&s, vec![2, 4].into());
let union = r.builder().union(s).build();
assert_eq!(vec![0, 1, 2, 4], db.evaluate(&union).unwrap().into_tuples());Sourcepub fn product<R, Right, I>(self, other: I) -> ProductBuilder<L, R, Left, Right>
pub fn product<R, Right, I>(self, other: I) -> ProductBuilder<L, R, Left, Right>
Combines the receiver’s expression with other in a temporary builder, which then can be turned into
a Product expression using a combining closure provided by method on.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let r = db.add_relation::<i32>("R").unwrap();
let s = db.add_relation::<i32>("S").unwrap();
db.insert(&r, vec![0, 1, 2].into());
db.insert(&s, vec![2, 4].into());
let prod = r.builder().product(s).on(|l, r| l*r).build();
assert_eq!(vec![0, 2, 4, 8], db.evaluate(&prod).unwrap().into_tuples());Sourcepub fn with_key<K>(
self,
f: impl FnMut(&L) -> K + 'static,
) -> WithKeyBuilder<K, L, Left>where
K: Tuple,
pub fn with_key<K>(
self,
f: impl FnMut(&L) -> K + 'static,
) -> WithKeyBuilder<K, L, Left>where
K: Tuple,
Combines the receiver’s expression with closure f as the join key. This value can then be joined with
another expression and it’s key to create a temporary join builder. Finally, the temporary builder
can be turned into a Join expression using a combining closure provided by method on.
Example:
use codd::{Database, Expression};
let mut db = Database::new();
let fruit = db.add_relation::<(i32, String)>("R").unwrap();
let numbers = db.add_relation::<i32>("S").unwrap();
db.insert(&fruit, vec![
(0, "Apple".to_string()),
(1, "Banana".into()),
(2, "Cherry".into())
].into());
db.insert(&numbers, vec![0, 2].into());
let join = fruit
.builder()
.with_key(|t| t.0) // first element of tuples in `r` is the key for join
.join(numbers.builder().with_key(|&t| t))
.on(|k, l, r| format!("{}{}", l.1, k + r))
// combine the key `k`, left tuple `l` and right tuple `r`:
.build();
assert_eq!(vec!["Apple0", "Cherry4"], db.evaluate(&join).unwrap().into_tuples());Examples found in repository?
34fn main() -> Result<(), Error> {
35 let mut music = Database::new();
36 let musician = music.add_relation("musician")?;
37 let band = music.add_relation("band")?;
38 let song = music.add_relation("song")?;
39 music.insert(
40 &musician,
41 vec![
42 Musician {
43 name: "John Petrucci".into(),
44 band: Some("Dream Theater".into()),
45 instruments: vec![Guitar],
46 },
47 Musician {
48 name: "Taylor Swift".into(),
49 band: None,
50 instruments: vec![Vocals],
51 },
52 Musician {
53 name: "Conor Mason".into(),
54 band: Some("Nothing But Thieves".into()),
55 instruments: vec![Vocals, Guitar],
56 },
57 Musician {
58 name: "Stevie Wonder".into(),
59 band: None,
60 instruments: vec![Vocals, Piano],
61 },
62 Musician {
63 name: "Jordan Rudess".into(),
64 band: Some("Dream Theater".into()),
65 instruments: vec![Keyboard],
66 },
67 Musician {
68 name: "Alex Turner".into(),
69 band: Some("Arctic Monkeys".into()),
70 instruments: vec![Vocals, Guitar, Piano],
71 },
72 Musician {
73 name: "Billie Eilish".into(),
74 band: None,
75 instruments: vec![Vocals, Piano],
76 },
77 Musician {
78 name: "Lars Ulrich".into(),
79 band: Some("Metallica".into()),
80 instruments: vec![Drums],
81 },
82 ]
83 .into(),
84 )?;
85
86 music.insert(
87 &band,
88 vec![
89 Band {
90 name: "Dream Theater".into(),
91 genre: "Progressive Metal".into(),
92 },
93 Band {
94 name: "Nothing But Thieves".into(),
95 genre: "Alternative Rock".into(),
96 },
97 Band {
98 name: "Metallica".into(),
99 genre: "Heavy Metal".into(),
100 },
101 Band {
102 name: "Arctic Monkeys".into(),
103 genre: "Indie Rock".into(),
104 },
105 ]
106 .into(),
107 )?;
108
109 music.insert(
110 &song,
111 vec![
112 Song {
113 title: "pull me under".into(),
114 artist: Either::Right("Dream Theater".into()),
115 },
116 Song {
117 title: "bad guy".into(),
118 artist: Either::Left("Billie Eilish".into()),
119 },
120 Song {
121 title: "excuse me".into(),
122 artist: Either::Left("Nothing But Thieves".into()),
123 },
124 Song {
125 title: "enter sandman".into(),
126 artist: Either::Right("Metallica".into()),
127 },
128 Song {
129 title: "panic attack".into(),
130 artist: Either::Right("Dream Theater".into()),
131 },
132 Song {
133 title: "shake it off".into(),
134 artist: Either::Left("Taylor Swift".into()),
135 },
136 Song {
137 title: "r u mine".into(),
138 artist: Either::Right("Artcic Monkeys".into()),
139 },
140 Song {
141 title: "as I am".into(),
142 artist: Either::Right("Dream Theater".into()),
143 },
144 ]
145 .into(),
146 )?;
147
148 let guitarist_name = musician
149 .builder()
150 .select(|m| m.instruments.contains(&Guitar))
151 .project(|g| g.name.to_string())
152 .build();
153
154 assert_eq!(
155 vec![
156 "Alex Turner".to_string(),
157 "Conor Mason".into(),
158 "John Petrucci".into(),
159 ],
160 music.evaluate(&guitarist_name)?.into_tuples()
161 );
162
163 let dt_member = musician
164 .builder()
165 .with_key(|m| m.band.clone())
166 .join(band.builder().with_key(|b| Some(b.name.clone())))
167 .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168 .select(|m| m.1 == "Dream Theater")
169 .project(|m| m.0.to_string())
170 .build();
171
172 assert_eq!(
173 vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174 music.evaluate(&dt_member)?.into_tuples()
175 );
176
177 let dt_member_view = music.store_view(dt_member)?;
178 let drummer_view = music.store_view(
179 musician
180 .builder()
181 .select(|m| m.instruments.contains(&Drums))
182 .build(),
183 )?;
184
185 music.insert(
186 &musician,
187 vec![
188 Musician {
189 name: "John Myung".into(),
190 band: Some("Dream Theater".into()),
191 instruments: vec![Guitar],
192 },
193 Musician {
194 name: "Mike Mangini".into(),
195 band: Some("Dream Theater".into()),
196 instruments: vec![Drums],
197 },
198 ]
199 .into(),
200 )?;
201
202 assert_eq!(
203 vec![
204 Musician {
205 name: "Lars Ulrich".into(),
206 band: Some("Metallica".into()),
207 instruments: vec![Drums]
208 },
209 Musician {
210 name: "Mike Mangini".into(),
211 band: Some("Dream Theater".into()),
212 instruments: vec![Drums]
213 }
214 ],
215 music.evaluate(&drummer_view)?.into_tuples()
216 );
217 assert_eq!(
218 vec![
219 "John Myung".to_string(),
220 "John Petrucci".into(),
221 "Jordan Rudess".into(),
222 "Mike Mangini".into()
223 ],
224 music.evaluate(&dt_member_view)?.into_tuples()
225 );
226
227 Ok(())
228}Sourcepub fn build(self) -> Left
pub fn build(self) -> Left
Builds an expression from the receiver.
Examples found in repository?
34fn main() -> Result<(), Error> {
35 let mut music = Database::new();
36 let musician = music.add_relation("musician")?;
37 let band = music.add_relation("band")?;
38 let song = music.add_relation("song")?;
39 music.insert(
40 &musician,
41 vec![
42 Musician {
43 name: "John Petrucci".into(),
44 band: Some("Dream Theater".into()),
45 instruments: vec![Guitar],
46 },
47 Musician {
48 name: "Taylor Swift".into(),
49 band: None,
50 instruments: vec![Vocals],
51 },
52 Musician {
53 name: "Conor Mason".into(),
54 band: Some("Nothing But Thieves".into()),
55 instruments: vec![Vocals, Guitar],
56 },
57 Musician {
58 name: "Stevie Wonder".into(),
59 band: None,
60 instruments: vec![Vocals, Piano],
61 },
62 Musician {
63 name: "Jordan Rudess".into(),
64 band: Some("Dream Theater".into()),
65 instruments: vec![Keyboard],
66 },
67 Musician {
68 name: "Alex Turner".into(),
69 band: Some("Arctic Monkeys".into()),
70 instruments: vec![Vocals, Guitar, Piano],
71 },
72 Musician {
73 name: "Billie Eilish".into(),
74 band: None,
75 instruments: vec![Vocals, Piano],
76 },
77 Musician {
78 name: "Lars Ulrich".into(),
79 band: Some("Metallica".into()),
80 instruments: vec![Drums],
81 },
82 ]
83 .into(),
84 )?;
85
86 music.insert(
87 &band,
88 vec![
89 Band {
90 name: "Dream Theater".into(),
91 genre: "Progressive Metal".into(),
92 },
93 Band {
94 name: "Nothing But Thieves".into(),
95 genre: "Alternative Rock".into(),
96 },
97 Band {
98 name: "Metallica".into(),
99 genre: "Heavy Metal".into(),
100 },
101 Band {
102 name: "Arctic Monkeys".into(),
103 genre: "Indie Rock".into(),
104 },
105 ]
106 .into(),
107 )?;
108
109 music.insert(
110 &song,
111 vec![
112 Song {
113 title: "pull me under".into(),
114 artist: Either::Right("Dream Theater".into()),
115 },
116 Song {
117 title: "bad guy".into(),
118 artist: Either::Left("Billie Eilish".into()),
119 },
120 Song {
121 title: "excuse me".into(),
122 artist: Either::Left("Nothing But Thieves".into()),
123 },
124 Song {
125 title: "enter sandman".into(),
126 artist: Either::Right("Metallica".into()),
127 },
128 Song {
129 title: "panic attack".into(),
130 artist: Either::Right("Dream Theater".into()),
131 },
132 Song {
133 title: "shake it off".into(),
134 artist: Either::Left("Taylor Swift".into()),
135 },
136 Song {
137 title: "r u mine".into(),
138 artist: Either::Right("Artcic Monkeys".into()),
139 },
140 Song {
141 title: "as I am".into(),
142 artist: Either::Right("Dream Theater".into()),
143 },
144 ]
145 .into(),
146 )?;
147
148 let guitarist_name = musician
149 .builder()
150 .select(|m| m.instruments.contains(&Guitar))
151 .project(|g| g.name.to_string())
152 .build();
153
154 assert_eq!(
155 vec![
156 "Alex Turner".to_string(),
157 "Conor Mason".into(),
158 "John Petrucci".into(),
159 ],
160 music.evaluate(&guitarist_name)?.into_tuples()
161 );
162
163 let dt_member = musician
164 .builder()
165 .with_key(|m| m.band.clone())
166 .join(band.builder().with_key(|b| Some(b.name.clone())))
167 .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168 .select(|m| m.1 == "Dream Theater")
169 .project(|m| m.0.to_string())
170 .build();
171
172 assert_eq!(
173 vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174 music.evaluate(&dt_member)?.into_tuples()
175 );
176
177 let dt_member_view = music.store_view(dt_member)?;
178 let drummer_view = music.store_view(
179 musician
180 .builder()
181 .select(|m| m.instruments.contains(&Drums))
182 .build(),
183 )?;
184
185 music.insert(
186 &musician,
187 vec![
188 Musician {
189 name: "John Myung".into(),
190 band: Some("Dream Theater".into()),
191 instruments: vec![Guitar],
192 },
193 Musician {
194 name: "Mike Mangini".into(),
195 band: Some("Dream Theater".into()),
196 instruments: vec![Drums],
197 },
198 ]
199 .into(),
200 )?;
201
202 assert_eq!(
203 vec![
204 Musician {
205 name: "Lars Ulrich".into(),
206 band: Some("Metallica".into()),
207 instruments: vec![Drums]
208 },
209 Musician {
210 name: "Mike Mangini".into(),
211 band: Some("Dream Theater".into()),
212 instruments: vec![Drums]
213 }
214 ],
215 music.evaluate(&drummer_view)?.into_tuples()
216 );
217 assert_eq!(
218 vec![
219 "John Myung".to_string(),
220 "John Petrucci".into(),
221 "Jordan Rudess".into(),
222 "Mike Mangini".into()
223 ],
224 music.evaluate(&dt_member_view)?.into_tuples()
225 );
226
227 Ok(())
228}