assets.rs

 1use anyhow::anyhow;
 2use gpui2::{AssetSource, Result, SharedString};
 3use rust_embed::RustEmbed;
 4
 5#[derive(RustEmbed)]
 6#[folder = "../../assets"]
 7#[include = "fonts/**/*"]
 8#[include = "icons/**/*"]
 9#[include = "themes/**/*"]
10#[include = "sounds/**/*"]
11#[include = "*.md"]
12#[exclude = "*.DS_Store"]
13pub struct Assets;
14
15impl AssetSource for Assets {
16    fn load(&self, path: &str) -> Result<std::borrow::Cow<[u8]>> {
17        Self::get(path)
18            .map(|f| f.data)
19            .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
20    }
21
22    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
23        Ok(Self::iter()
24            .filter_map(|p| {
25                if p.starts_with(path) {
26                    Some(p.into())
27                } else {
28                    None
29                }
30            })
31            .collect())
32    }
33}