diff --git a/Cargo.lock b/Cargo.lock index 6913ba5..705ffa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6947,7 +6947,7 @@ dependencies = [ [[package]] name = "wl-clipboard-rs" version = "0.8.1" -source = "git+https://github.com/wiiznokes/wl-clipboard-rs.git?branch=watch#10d95f5d5e22d062e51782d4270291295216c4e7" +source = "git+https://github.com/wiiznokes/wl-clipboard-rs.git?branch=watch#2c10217b78dee4fe6435f29e263450da5398eb8e" dependencies = [ "libc", "log", diff --git a/src/db/mod.rs b/src/db/mod.rs index 0e5d726..02359f5 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -21,7 +21,7 @@ pub type MimeDataMap = HashMap>; pub enum Content<'a> { Text(&'a str), - Image(&'a Vec), + Image(&'a [u8]), UriList(Vec<&'a str>), } @@ -35,6 +35,8 @@ impl Debug for Content<'_> { } } +const PREFERRED_MIME_TYPES: &[&str] = &["text/plain"]; + pub trait EntryTrait: Debug + Clone + Send { fn is_favorite(&self) -> bool; @@ -49,9 +51,8 @@ pub trait EntryTrait: Debug + Clone + Send { self.raw_content().iter().next().unwrap().1 } - // todo: prioritize certain mime types fn viewable_content(&self) -> Result> { - for (mime, content) in self.raw_content() { + fn try_get_content<'a>(mime: &str, content: &'a [u8]) -> Result>> { if mime == "text/uri-list" { let text = core::str::from_utf8(content)?; @@ -60,15 +61,39 @@ pub trait EntryTrait: Debug + Clone + Send { .filter(|l| !l.is_empty() && !l.starts_with('#')) .collect(); - return Ok(Content::UriList(uris)); + return Ok(Some(Content::UriList(uris))); } if mime.starts_with("text/") { - return Ok(Content::Text(core::str::from_utf8(content)?)); + return Ok(Some(Content::Text(core::str::from_utf8(content)?))); } if mime.starts_with("image/") { - return Ok(Content::Image(content)); + return Ok(Some(Content::Image(content))); + } + + Ok(None) + } + + for pref_mime in PREFERRED_MIME_TYPES { + if let Some(content) = self.raw_content().get(*pref_mime) { + match try_get_content(pref_mime, content) { + Ok(Some(content)) => return Ok(content), + Ok(None) => error!("unsupported mime type {}", pref_mime), + Err(e) => { + error!("{e}"); + } + } + } + } + + for (mime, content) in self.raw_content() { + match try_get_content(mime, content) { + Ok(Some(content)) => return Ok(content), + Ok(None) => {} + Err(e) => { + error!("{e}"); + } } }