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
16#[derive(Debug, Clone)]
17pub struct DesktopEntry {
18    pub entry: freedesktop_desktop_entry::DesktopEntry,
19}
20
21impl DesktopEntry {
22    pub fn icon(&self) -> Option<String> {
23        self.entry.icon().and_then(|n| icon::lookup(n).ok())
24    }
25}
26
27// 楽をするためにfreedesktop_desktop_entryを使っているからStreamではなく、性能を最大限に活かしきれていない
28pub fn new<'a>() -> Result<Source<'a, DesktopEntry>, DesktopError> {
29    use freedesktop_desktop_entry::{Iter, default_paths};
30    let entries = Iter::new(default_paths())
31        .entries::<String>(None)
32        .map(|e| DesktopEntry { entry: e });
33
34    Ok(Box::pin(tokio_stream::iter(entries)))
35}