2021-01-30 03:06:18 +05:30
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
|
2020-10-26 23:25:04 +05:30
|
|
|
use std::fmt::{self, Display, Formatter};
|
2021-01-30 03:06:18 +05:30
|
|
|
use std::io;
|
2020-10-26 23:25:04 +05:30
|
|
|
|
2021-01-30 03:06:18 +05:30
|
|
|
// Custom error type for early exit.
|
2020-10-26 23:25:04 +05:30
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SilentExit {
|
|
|
|
pub code: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SilentExit {
|
|
|
|
fn fmt(&self, _: &mut Formatter) -> fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 03:06:18 +05:30
|
|
|
|
|
|
|
pub trait WriteErrorHandler {
|
|
|
|
fn handle_err(self, device: &str) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WriteErrorHandler for io::Result<()> {
|
|
|
|
fn handle_err(self, device: &str) -> Result<()> {
|
|
|
|
match self {
|
|
|
|
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => bail!(SilentExit { code: 0 }),
|
|
|
|
result => result.with_context(|| format!("could not write to {}", device)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|