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
use chrono::Local;

use crate::*;


/// Alias Type for Vec<Story>
pub type Stories = Vec<Story>;


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// Story holds errornous state
pub struct Story {
    /// Story - timestamp
    pub timestamp: String,

    /// Story - failure count
    pub count: u64,

    /// Story - success
    #[serde(skip_serializing_if = "Option::is_none")]
    pub success: Option<Expected>,

    /// Story - keep history of unexpected results (failures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<Unexpected>,
}


impl Story {
    /// New success-story
    pub fn success(success: Expected) -> Story {
        Story {
            count: 1,
            timestamp: Local::now().to_rfc3339(),
            success: Some(success),
            error: None,
        }
    }


    /// New error-story
    pub fn error(error: Unexpected) -> Story {
        Story {
            count: 1,
            timestamp: Local::now().to_rfc3339(),
            success: None,
            error: Some(error),
        }
    }
}


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