ltrait_source_desktop/
lib.rs

1use ltrait::{source::Source, tokio_stream};
2
3pub mod icon;
4
5#[derive(Debug, thiserror::Error)]
6pub enum DesktopError {
7    // Stringは名前
8    #[error("Failed to find icon of {0}")]
9    NoIcon(String),
10    #[error("Failed to open file: {0}")]
11    OpenFile(#[source] std::io::Error),
12    #[error("Failed to decode the image: {0}")]
13    ImageDecode(#[source] image::ImageError),
14}
15
16pub struct DesktopEntry {
17    pub entry: freedesktop_desktop_entry::DesktopEntry,
18
19    /// base64 encoded icon of the entry
20    pub icon: Option<String>,
21}
22
23// 楽をするためにfreedesktop_desktop_entryを使っているからStreamではなく、性能を最大限に活かしきれていない
24pub fn new<'a>() -> Result<Source<'a, DesktopEntry>, DesktopError> {
25    use freedesktop_desktop_entry::{Iter, default_paths};
26    let entries = Iter::new(default_paths()).entries::<String>(None).map(|e| {
27        let icon = e.icon().and_then(|n| icon::lookup(n).ok());
28        DesktopEntry { entry: e, icon }
29    });
30
31    Ok(Box::pin(tokio_stream::iter(entries)))
32}