use std::io;
use std::path::PathBuf;
use std::time::Duration;
use thiserror::Error;
use crate::keymap::Revision;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("device discovery failed: {message}")]
Discovery {
message: String,
},
#[error("no Codex Micro ({vendor_id:04x}:{product_id:04x}) was found")]
DeviceNotFound {
vendor_id: u16,
product_id: u16,
},
#[error("permission denied opening {path}; check the Codex Micro udev rule")]
PermissionDenied {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("{operation} failed: {source}")]
Io {
operation: &'static str,
#[source]
source: io::Error,
},
#[error("Codex Micro disconnected")]
Disconnected,
#[error("{operation} timed out after {timeout:?}")]
Timeout {
operation: String,
timeout: Duration,
},
#[error("protocol error: {message}")]
Protocol {
message: String,
},
#[error("RPC method {method} failed: {message}")]
Rpc {
method: String,
code: Option<i64>,
message: String,
data: Option<Box<serde_json::Value>>,
},
#[error("unsupported firmware {found}; libremicro v1 requires {supported}")]
UnsupportedFirmware {
found: String,
supported: &'static str,
},
#[error("invalid {context}: {message}")]
Validation {
context: &'static str,
message: String,
},
#[error("stale keymap plan: expected revision {expected}, found {actual}")]
StaleConfiguration {
expected: Revision,
actual: Revision,
},
#[error("keymap write outcome is indeterminate: {message}")]
IndeterminateWrite {
message: String,
},
}
impl Error {
pub(crate) fn protocol(message: impl Into<String>) -> Self {
Self::Protocol {
message: message.into(),
}
}
pub(crate) fn validation(context: &'static str, message: impl Into<String>) -> Self {
Self::Validation {
context,
message: message.into(),
}
}
}