ltrait_source_desktop/
lib.rs

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