1use opts;
3use std::{borrow::Cow, fmt, time::SystemTime};
4pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug, PartialEq, Eq, Clone)]
8pub enum Success {
10 Bytes {
13 bytes: usize,
15 start: SystemTime,
17 },
18 Unblock {
21 blocks: usize,
23 block_size: usize,
25 start: SystemTime,
26 },
27 Block {
29 lines: usize,
31 truncated: usize,
33 padded: usize,
35 block_size: usize,
37 start: SystemTime,
39 },
40}
41#[derive(Debug)]
42pub enum Error {
44 IO(std::io::Error),
46 Opt(opts::Error),
49}
50
51impl fmt::Display for Error {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 match self {
54 Error::IO(err) => write!(f, "io error: {}", err),
55 Error::Opt(err) => write!(f, "command line argument error: {}", err),
56 }
57 }
58}
59
60impl From<std::io::Error> for Error {
61 fn from(err: ::std::io::Error) -> Self { Error::IO(err) }
62}
63impl From<opts::Error> for Error {
64 fn from(err: opts::Error) -> Self { Error::Opt(err) }
65}
66impl std::error::Error for Error {}
67impl fmt::Display for Success {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 match self {
70 Success::Bytes { bytes, start } => write!(f, "wrote {} bytes in {}", bytes, elapsed(start)),
71 Success::Block {
72 lines,
73 truncated,
74 padded,
75 block_size,
76 start,
77 } => write!(
78 f,
79 concat!(
80 "Wrote {lines} lines of length {len} in {seconds}.\n",
81 "Padded {padded}/{len} lines\n",
82 "Truncated {truncated}/{len} lines\n"
83 ),
84 lines = lines,
85 truncated = truncated,
86 len = block_size,
87 padded = padded,
88 seconds = elapsed(start),
89 ),
90 Success::Unblock {
91 blocks,
92 block_size,
93 start,
94 } => write!(
95 f,
96 "wrote {} records of fixed size {} in {}",
97 blocks,
98 block_size,
99 elapsed(start)
100 ),
101 }
102 }
103}
104
105pub fn elapsed(start: &SystemTime) -> Cow<'static, str> {
107 match start.elapsed() {
108 Ok(elapsed) => Cow::Owned(format!("{}.{:03} seconds", elapsed.as_secs(), elapsed.subsec_millis())),
109 _ => Cow::Borrowed("<error obtaining elapsed time>"),
110 }
111}