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
use console::Console;
use std::fs::File;
use std::io::{BufReader, Error, ErrorKind, Read};
use std::process;
use std::time::{Duration, Instant};
#[cfg(feature = "statistics")]
macro_rules! watch_time (
( $total:expr, $func:block ) => (
{
let beg = Instant::now();
$func;
$total += beg.elapsed();
}
);
);
#[cfg(not(feature = "statistics"))]
macro_rules! watch_time (
( $total:expr, $func:block ) => (
{
$func;
}
);
);
pub fn watch_time<F>(closure: F) -> Duration
where
F: FnOnce() -> (),
{
let start = Instant::now();
closure();
start.elapsed()
}
pub fn catch<F, T, E>(closure: F) -> Result<T, E>
where
F: FnOnce() -> Result<T, E>,
{
closure()
}
pub fn as_secsf64(dur: Duration) -> f64 {
(dur.as_secs() as f64) + (dur.subsec_nanos() as f64 / 1000_000_000.0)
}
pub fn read_from_file(path: &str) -> Result<Vec<u8>, Error> {
let file = match File::open(path) {
Ok(x) => x,
Err(e) => return Err(e),
};
let mut reader = BufReader::new(file);
let mut ret: String = String::new();
let _ = reader.read_to_string(&mut ret);
Ok(ret.into_bytes())
}
pub fn decode_error(e: ErrorKind) -> &'static str {
match e {
ErrorKind::NotFound => "file not found",
ErrorKind::PermissionDenied => "permission denied",
ErrorKind::ConnectionRefused => "connection refused",
ErrorKind::ConnectionReset => "connection reset",
ErrorKind::ConnectionAborted => "connection aborted",
ErrorKind::NotConnected => "not connected",
ErrorKind::AddrInUse => "address is in use",
ErrorKind::AddrNotAvailable => "address is not available",
ErrorKind::BrokenPipe => "broken pipe",
ErrorKind::AlreadyExists => "file is already exists",
ErrorKind::WouldBlock => "world be blocked",
ErrorKind::InvalidInput => "invalid parameter",
ErrorKind::InvalidData => "invalid data",
ErrorKind::TimedOut => "operation timeout",
ErrorKind::WriteZero => "write size is zero",
ErrorKind::Interrupted => "interrupted",
ErrorKind::Other => "unknown",
_ => "unknown",
}
}
pub enum PipelineInfo<T> {
Beg(usize),
Ok(T),
Info(String),
Err(String),
Time(Duration, Duration),
End(usize),
}
pub fn exit(code: i32, console: &mut Console) -> ! {
console.reset();
console.flush();
process::exit(code);
}