use std::fmt;
pub type ZkResult<T> = Result<T, ZkError>;
#[derive(Debug)]
#[non_exhaustive]
pub enum ZkError {
NotConnected,
Response {
message: String,
code: Option<u16>,
},
Unauthenticated,
NotSupported(String),
Network(std::io::Error),
Parse(String),
}
impl ZkError {
pub(crate) fn response(message: impl Into<String>) -> Self {
ZkError::Response {
message: message.into(),
code: None,
}
}
pub(crate) fn response_with_code(message: impl Into<String>, code: u16) -> Self {
ZkError::Response {
message: message.into(),
code: Some(code),
}
}
}
impl fmt::Display for ZkError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ZkError::NotConnected => write!(f, "instance is not connected"),
ZkError::Response { message, code } => match code {
Some(c) => write!(f, "{message} (device code {c})"),
None => write!(f, "{message}"),
},
ZkError::Unauthenticated => write!(f, "unauthenticated: wrong device password"),
ZkError::NotSupported(what) => write!(f, "not supported: {what}"),
ZkError::Network(e) => write!(f, "network error: {e}"),
ZkError::Parse(e) => write!(f, "parse error: {e}"),
}
}
}
impl std::error::Error for ZkError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ZkError::Network(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for ZkError {
fn from(e: std::io::Error) -> Self {
ZkError::Network(e)
}
}