acts_channel/
action_result.rs1use std::fmt::Debug;
2
3use crate::utils;
4use tonic::Status;
5
6pub struct ActionResult<T> {
7 pub start_time: i64,
8 pub end_time: i64,
9 pub data: Option<T>,
10}
11
12impl<T> ActionResult<T> {
13 pub fn begin() -> Self {
14 Self {
15 start_time: utils::time_millis(),
16 end_time: 0,
17 data: None,
18 }
19 }
20
21 #[allow(clippy::result_large_err)]
22 pub fn end(mut self) -> Result<Self, Status> {
23 self.end_time = utils::time_millis();
24 Ok(self)
25 }
26
27 pub fn cost(&self) -> i64 {
29 self.end_time - self.start_time
30 }
31}
32
33impl<T> std::fmt::Debug for ActionResult<T>
34where
35 T: Debug,
36{
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.debug_struct("State")
39 .field("start_time", &self.start_time)
40 .field("end_time", &self.end_time)
41 .field("data", &self.data)
42 .finish()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use crate::ActionResult;
49
50 #[test]
51 fn action_result_begin() {
52 let state = ActionResult::<()>::begin();
53 assert!(state.start_time > 0)
54 }
55
56 #[test]
57 fn action_result_end() {
58 let state = ActionResult::<()>::begin();
59 std::thread::sleep(std::time::Duration::from_millis(2));
60 let result = state.end();
61 assert!(result.unwrap().cost() > 0)
62 }
63
64 #[test]
65 fn action_data_ok() {
66 let mut state = ActionResult::<i32>::begin();
67 state.data = Some(1);
68 assert_eq!(state.data.is_some(), true);
69 }
70}