feat: add methods for loading raw data

This commit is contained in:
Ashley Wulber 2024-03-14 16:42:24 -04:00
parent 5dd795d463
commit 71df657777
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
9 changed files with 153 additions and 63 deletions

11
mime/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "mime"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies]
smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-mime-types" }

47
mime/src/lib.rs Normal file
View file

@ -0,0 +1,47 @@
pub mod platform;
// 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 {}

View file

@ -0,0 +1,47 @@
use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType};
use crate::{ClipboardLoadData, ClipboardStoreData};
impl<T: crate::AsMimeTypes> AsMimeTypes for ClipboardStoreData<T> {
fn available(&self) -> std::borrow::Cow<'static, [MimeType]> {
self.data
.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.data.as_bytes(mime_type.as_ref())
}
}
impl<T: crate::AllowedMimeTypes> AllowedMimeTypes for ClipboardLoadData<T> {
// TODO select text variants if string matches...
fn allowed() -> std::borrow::Cow<'static, [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::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::Error)?,
))
}
}

11
mime/src/platform/mod.rs Normal file
View file

@ -0,0 +1,11 @@
#[cfg(all(
unix,
not(any(
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "emscripten",
target_os = "redox"
))
))]
pub mod linux;