mime/platform/
linux.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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.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::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)?,
        ))
    }
}