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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::collections::HashMap;
use std::str::FromStr;
use std::num::ParseFloatError;
mod collection;
pub use collection::Collection;
pub use collection::Bin;
pub use collection::Sinv;
pub use collection::beta;
pub use collection::gamma;
pub use collection::boost;
pub use collection::FourVec;
pub use collection::FourMat;
pub use collection::ThreeMat;
pub use collection::ThreeVec;
pub use collection::{radians_between, degrees_between};
pub use collection::consts;
pub use collection::Serializable;
pub struct Branch {
subtype: String,
branch: Box<dyn Serializable>,
}
pub struct Tree {
metadata: HashMap<&'static str,&'static str>,
branches: HashMap<&'static str,Branch>,
}
impl Branch {
pub fn new(subtype: String, branch: Box<dyn Serializable>) -> Branch{
Branch {
subtype,
branch,
}
}
pub fn extract_str(&self) -> Collection<String> {
if self.subtype != "String" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<String> = Collection::empty();
for ff in self.branch.to_json().trim_matches(|p| p == '[' || p == ']' ).split(','){
out.push(ff.trim_matches(|p| p == '"').to_string());
}
out
}
pub fn extract_f64(&self) -> Result<Collection<f64>, ParseFloatError> {
if self.subtype != "f64" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<f64> = Collection::empty();
for ff in self.branch.to_json().trim_matches(|p| p == '[' || p == ']' ).split(','){
let f: f64 = ff.parse::<f64>()?;
out.push(f);
}
Ok(out)
}
pub fn extract_3v(&self) -> Result<Collection<ThreeVec>, ParseFloatError> {
if self.subtype != "ThreeVec" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<ThreeVec> = Collection::empty();
for ff in self.branch.to_json().replace("},{","}|{").trim_matches(|p| p == '[' || p == ']' ).split('|'){
let f: ThreeVec = ThreeVec::from_str(ff)?;
out.push(f);
}
Ok(out)
}
pub fn extract_3m(&self) -> Result<Collection<ThreeMat>, ParseFloatError> {
if self.subtype != "ThreeMat" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<ThreeMat> = Collection::empty();
for ff in self.branch.to_json().replace("},{","}|{").trim_matches(|p| p == '[' || p == ']' ).split('|'){
let f: ThreeMat = ThreeMat::from_str(ff)?;
out.push(f);
}
Ok(out)
}
pub fn extract_4v(&self) -> Result<Collection<FourVec>, ParseFloatError> {
if self.subtype != "FourVec" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<FourVec> = Collection::empty();
for ff in self.branch.to_json().replace("},{","}|{").trim_matches(|p| p == '[' || p == ']' ).split('|'){
let f: FourVec = FourVec::from_str(ff)?;
out.push(f);
}
Ok(out)
}
pub fn extract_4m(&self) -> Result<Collection<FourMat>, ParseFloatError> {
if self.subtype != "FourMat" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<FourMat> = Collection::empty();
for ff in self.branch.to_json().replace("},{","}|{").trim_matches(|p| p == '[' || p == ']' ).split('|'){
let f: FourMat = FourMat::from_str(ff)?;
out.push(f);
}
Ok(out)
}
pub fn extract_bin(&self) -> Result<Collection<Bin>, ParseFloatError> {
if self.subtype != "Bin" {panic!("Used incorrect extract function. Check Branch.subtype.")}
let mut out: Collection<Bin> = Collection::empty();
for ff in self.branch.to_json().replace("},{","}|{").trim_matches(|p| p == '[' || p == ']' ).split('|'){
let f: Bin = Bin::from_str(ff)?;
out.push(f);
}
Ok(out)
}
}
impl Tree {
pub fn new(name: &'static str) -> Tree {
let mut md = HashMap::new();
md.insert("Name",name);
let br = HashMap::new();
Tree {
metadata: md,
branches: br,
}
}
pub fn add_field(&mut self, key: &'static str, f: &'static str) {
self.metadata.insert(key,f);
}
pub fn add_branch<T: 'static + Serializable>(&mut self, key: &'static str, b: Collection<T>, t: &'static str) {
match t {
"f64" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"String" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"ThreeVec" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"ThreeMat" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"FourVec" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"FourMat" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
"Bin" => {
let br = Branch::new(String::from(t),Box::new(b));
self.branches.insert(key,br);
},
x => panic!("Subtype must be one of \"f64\", \"String\", \"ThreeVec\", \"ThreeMat\", \"FourVec\", \"FourMat\", \"Bin\", not {}",x),
}
}
pub fn get_branch(&mut self, key: &'static str) -> Option<&Branch> {
self.branches.get(key)
}
}
impl Serializable for Branch {
fn to_json(&self) -> String {
format!("{{\"branch\":{},\"subtype\":{}}}",self.branch.to_json(),self.subtype.to_json())
}
}
impl Serializable for Tree {
fn to_json(&self) -> String {
let mut out = String::from("{");
for (key, val) in &self.metadata {
out.push_str(format!("\"{}\":\"{}\",",key,val).as_str());
}
out.push_str("\"branches\":{");
for (key, val) in &self.branches {
out.push_str(format!("\"{}\":{},",key,val.to_json()).as_str());
}
out.pop();
out.push_str("}}");
out
}
}
#[cfg(test)]
mod tests {
use std::io::prelude::*;
use std::io::BufWriter;
use std::fs::File;
use super::*;
#[test]
fn test_tree_json() {
let f = File::create("test_tree.json").unwrap();
let mut wr = BufWriter::new(f);
let fcol: Collection<f64> = Collection::from_vec(vec![0.0,0.0]);
let mut col_3v: Collection<ThreeVec> = Collection::empty();
for _i in 0..9 {col_3v.push(ThreeVec::random(1.0));}
let mut ttree = Tree::new("Test_Tree");
ttree.add_field("Desc", "This is a Tree for testing.");
ttree.add_branch("fcol", fcol, "f64");
ttree.add_branch("col_3v", col_3v, "ThreeVec");
wr.write(ttree.to_json().as_bytes()).unwrap();
}
}