waybar-collectd/protocol/src/err.rs

46 lines
1.6 KiB
Rust

use std::error::Error;
use std::fmt;
#[derive(PartialEq, Debug)]
pub enum ProtocolError {
EmptyResponse,
HeaderMissingValueCount,
HeaderValueCountIncorrectFormat(String),
MalformedValueReceived(String),
ValueNotFound(String, Vec<String>),
CouldNotOpenSocket(String),
CouldNotDecodeValue,
CouldNotReadOrWriteSocket(String),
}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match *self {
Self::EmptyResponse => "Got an empty response".to_string(),
Self::HeaderMissingValueCount => "Value count not found in the header".to_string(),
Self::HeaderValueCountIncorrectFormat(ref val) => {
format!("String could not be parsed as value count: {}", val)
}
Self::MalformedValueReceived(ref val) => {
format!("Could not parse value as float: {}", val)
}
Self::ValueNotFound(ref name, ref values) => format!(
"Value not found in the received values: {} in [{}]",
name,
values.join(", ")
),
Self::CouldNotOpenSocket(ref reason) => {
format!("Could not open unix socket: {}", reason)
}
Self::CouldNotDecodeValue => "Could not decode received bytes".to_string(),
Self::CouldNotReadOrWriteSocket(ref reason) => {
format!("Could not read or write socket: {}", reason)
}
};
f.write_str(&message)
}
}
impl Error for ProtocolError {}