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
use ron;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io;

#[derive(Debug, Deserialize, Serialize)]
pub struct Model {
    pub crisps: Vec<u32>,
    pub a: u32,
    pub b: u32,
    pub t: u32,
    pub p: u32,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct NormalizedModel {
    pub phased_crisps: Vec<f64>,
    pub phased_t: f64,
}

impl Model {
    pub fn parse(raw_data: Vec<String>) -> Vec<Model> {
        let mut models = Vec::new();

        for raw in raw_data {
            let normal = raw
                .split(",")
                .collect::<Vec<&str>>()
                .iter()
                .map(|s| s.parse::<u32>().unwrap())
                .collect::<Vec<u32>>();
            let (crisps, other) = &normal.split_at(normal.len() - 4);
            models.push(Model {
                crisps: crisps.to_vec(),
                a: other[0],
                b: other[1],
                t: other[2],
                p: other[3],
            })
        }

        models
    }

    pub fn parse_ron(path: String) -> io::Result<Vec<Model>> {
        let f = File::open(&path)?;
        let models: Vec<Model> = match ron::de::from_reader(f) {
            Ok(x) => x,
            Err(e) => panic!(format!("Failed to load: {}", e)),
        };
        Ok(models)
    }
}