-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
265 lines (212 loc) · 6.11 KB
/
main.rs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#![allow(unused)]
use std::io::{Stderr, Stdout, StdoutLock, Write};
use std::marker::PhantomData;
use std::process::Stdio;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{mpsc, Arc, Mutex};
use std::time::Duration;
use std::{io, thread};
fn main() {
let (sender, receiver) = mpsc::channel::<Message>();
let (disconnect_sender, disconnect_receiver) = mpsc::channel::<Disconnect>();
let mut reporter = CargoMsrvReporter::setup(sender, disconnect_receiver);
let indicatif_handler = IndicatifHandler::default();
let json_handler = JsonHandler::default();
let multi_handler = MultiHandler::new()
.push(Box::new(json_handler))
.push(Box::new(indicatif_handler));
let _writer = CargoMsrvWriter::setup(receiver, disconnect_sender, multi_handler);
reporter.report_event(Message::CurrentStatus("chris!".into()));
reporter.report_event(Message::CurrentStatus("jean!".into()));
reporter.report_event(Message::Progression(Progression {
current: 1,
max: 10,
}));
reporter.report_event(Message::CurrentStatus("chris!".into()));
reporter.report_event(Message::Event(Event::Installing));
reporter.report_event(Message::Progression(Progression {
current: 5,
max: 10,
}));
reporter.report_event(Message::Event(Event::Installing));
reporter.report_event(Message::Progression(Progression {
current: 10,
max: 10,
}));
reporter.report_event(Message::Event(Event::Installing));
let _ = reporter.disconnect();
}
trait Reporter {
type Event;
type Disconnect;
type Err;
fn setup(
sender: mpsc::Sender<Self::Event>,
disconnect_receiver: mpsc::Receiver<Self::Disconnect>,
) -> Self;
fn report_event(&mut self, event: Self::Event) -> Result<(), Self::Err>;
fn disconnect(self) -> Disconnect;
}
#[derive(serde::Serialize, Clone)]
enum Message {
Event(Event),
CurrentStatus(String),
Progression(Progression),
}
#[derive(serde::Serialize, Clone)]
enum Event {
Installing,
Updating(String),
}
#[derive(serde::Serialize, Clone)]
struct Progression {
max: u64,
current: u64,
}
struct Disconnect;
struct CargoMsrvReporter {
sender: mpsc::Sender<Message>,
disconnect_receiver: mpsc::Receiver<Disconnect>,
}
impl Reporter for CargoMsrvReporter {
type Event = Message;
type Disconnect = Disconnect;
type Err = ();
fn setup(sender: Sender<Self::Event>, disconnect_receiver: Receiver<Self::Disconnect>) -> Self {
Self {
sender,
disconnect_receiver,
}
}
fn report_event(&mut self, event: Self::Event) -> Result<(), Self::Err> {
self.sender.send(event).map_err(|_| ())
}
fn disconnect(self) -> Disconnect {
drop(self.sender);
self.disconnect_receiver.recv().unwrap()
}
}
struct CargoMsrvWriter {
handle: thread::JoinHandle<()>,
}
trait EventWriter {
type Event;
type Disconnect;
fn setup<H>(
receiver: mpsc::Receiver<Self::Event>,
disconnect_sender: mpsc::Sender<Self::Disconnect>,
handler: H,
) -> Self
where
H: EventHandler<Event = Self::Event>;
}
impl EventWriter for CargoMsrvWriter {
type Event = Message;
type Disconnect = Disconnect;
fn setup<H>(
receiver: Receiver<Self::Event>,
disconnect_sender: Sender<Self::Disconnect>,
handler: H,
) -> Self
where
H: EventHandler<Event = Self::Event>,
{
let handle = thread::spawn(move || {
let disconnect_sender = disconnect_sender;
loop {
match receiver.recv() {
Ok(message) => handler.handle(message),
Err(_e) => {
handler.finish();
eprintln!("\n\nSender closed!");
disconnect_sender.send(Disconnect).unwrap();
break;
}
}
}
});
Self { handle }
}
}
trait EventHandler: Send + 'static {
type Event;
fn handle(&self, event: Self::Event);
fn finish(&self);
}
struct IndicatifHandler {
bar: indicatif::ProgressBar,
}
impl Default for IndicatifHandler {
fn default() -> Self {
let bar = indicatif::ProgressBar::new(10);
bar.enable_steady_tick(250);
Self { bar }
}
}
impl EventHandler for IndicatifHandler {
type Event = Message;
fn handle(&self, event: Self::Event) {
match event {
Message::Event(e) => {
thread::sleep(Duration::from_secs(2));
self.bar
.set_message(format!("Event ({})", self.bar.position()))
}
Message::CurrentStatus(s) => self.bar.println(s),
Message::Progression(p) => {
self.bar.set_length(p.max);
self.bar.set_position(p.current);
}
}
}
fn finish(&self) {
self.bar.finish();
}
}
struct JsonHandler {
stdout: Arc<Mutex<Stderr>>,
}
impl Default for JsonHandler {
fn default() -> Self {
Self {
stdout: Arc::new(Mutex::new(io::stderr())),
}
}
}
impl EventHandler for JsonHandler {
type Event = Message;
fn handle(&self, event: Self::Event) {
let message = serde_json::to_string(&event).unwrap_or_default();
let mut out = self.stdout.lock().unwrap();
write!(out, "{}\n", message);
out.flush();
}
fn finish(&self) {}
}
struct MultiHandler {
handlers: Vec<Box<dyn EventHandler<Event = Message>>>,
}
impl MultiHandler {
fn new() -> Self {
Self {
handlers: Vec::new(),
}
}
fn push(mut self, handler: Box<dyn EventHandler<Event = Message>>) -> Self {
self.handlers.push(handler);
self
}
}
impl EventHandler for MultiHandler {
type Event = Message;
fn handle(&self, event: Self::Event) {
for handler in &self.handlers {
handler.handle(event.clone())
}
}
fn finish(&self) {
for handler in &self.handlers {
handler.finish()
}
}
}