feat: add methods for loading raw data
This commit is contained in:
parent
5dd795d463
commit
71df657777
9 changed files with 153 additions and 63 deletions
26
src/lib.rs
26
src/lib.rs
|
|
@ -1,4 +1,4 @@
|
|||
pub mod mime;
|
||||
pub use mime;
|
||||
|
||||
#[cfg(all(
|
||||
unix,
|
||||
|
|
@ -48,7 +48,7 @@ mod platform;
|
|||
#[path = "platform/dummy.rs"]
|
||||
mod platform;
|
||||
|
||||
use mime::{ClipboardLoadData, ClipboardStoreData};
|
||||
use mime::ClipboardStoreData;
|
||||
use raw_window_handle::HasDisplayHandle;
|
||||
use std::error::Error;
|
||||
|
||||
|
|
@ -108,7 +108,7 @@ pub trait ClipboardProvider {
|
|||
|
||||
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||
where
|
||||
ClipboardLoadData<T>: platform::InnerAllowedMimeTypes,
|
||||
T: mime::AllowedMimeTypes,
|
||||
{
|
||||
None
|
||||
}
|
||||
|
|
@ -118,24 +118,38 @@ pub trait ClipboardProvider {
|
|||
_contents: ClipboardStoreData<T>,
|
||||
) -> Option<Result<(), Box<dyn Error>>>
|
||||
where
|
||||
ClipboardStoreData<T>: platform::InnerAsMimeTypes,
|
||||
T: mime::AsMimeTypes,
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||
where
|
||||
ClipboardLoadData<T>: platform::InnerAllowedMimeTypes,
|
||||
T: mime::AllowedMimeTypes,
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
fn read_primary_raw(
|
||||
&self,
|
||||
_allowed: Vec<String>,
|
||||
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn read_raw(
|
||||
&self,
|
||||
_allowed: Vec<String>,
|
||||
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn write_primary_data<T: Send + Sync + 'static>(
|
||||
&mut self,
|
||||
_contents: ClipboardStoreData<T>,
|
||||
) -> Option<Result<(), Box<dyn Error>>>
|
||||
where
|
||||
ClipboardStoreData<T>: platform::InnerAsMimeTypes,
|
||||
T: mime::AsMimeTypes,
|
||||
{
|
||||
None
|
||||
}
|
||||
|
|
|
|||
45
src/mime.rs
45
src/mime.rs
|
|
@ -1,45 +0,0 @@
|
|||
// need a type that can implement traits for storing custom data
|
||||
|
||||
use std::{borrow::Cow, error, fmt};
|
||||
|
||||
/// Data that can be loaded from the clipboard.
|
||||
pub struct ClipboardLoadData<T>(pub T);
|
||||
|
||||
/// Describes the mime types which are accepted.
|
||||
pub trait AllowedMimeTypes:
|
||||
TryFrom<(Vec<u8>, String)> + Send + Sync + 'static
|
||||
{
|
||||
/// List allowed mime types for the type to convert from a byte slice.
|
||||
///
|
||||
/// Allowed mime types should be listed in order of decreasing preference,
|
||||
/// most preferred first.
|
||||
fn allowed() -> Cow<'static, [String]>;
|
||||
}
|
||||
|
||||
/// Can be converted to data with the available mime types.
|
||||
pub trait AsMimeTypes {
|
||||
/// List available mime types for this data to convert to a byte slice.
|
||||
fn available(&self) -> Cow<'static, [String]>;
|
||||
|
||||
/// Converts a type to a byte slice for the given mime type if possible.
|
||||
fn as_bytes(&self, mime_type: &str) -> Option<Cow<'static, [u8]>>;
|
||||
}
|
||||
|
||||
/// Data that can be stored to the clipboard.
|
||||
pub struct ClipboardStoreData<T> {
|
||||
/// Clipboard data.
|
||||
pub data: T,
|
||||
/// Available mime types for the clipboard data.
|
||||
pub available_mime_types: Vec<Cow<'static, str>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Error;
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Unsupported mime type")
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {}
|
||||
|
|
@ -5,13 +5,9 @@ use crate::{
|
|||
|
||||
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
|
||||
use std::error::Error;
|
||||
use wayland::MimeType;
|
||||
|
||||
pub use clipboard_wayland as wayland;
|
||||
pub use clipboard_x11 as x11;
|
||||
pub use wayland::{
|
||||
AllowedMimeTypes as InnerAllowedMimeTypes, AsMimeTypes as InnerAsMimeTypes,
|
||||
};
|
||||
|
||||
pub enum Clipboard {
|
||||
Wayland(wayland::Clipboard),
|
||||
|
|
@ -54,7 +50,7 @@ impl ClipboardProvider for Clipboard {
|
|||
|
||||
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||
where
|
||||
ClipboardLoadData<T>: InnerAllowedMimeTypes,
|
||||
T: mime::AllowedMimeTypes,
|
||||
{
|
||||
match self {
|
||||
Clipboard::Wayland(c) => {
|
||||
|
|
@ -70,7 +66,7 @@ impl ClipboardProvider for Clipboard {
|
|||
contents: ClipboardStoreData<T>,
|
||||
) -> Option<Result<(), Box<dyn Error>>>
|
||||
where
|
||||
ClipboardStoreData<T>: InnerAsMimeTypes,
|
||||
T: mime::AsMimeTypes,
|
||||
{
|
||||
match self {
|
||||
Clipboard::Wayland(c) => {
|
||||
|
|
@ -82,7 +78,7 @@ impl ClipboardProvider for Clipboard {
|
|||
|
||||
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
|
||||
where
|
||||
ClipboardLoadData<T>: InnerAllowedMimeTypes,
|
||||
T: mime::AllowedMimeTypes,
|
||||
{
|
||||
match self {
|
||||
Clipboard::Wayland(c) => {
|
||||
|
|
@ -93,12 +89,32 @@ impl ClipboardProvider for Clipboard {
|
|||
}
|
||||
}
|
||||
|
||||
fn read_primary_raw(
|
||||
&self,
|
||||
allowed: Vec<String>,
|
||||
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => Some(c.read_primary_raw(allowed)),
|
||||
Clipboard::X11(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn read_raw(
|
||||
&self,
|
||||
allowed: Vec<String>,
|
||||
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => Some(c.read_raw(allowed)),
|
||||
Clipboard::X11(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_primary_data<T: Send + Sync + 'static>(
|
||||
&mut self,
|
||||
contents: ClipboardStoreData<T>,
|
||||
) -> Option<Result<(), Box<dyn Error>>>
|
||||
where
|
||||
ClipboardStoreData<T>: InnerAsMimeTypes,
|
||||
T: mime::AsMimeTypes,
|
||||
{
|
||||
match self {
|
||||
Clipboard::Wayland(c) => {
|
||||
|
|
@ -121,51 +137,3 @@ pub unsafe fn connect<W: HasDisplayHandle>(
|
|||
|
||||
Ok(clipboard)
|
||||
}
|
||||
|
||||
impl<T: crate::mime::AsMimeTypes> InnerAsMimeTypes for ClipboardLoadData<T> {
|
||||
fn available(&self) -> std::borrow::Cow<'static, [MimeType]> {
|
||||
self.0
|
||||
.available()
|
||||
.into_iter()
|
||||
.map(|m| MimeType::Other(m.clone().into()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn as_bytes(
|
||||
&self,
|
||||
mime_type: &MimeType,
|
||||
) -> Option<std::borrow::Cow<'static, [u8]>> {
|
||||
self.0.as_bytes(mime_type.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: crate::mime::AllowedMimeTypes> InnerAllowedMimeTypes
|
||||
for ClipboardLoadData<T>
|
||||
where
|
||||
ClipboardLoadData<T>: TryFrom<(Vec<u8>, MimeType)>,
|
||||
{
|
||||
// TODO select text variants if string matches...
|
||||
fn allowed() -> std::borrow::Cow<'static, [wayland::MimeType]> {
|
||||
T::allowed()
|
||||
.into_iter()
|
||||
.map(|s| MimeType::Other(s.clone().into()))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(Vec<u8>, MimeType)> for ClipboardLoadData<T>
|
||||
where
|
||||
T: for<'b> TryFrom<(Vec<u8>, String)>,
|
||||
T: 'static,
|
||||
{
|
||||
type Error = crate::mime::Error;
|
||||
|
||||
fn try_from(
|
||||
(value, mime): (Vec<u8>, MimeType),
|
||||
) -> Result<Self, Self::Error> {
|
||||
let mime = mime.to_string();
|
||||
Ok(ClipboardLoadData(
|
||||
T::try_from((value, mime)).map_err(|_| crate::mime::Error)?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue