#[cfg(feature = "image-data")]
use std::borrow::Cow;
use thiserror::Error;
#[derive(Error)]
pub enum Error {
#[error("The clipboard contents were not available in the requested format or the clipboard is empty.")]
ContentNotAvailable,
#[error("The selected clipboard is not supported with the current system configuration.")]
ClipboardNotSupported,
#[error("The native clipboard is not accessible due to being held by an other party.")]
ClipboardOccupied,
#[error("The image or the text that was about the be transferred to/from the clipboard could not be converted to the appropriate format.")]
ConversionFailure,
#[error("Unknown error while interacting with the clipboard: {description}")]
Unknown { description: String },
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
macro_rules! kind_to_str {
($( $e: pat ),*) => {
match self {
$(
$e => stringify!($e),
)*
}
}
}
let name = kind_to_str!(
ContentNotAvailable,
ClipboardNotSupported,
ClipboardOccupied,
ConversionFailure,
Unknown { .. }
);
f.write_fmt(format_args!("{} - \"{}\"", name, self))
}
}
#[cfg(feature = "image-data")]
#[derive(Debug, Clone)]
pub struct ImageData<'a> {
pub width: usize,
pub height: usize,
pub bytes: Cow<'a, [u8]>,
}
#[cfg(feature = "image-data")]
impl<'a> ImageData<'a> {
pub fn into_owned_bytes(self) -> Cow<'static, [u8]> {
self.bytes.into_owned().into()
}
pub fn to_owned_img(&self) -> ImageData<'static> {
ImageData {
width: self.width,
height: self.height,
bytes: self.bytes.clone().into_owned().into(),
}
}
}
#[cfg(any(windows, unix))]
pub(crate) struct ScopeGuard<F: FnOnce()> {
callback: Option<F>,
}
#[cfg(any(windows, unix))]
impl<F: FnOnce()> ScopeGuard<F> {
#[cfg_attr(all(windows, not(feature = "image-data")), allow(dead_code))]
pub(crate) fn new(callback: F) -> Self {
ScopeGuard { callback: Some(callback) }
}
}
#[cfg(any(windows, unix))]
impl<F: FnOnce()> Drop for ScopeGuard<F> {
fn drop(&mut self) {
if let Some(callback) = self.callback.take() {
(callback)();
}
}
}