refactor: reuse built-ins and shared helpers
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
use core::fmt;
|
||||
|
||||
use crate::ptt::{AudioTransmitGate, PttBackendDescriptor};
|
||||
use thiserror::Error;
|
||||
|
||||
mod focused;
|
||||
|
||||
@@ -108,35 +109,51 @@ impl fmt::Display for PttInputClass {
|
||||
}
|
||||
|
||||
/// Errors raised by a desktop PTT backend.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum PttBackendError {
|
||||
/// The OS rejected the backend initialisation (e.g. Raw Input
|
||||
/// registration failed, event tap creation failed).
|
||||
#[error("init failed: {0}")]
|
||||
Init(String),
|
||||
/// The user-granted permission required for global capture is
|
||||
/// not granted (typically macOS Input Monitoring / Accessibility).
|
||||
#[error("permission denied")]
|
||||
PermissionDenied,
|
||||
/// The display server or compositor does not expose the
|
||||
/// expected interface (typically a non-tested Linux compositor).
|
||||
#[error("unsupported environment")]
|
||||
UnsupportedEnvironment,
|
||||
/// Caller submitted a binding whose `platform_key` cannot be
|
||||
/// parsed in the active OS.
|
||||
#[error("invalid binding: {0}")]
|
||||
InvalidBinding(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for PttBackendError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Init(s) => write!(f, "init failed: {s}"),
|
||||
Self::PermissionDenied => f.write_str("permission denied"),
|
||||
Self::UnsupportedEnvironment => f.write_str("unsupported environment"),
|
||||
Self::InvalidBinding(s) => write!(f, "invalid binding: {s}"),
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ptt_backend_error_display_strings_stay_stable() {
|
||||
assert_eq!(
|
||||
PttBackendError::Init("rawinput".into()).to_string(),
|
||||
"init failed: rawinput"
|
||||
);
|
||||
assert_eq!(
|
||||
PttBackendError::PermissionDenied.to_string(),
|
||||
"permission denied"
|
||||
);
|
||||
assert_eq!(
|
||||
PttBackendError::UnsupportedEnvironment.to_string(),
|
||||
"unsupported environment"
|
||||
);
|
||||
assert_eq!(
|
||||
PttBackendError::InvalidBinding("bad key".into()).to_string(),
|
||||
"invalid binding: bad key"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for PttBackendError {}
|
||||
|
||||
/// Cross-platform desktop PTT backend (SDD-081).
|
||||
///
|
||||
/// All implementations call exactly the audio transmit gate's
|
||||
|
||||
Reference in New Issue
Block a user