atagen

macltok

public
Star0
Watch0
d1313f47f24eff9c7654a76b44fdf9e65ba6db5d
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git
src/repair_model.rsRust152 lines4.1 KB
1
use ruma::OwnedRoomId;
2
3
#[cfg(feature = "serde")]
4
use serde::{Deserialize, Serialize};
5
6
use crate::Error;
7
8
#[derive(Clone, Debug, PartialEq)]
9
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10
pub enum HistoryRepairPolicy {
11
    StartRecent,
12
    Bridge,
13
    BridgeShortGaps { threshold_ms: u64 },
14
}
15
16
impl Default for HistoryRepairPolicy {
17
    fn default() -> Self {
18
        Self::StartRecent
19
    }
20
}
21
22
#[derive(Clone, Debug, PartialEq)]
23
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24
pub struct HistoryRepairBudget {
25
    pub max_pages: u16,
26
    pub max_events: u32,
27
    pub max_duration_ms: u64,
28
}
29
30
impl Default for HistoryRepairBudget {
31
    fn default() -> Self {
32
        Self {
33
            max_pages: 10,
34
            max_events: 1000,
35
            max_duration_ms: 15_000,
36
        }
37
    }
38
}
39
40
#[derive(Clone, Debug, PartialEq)]
41
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42
pub enum HistoryRepairCommand {
43
    Start {
44
        room_id: OwnedRoomId,
45
        policy: Option<HistoryRepairPolicy>,
46
        budget: HistoryRepairBudget,
47
    },
48
    Continue {
49
        room_id: OwnedRoomId,
50
        budget: HistoryRepairBudget,
51
    },
52
    Cancel {
53
        room_id: OwnedRoomId,
54
    },
55
    StartRecent {
56
        room_id: OwnedRoomId,
57
    },
58
    GetPolicy,
59
    SetPolicy(HistoryRepairPolicy),
60
}
61
62
#[derive(Clone, Debug, PartialEq)]
63
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
64
pub enum HistoryRepairStrategy {
65
    Recent,
66
    Overlap,
67
    Bridge,
68
}
69
70
#[derive(Clone, Debug, PartialEq)]
71
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
72
pub enum HistoryContinuity {
73
    Unresolved,
74
    Verified,
75
    AccessibleStart,
76
}
77
78
#[derive(Clone, Debug, PartialEq)]
79
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
80
pub enum HistoryRepairPhase {
81
    FetchingRecent,
82
    Bridging,
83
    Ready,
84
    Paused,
85
}
86
87
#[derive(Clone, Debug, PartialEq)]
88
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
89
pub enum HistoryRepairStop {
90
    Cancelled,
91
    Budget,
92
    HistoryUnavailable,
93
    NoAnchor,
94
    CursorStalled,
95
}
96
97
#[derive(Clone, Debug, PartialEq)]
98
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
99
pub struct HistoryRepairReport {
100
    pub room_id: OwnedRoomId,
101
    pub requested_policy: HistoryRepairPolicy,
102
    pub strategy: HistoryRepairStrategy,
103
    pub coverage_duration_ms: Option<u64>,
104
    pub threshold_evaluated: bool,
105
    pub pages: u32,
106
    pub events: u32,
107
    pub phase: HistoryRepairPhase,
108
    pub continuity: HistoryContinuity,
109
    pub recent_available: bool,
110
    pub stop: Option<HistoryRepairStop>,
111
    pub failure: Option<Error>,
112
}
113
114
#[cfg(all(test, feature = "serde"))]
115
mod tests {
116
    use super::*;
117
    use crate::ErrorKind;
118
    use ruma::room_id;
119
120
    #[test]
121
    fn history_repair_types_round_trip_through_serde() {
122
        let command = HistoryRepairCommand::Start {
123
            room_id: room_id!("!room:example.org").to_owned(),
124
            policy: Some(HistoryRepairPolicy::BridgeShortGaps {
125
                threshold_ms: 86_400_000,
126
            }),
127
            budget: HistoryRepairBudget::default(),
128
        };
129
        let encoded_command = serde_json::to_value(&command).unwrap();
130
        let decoded_command: HistoryRepairCommand =
131
            serde_json::from_value(encoded_command).unwrap();
132
        assert_eq!(decoded_command, command);
133
134
        let report = HistoryRepairReport {
135
            room_id: room_id!("!room:example.org").to_owned(),
136
            requested_policy: HistoryRepairPolicy::Bridge,
137
            strategy: HistoryRepairStrategy::Overlap,
138
            coverage_duration_ms: Some(86_400_000),
139
            threshold_evaluated: false,
140
            pages: 3,
141
            events: 42,
142
            phase: HistoryRepairPhase::Paused,
143
            continuity: HistoryContinuity::Unresolved,
144
            recent_available: true,
145
            stop: Some(HistoryRepairStop::Budget),
146
            failure: Some(Error::new(ErrorKind::Timeline, "repair failed")),
147
        };
148
        let encoded_report = serde_json::to_value(&report).unwrap();
149
        let decoded_report: HistoryRepairReport = serde_json::from_value(encoded_report).unwrap();
150
        assert_eq!(decoded_report, report);
151
    }
152
}