-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic.rs
228 lines (212 loc) · 6.19 KB
/
music.rs
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
226
227
228
use codd::{Database, Error, Expression};
use either::Either;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
enum Instrument {
Guitar,
Piano,
Keyboard,
Drums,
Vocals,
}
use Instrument::*;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Musician {
name: String,
band: Option<String>,
instruments: Vec<Instrument>,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Band {
name: String,
genre: String,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Song {
title: String,
artist: Either<String /* musician */, String /* band */>,
}
fn main() -> Result<(), Error> {
let mut music = Database::new();
let musician = music.add_relation("musician")?;
let band = music.add_relation("band")?;
let song = music.add_relation("song")?;
music.insert(
&musician,
vec![
Musician {
name: "John Petrucci".into(),
band: Some("Dream Theater".into()),
instruments: vec![Guitar],
},
Musician {
name: "Taylor Swift".into(),
band: None,
instruments: vec![Vocals],
},
Musician {
name: "Conor Mason".into(),
band: Some("Nothing But Thieves".into()),
instruments: vec![Vocals, Guitar],
},
Musician {
name: "Stevie Wonder".into(),
band: None,
instruments: vec![Vocals, Piano],
},
Musician {
name: "Jordan Rudess".into(),
band: Some("Dream Theater".into()),
instruments: vec![Keyboard],
},
Musician {
name: "Alex Turner".into(),
band: Some("Arctic Monkeys".into()),
instruments: vec![Vocals, Guitar, Piano],
},
Musician {
name: "Billie Eilish".into(),
band: None,
instruments: vec![Vocals, Piano],
},
Musician {
name: "Lars Ulrich".into(),
band: Some("Metallica".into()),
instruments: vec![Drums],
},
]
.into(),
)?;
music.insert(
&band,
vec![
Band {
name: "Dream Theater".into(),
genre: "Progressive Metal".into(),
},
Band {
name: "Nothing But Thieves".into(),
genre: "Alternative Rock".into(),
},
Band {
name: "Metallica".into(),
genre: "Heavy Metal".into(),
},
Band {
name: "Arctic Monkeys".into(),
genre: "Indie Rock".into(),
},
]
.into(),
)?;
music.insert(
&song,
vec![
Song {
title: "pull me under".into(),
artist: Either::Right("Dream Theater".into()),
},
Song {
title: "bad guy".into(),
artist: Either::Left("Billie Eilish".into()),
},
Song {
title: "excuse me".into(),
artist: Either::Left("Nothing But Thieves".into()),
},
Song {
title: "enter sandman".into(),
artist: Either::Right("Metallica".into()),
},
Song {
title: "panic attack".into(),
artist: Either::Right("Dream Theater".into()),
},
Song {
title: "shake it off".into(),
artist: Either::Left("Taylor Swift".into()),
},
Song {
title: "r u mine".into(),
artist: Either::Right("Artcic Monkeys".into()),
},
Song {
title: "as I am".into(),
artist: Either::Right("Dream Theater".into()),
},
]
.into(),
)?;
let guitarist_name = musician
.builder()
.select(|m| m.instruments.contains(&Guitar))
.project(|g| g.name.to_string())
.build();
assert_eq!(
vec![
"Alex Turner".to_string(),
"Conor Mason".into(),
"John Petrucci".into(),
],
music.evaluate(&guitarist_name)?.into_tuples()
);
let dt_member = musician
.builder()
.with_key(|m| m.band.clone())
.join(band.builder().with_key(|b| Some(b.name.clone())))
.on(|_, m, b| (m.name.to_string(), b.name.to_string()))
.select(|m| m.1 == "Dream Theater")
.project(|m| m.0.to_string())
.build();
assert_eq!(
vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
music.evaluate(&dt_member)?.into_tuples()
);
let dt_member_view = music.store_view(dt_member)?;
let drummer_view = music.store_view(
musician
.builder()
.select(|m| m.instruments.contains(&Drums))
.build(),
)?;
music.insert(
&musician,
vec![
Musician {
name: "John Myung".into(),
band: Some("Dream Theater".into()),
instruments: vec![Guitar],
},
Musician {
name: "Mike Mangini".into(),
band: Some("Dream Theater".into()),
instruments: vec![Drums],
},
]
.into(),
)?;
assert_eq!(
vec![
Musician {
name: "Lars Ulrich".into(),
band: Some("Metallica".into()),
instruments: vec![Drums]
},
Musician {
name: "Mike Mangini".into(),
band: Some("Dream Theater".into()),
instruments: vec![Drums]
}
],
music.evaluate(&drummer_view)?.into_tuples()
);
assert_eq!(
vec![
"John Myung".to_string(),
"John Petrucci".into(),
"Jordan Rudess".into(),
"Mike Mangini".into()
],
music.evaluate(&dt_member_view)?.into_tuples()
);
Ok(())
}