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
use std::fmt;

#[derive(Clone, Debug)]
pub struct Event {
    pub timestamp: f64,
    intensity: Option<f64>,
    children: Vec<Event>
}


impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        
        if self.children.len() > 0 {
            write!(f, "Event({},{})", self.timestamp, self.children.iter().fold(
                String::new(),
                |acc, ev| acc + &ev.to_string()
            ))
        } else {
            write!(f, "Event({})", self.timestamp)
        }
    }
}


impl Event {
    pub fn new(timestamp: f64) -> Event {
        Event {
            timestamp,
            intensity: None,
            children: vec!()
        }
    }

    pub fn add_intensity(&mut self, intensity: f64) {
        self.intensity.get_or_insert(intensity);
    }

    pub fn add_child(&mut self, par: Event) {
        self.children.push(par);
    }

    pub fn intensity(&self) -> f64 {
        assert!(self.intensity.is_some());
        self.intensity.unwrap()
    }
}