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
use crate::*;


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// History is list of Stories
pub struct History(Stories);


impl History {
    /// New empty history
    pub fn empty() -> History {
        History(vec![])
    }


    /// New History with first element
    pub fn new(first: Story) -> History {
        History(vec![first])
    }


    /// New History with stories list
    pub fn new_from(stories: Stories) -> History {
        History(stories)
    }


    /// Stories extractor
    pub fn stories(&self) -> Stories {
        self.0.clone()
    }


    /// Head of the History - first element added
    pub fn head(&self) -> Story {
        self.0[0].clone()
    }


    /// History length
    pub fn length(&self) -> usize {
        self.0.len()
    }


    /// Append Story to History
    pub fn append(&self, story: Story) -> History {
        History([self.0.clone(), vec![story]].concat())
    }


    /// Merge History with another History
    pub fn merge(&self, a_history: History) -> History {
        match a_history {
            History(stories) => History([self.0.clone(), stories].concat()),
        }
    }
}


/// Implement JSON serialization on .to_string():
impl ToString for History {
    fn to_string(&self) -> String {
        serde_json::to_string(&self.0).unwrap_or_else(|_| {
            String::from("{\"status\": \"History serialization failure\"}")
        })
    }
}