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)]
pub struct History(Stories);
impl History {
pub fn empty() -> History {
History(vec![])
}
pub fn new(first: Story) -> History {
History(vec![first])
}
pub fn new_from(stories: Stories) -> History {
History(stories)
}
pub fn stories(&self) -> Stories {
self.0.clone()
}
pub fn head(&self) -> Story {
self.0[0].clone()
}
pub fn length(&self) -> usize {
self.0.len()
}
pub fn append(&self, story: Story) -> History {
History([self.0.clone(), vec![story]].concat())
}
pub fn merge(&self, a_history: History) -> History {
match a_history {
History(stories) => History([self.0.clone(), stories].concat()),
}
}
}
impl ToString for History {
fn to_string(&self) -> String {
serde_json::to_string(&self.0).unwrap_or_else(|_| {
String::from("{\"status\": \"History serialization failure\"}")
})
}
}