copernica_common/
operations.rs

1use {
2    crossbeam_channel::{Sender},
3    std::fmt::{self},
4};
5#[derive(Clone, Debug)]
6pub enum Operations {
7    On { tx: Sender<LogEntry> },
8    Off,
9}
10impl Operations {
11    pub fn turned_on(tx: Sender<LogEntry>) -> Self {
12        Operations::On { tx }
13    }
14    pub fn turned_off() -> Self {
15        Operations::Off
16    }
17    // convenience function to reduce typing on Protocol, Link, Router API
18    pub fn label(&self, label: &str) -> (String, Self) {
19        (label.to_string(), self.clone())
20    }
21    pub fn end(&self) {
22        match self {
23            Operations::On { tx } => {
24                match tx.send(LogEntry::End) {
25                    Ok(_) => {},
26                    Err(_) => {},
27                }
28            },
29            Operations::Off => {}
30        }
31    }
32    pub fn register_protocol(&self, label: String) {
33        match self {
34            Operations::On { tx } => {
35                match tx.send(LogEntry::register(&label)) {
36                    Ok(_) => {},
37                    Err(_) => {},
38                }
39            },
40            Operations::Off => {}
41        }
42    }
43    pub fn register_link(&self, label: String) {
44        match self {
45            Operations::On { tx } => {
46                match tx.send(LogEntry::register(&label)) {
47                    Ok(_) => {},
48                    Err(_) => {},
49                }
50            },
51            Operations::Off => {}
52        }
53    }
54    pub fn register_router(&self, label: String) {
55        match self {
56            Operations::On { tx } => {
57                match tx.send(LogEntry::register(&label)) {
58                    Ok(_) => {},
59                    Err(_) => {},
60                }
61            },
62            Operations::Off => {}
63        }
64    }
65    pub fn message_from(&self, label: String) {
66        match self {
67            Operations::On { tx } => {
68                match tx.send(LogEntry::message(&label)) {
69                    Ok(_) => {},
70                    Err(_) => {},
71                }
72            },
73            Operations::Off => {}
74        }
75    }
76    pub fn found_response_upstream(&self, label: String) {
77        match self {
78            Operations::On { tx } => {
79                match tx.send(LogEntry::found_response_upstream(&label)) {
80                    Ok(_) => {},
81                    Err(_) => {},
82                }
83            },
84            Operations::Off => {}
85        }
86    }
87    pub fn response_arrived_downstream(&self, label: String) {
88        match self {
89            Operations::On { tx } => {
90                match tx.send(LogEntry::response_arrived_downstream(&label)) {
91                    Ok(_) => {},
92                    Err(_) => {},
93                }
94            },
95            Operations::Off => {}
96        }
97    }
98    pub fn forward_response_downstream(&self, label: String) {
99        match self {
100            Operations::On { tx } => {
101                match tx.send(LogEntry::forward_response_downstream(&label)) {
102                    Ok(_) => {},
103                    Err(_) => {},
104                }
105            },
106            Operations::Off => {}
107        }
108    }
109    pub fn forward_request_upstream(&self, label: String) {
110        match self {
111            Operations::On { tx } => {
112                match tx.send(LogEntry::forward_request_upstream(&label)) {
113                    Ok(_) => {},
114                    Err(_) => {},
115                }
116            },
117            Operations::Off => {}
118        }
119    }
120}
121
122#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
123pub enum LogEntry {
124    End,
125    Register {
126        label: String,
127    },
128    Message {
129        label: String,
130    },
131    FoundResponseUpstream {
132        label: String,
133    },
134    ResponseArrivedDownstream {
135        label: String,
136    },
137    ForwardResponseDownstream {
138        label: String,
139    },
140    ForwardRequestUpstream {
141        label: String,
142    },
143}
144impl LogEntry {
145    pub fn end() -> Self {
146        LogEntry::End
147    }
148    pub fn register(label: &str) -> Self {
149        //LogEntry::Register { label: format!("register\t\t\t| {}", label) }
150        LogEntry::Register { label: format!("expected_behaviour.insert(LogEntry::register({}.clone()),", label) }
151    }
152    pub fn message(label: &str) -> Self {
153        //LogEntry::Message { label: format!("message\t\t\t\t| {}", &label)  }
154        LogEntry::Message { label: format!("expected_behaviour.insert(LogEntry::message({}.clone()),", &label)  }
155    }
156    pub fn found_response_upstream(label: &str) -> Self {
157        //LogEntry::FoundResponseUpstream { label: format!("found_response_upstream\t\t| {}", &label)  }
158        LogEntry::FoundResponseUpstream { label: format!("expected_behaviour.insert(LogEntry::found_response_upstream({}.clone()),", &label)  }
159    }
160    pub fn response_arrived_downstream(label: &str) -> Self {
161        //LogEntry::ResponseArrivedDownstream { label: format!("response_arrived_downstream\t| {}", &label)  }
162        LogEntry::ResponseArrivedDownstream { label: format!("expected_behaviour.insert(LogEntry::response_arrived_downstream({}.clone()),", &label)  }
163    }
164    pub fn forward_request_upstream(label: &str) -> Self {
165        //LogEntry::ForwardRequestUpstream { label: format!("forward_request_upstream\t| {}", &label)  }
166        LogEntry::ForwardRequestUpstream { label: format!("expected_behaviour.insert(LogEntry::forward_request_upstream({}.clone()),", &label)  }
167    }
168    pub fn forward_response_downstream(label: &str) -> Self {
169        //LogEntry::ForwardResponseDownstream { label: format!("forward_response_downstream\t| {}", &label)  }
170        LogEntry::ForwardResponseDownstream { label: format!("expected_behaviour.insert(LogEntry::forward_response_downstream({}.clone()),", &label)  }
171    }
172}
173impl fmt::Display for LogEntry {
174    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175        let out = match self {
176            LogEntry::Register { label } => {
177                format!("{}", label)
178            }
179            LogEntry::Message { label } => {
180                format!("{}", label)
181            },
182            LogEntry::FoundResponseUpstream { label } => {
183                format!("{}", label)
184            },
185            LogEntry::ResponseArrivedDownstream { label } => {
186                format!("{}", label)
187            },
188            LogEntry::ForwardResponseDownstream { label } => {
189                format!("{}", label)
190            },
191            LogEntry::ForwardRequestUpstream { label } => {
192                format!("{}", label)
193            },
194            LogEntry::End => {
195                format!("end")
196            },
197        };
198        write!(f, "{}", out)
199    }
200}