mime/
lib.rs

1pub mod platform;
2
3// need a type that can implement traits for storing custom data
4
5use std::{borrow::Cow, error, fmt};
6
7/// Raw data from the clipboard
8pub struct ClipboardData(pub Vec<u8>, pub String);
9
10impl AllowedMimeTypes for ClipboardData {
11    fn allowed() -> Cow<'static, [String]> {
12        Cow::Owned(vec![])
13    }
14}
15
16impl TryFrom<(Vec<u8>, String)> for ClipboardData {
17    type Error = Error;
18
19    fn try_from((data, mime): (Vec<u8>, String)) -> Result<Self, Self::Error> {
20        Ok(ClipboardData(data, mime))
21    }
22}
23
24/// Data that can be loaded from the clipboard.
25pub struct ClipboardLoadData<T>(pub T);
26
27/// Describes the mime types which are accepted.
28pub trait AllowedMimeTypes:
29    TryFrom<(Vec<u8>, String)> + Send + Sync + 'static
30{
31    /// List allowed mime types for the type to convert from a byte slice.
32    ///
33    /// Allowed mime types should be listed in order of decreasing preference,
34    /// most preferred first.
35    fn allowed() -> Cow<'static, [String]>;
36}
37
38/// Can be converted to data with the available mime types.
39pub trait AsMimeTypes {
40    /// List available mime types for this data to convert to a byte slice.
41    fn available(&self) -> Cow<'static, [String]>;
42
43    /// Converts a type to a byte slice for the given mime type if possible.
44    fn as_bytes(&self, mime_type: &str) -> Option<Cow<'static, [u8]>>;
45}
46
47impl<T: AsMimeTypes + ?Sized> AsMimeTypes for Box<T> {
48    fn available(&self) -> Cow<'static, [String]> {
49        self.as_ref().available()
50    }
51
52    fn as_bytes(&self, mime_type: &str) -> Option<Cow<'static, [u8]>> {
53        self.as_ref().as_bytes(mime_type)
54    }
55}
56
57/// Data that can be stored to the clipboard.
58pub struct ClipboardStoreData<T>(pub T);
59
60#[derive(Debug, Clone, Copy)]
61pub struct Error;
62
63impl fmt::Display for Error {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "Unsupported mime type")
66    }
67}
68
69impl error::Error for Error {}