forked from PeachCloud/peach-workspace
97 lines
3.0 KiB
Rust
97 lines
3.0 KiB
Rust
use std::{error, fmt};
|
|
|
|
use jsonrpc_core::types::error::Error as JsonRpcError;
|
|
use jsonrpc_core::ErrorCode;
|
|
use linux_embedded_hal::i2cdev::linux::LinuxI2CError;
|
|
|
|
#[derive(Debug)]
|
|
pub enum OledError {
|
|
I2CError {
|
|
source: LinuxI2CError,
|
|
},
|
|
InvalidCoordinate {
|
|
coord: String,
|
|
range: String,
|
|
value: i32,
|
|
},
|
|
InvalidFontSize {
|
|
font: String,
|
|
},
|
|
InvalidString {
|
|
len: usize,
|
|
},
|
|
MissingParameter {
|
|
source: JsonRpcError,
|
|
},
|
|
ParseError {
|
|
source: JsonRpcError,
|
|
},
|
|
}
|
|
|
|
impl error::Error for OledError {}
|
|
|
|
impl fmt::Display for OledError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
match *self {
|
|
OledError::ParseError { ref source } => {
|
|
write!(f, "Failed to parse parameter: {}", source)
|
|
}
|
|
OledError::MissingParameter { ref source } => {
|
|
write!(f, "Missing expected parameter: {}", source)
|
|
}
|
|
OledError::InvalidString { len } => {
|
|
write!(f, "String length out of range 0-21: {}", len)
|
|
}
|
|
OledError::InvalidFontSize { ref font } => {
|
|
write!(f, "Invalid font size: {}", font)
|
|
}
|
|
OledError::InvalidCoordinate {
|
|
ref coord,
|
|
ref range,
|
|
value,
|
|
} => {
|
|
write!(f, "Coordinate {} out of range {}: {}", coord, range, value)
|
|
}
|
|
OledError::I2CError { ref source } => {
|
|
write!(f, "Failed to create interface for I2C device: {}", source)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<OledError> for JsonRpcError {
|
|
fn from(err: OledError) -> Self {
|
|
match &err {
|
|
OledError::I2CError { source } => JsonRpcError {
|
|
code: ErrorCode::ServerError(-32000),
|
|
message: format!("Failed to create interface for I2C device: {}", source),
|
|
data: None,
|
|
},
|
|
OledError::InvalidCoordinate {
|
|
coord,
|
|
value,
|
|
range,
|
|
} => JsonRpcError {
|
|
code: ErrorCode::ServerError(-32001),
|
|
message: format!(
|
|
"Validation error: coordinate {} out of range {}: {}",
|
|
coord, range, value
|
|
),
|
|
data: None,
|
|
},
|
|
OledError::InvalidFontSize { font } => JsonRpcError {
|
|
code: ErrorCode::ServerError(-32002),
|
|
message: format!("Validation error: {} is not an accepted font size. Use 6x8, 6x12, 8x16 or 12x16 instead", font),
|
|
data: None,
|
|
},
|
|
OledError::InvalidString { len } => JsonRpcError {
|
|
code: ErrorCode::ServerError(-32003),
|
|
message: format!("Validation error: string length {} out of range 0-21", len),
|
|
data: None,
|
|
},
|
|
OledError::MissingParameter { source } => source.clone(),
|
|
OledError::ParseError { source } => source.clone(),
|
|
}
|
|
}
|
|
}
|