use std::fmt;
use std::io;
use std::sync::Arc;
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum ClipboardError {
LibNotFound,
NoDisplay,
PayloadTooLarge,
FocusRequired,
UnsupportedMime,
UnsupportedAsync,
InvalidUri,
BackendUnavailable,
Io(Arc<io::Error>),
}
impl ClipboardError {
pub(crate) fn io(e: io::Error) -> Self {
Self::Io(Arc::new(e))
}
pub(crate) fn io_other(msg: &str) -> Self {
Self::io(io::Error::other(msg))
}
}
impl fmt::Display for ClipboardError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LibNotFound => write!(f, "required native library not found"),
Self::NoDisplay => write!(f, "no display server or TTY available"),
Self::PayloadTooLarge => write!(f, "payload exceeds size cap"),
Self::FocusRequired => {
write!(f, "compositor requires focus (no data-control protocol)")
}
Self::UnsupportedMime => write!(f, "MIME type not supported by active backend"),
Self::UnsupportedAsync => {
write!(f, "async not supported by active backend")
}
Self::InvalidUri => write!(f, "URI must be absolute (RFC 3986)"),
Self::BackendUnavailable => {
write!(f, "backend was not available at runtime")
}
Self::Io(e) => write!(f, "I/O error: {e}"),
}
}
}
impl std::error::Error for ClipboardError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(&**e),
_ => None,
}
}
}
impl From<io::Error> for ClipboardError {
fn from(e: io::Error) -> Self {
Self::io(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clone_clipboard_error_smoke() {
let variants: Vec<ClipboardError> = vec![
ClipboardError::LibNotFound,
ClipboardError::NoDisplay,
ClipboardError::PayloadTooLarge,
ClipboardError::FocusRequired,
ClipboardError::UnsupportedMime,
ClipboardError::UnsupportedAsync,
ClipboardError::InvalidUri,
ClipboardError::BackendUnavailable,
ClipboardError::io_other("test io error"),
];
for v in &variants {
let cloned = v.clone();
assert_eq!(
v.to_string(),
cloned.to_string(),
"clone Display mismatch for {v:?}"
);
}
}
#[test]
fn io_arc_clone_shares_message() {
let e = ClipboardError::io_other("shared arc message");
let e2 = e.clone();
assert_eq!(e.to_string(), e2.to_string());
}
}