1/// List of allowed mimes.
2pub static ALLOWED_MIME_TYPES: [&str; 3] =
3 ["text/plain;charset=utf-8", "UTF8_STRING", "text/plain"];
4
5/// Mime type supported by clipboard.
6#[derive(Clone, Copy, Eq, PartialEq, Debug)]
7pub enum MimeType {
8 /// text/plain;charset=utf-8 mime type.
9 ///
10 /// The primary mime type used by most clients
11 TextPlainUtf8 = 0,
12 /// UTF8_STRING mime type.
13 ///
14 /// Some X11 clients are using only this mime type, so we
15 /// should have it as a fallback just in case.
16 Utf8String = 1,
17 /// text/plain mime type.
18 ///
19 /// Fallback without charset parameter.
20 TextPlain = 2,
21}
22
23impl MimeType {
24 /// Find first allowed mime type among the `offered_mime_types`.
25 ///
26 /// `find_allowed()` searches for mime type clipboard supports, if we have a
27 /// match, returns `Some(MimeType)`, otherwise `None`.
28 pub fn find_allowed(offered_mime_types: &[String]) -> Option<Self> {
29 let mut fallback: Option = None;
30 for offered_mime_type: &String in offered_mime_types.iter() {
31 if offered_mime_type == ALLOWED_MIME_TYPES[Self::TextPlainUtf8 as usize] {
32 return Some(Self::TextPlainUtf8);
33 } else if offered_mime_type == ALLOWED_MIME_TYPES[Self::Utf8String as usize] {
34 return Some(Self::Utf8String);
35 } else if offered_mime_type == ALLOWED_MIME_TYPES[Self::TextPlain as usize] {
36 // Only use this mime type as a fallback.
37 fallback = Some(Self::TextPlain);
38 }
39 }
40
41 fallback
42 }
43}
44
45impl std::fmt::Display for MimeType {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(f, "{}", ALLOWED_MIME_TYPES[*self as usize])
48 }
49}
50
51/// Normalize CR and CRLF into LF.
52///
53/// 'text' mime types require CRLF line ending according to
54/// RFC-2046, however the platform line terminator and what applications
55/// expect is LF.
56pub fn normalize_to_lf(text: String) -> String {
57 text.replace("\r\n", "\n").replace(from:'\r', to:"\n")
58}
59