tmux-copyrat/src/error.rs

47 lines
1.5 KiB
Rust
Raw Normal View History

2020-05-24 21:02:11 +02:00
use std::fmt;
#[derive(Debug)]
pub enum ParseError {
2020-05-25 23:06:00 +02:00
ExpectedSurroundingPair,
UnknownAlphabet,
UnknownColor,
ExpectedPaneIdMarker,
ExpectedInt(std::num::ParseIntError),
ExpectedBool(std::str::ParseBoolError),
ProcessFailure(String),
2020-05-24 21:02:11 +02:00
}
impl fmt::Display for ParseError {
2020-05-25 23:06:00 +02:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParseError::ExpectedSurroundingPair => write!(f, "Expected 2 chars"),
ParseError::UnknownAlphabet => write!(f, "Expected a known alphabet"),
ParseError::UnknownColor => {
write!(f, "Expected ANSI color name (magenta, cyan, black, ...)")
}
ParseError::ExpectedPaneIdMarker => write!(f, "Expected pane id marker"),
ParseError::ExpectedInt(msg) => write!(f, "Expected an int: {}", msg),
ParseError::ExpectedBool(msg) => write!(f, "Expected a bool: {}", msg),
ParseError::ProcessFailure(msg) => write!(f, "{}", msg),
}
}
}
impl From<std::num::ParseIntError> for ParseError {
fn from(error: std::num::ParseIntError) -> Self {
ParseError::ExpectedInt(error)
}
}
impl From<std::str::ParseBoolError> for ParseError {
fn from(error: std::str::ParseBoolError) -> Self {
ParseError::ExpectedBool(error)
}
}
impl From<std::io::Error> for ParseError {
fn from(error: std::io::Error) -> Self {
ParseError::ProcessFailure(error.to_string())
2020-05-24 21:02:11 +02:00
}
}