Skip to main content

gpui_component_assets/
lib.rs

1use anyhow::anyhow;
2use gpui::{AssetSource, Result, SharedString};
3use rust_embed::RustEmbed;
4use std::borrow::Cow;
5
6/// Embed application assets for GPUI Component.
7///
8/// This assets provides icons svg files for [IconName](https://docs.rs/gpui-component/latest/gpui_component/enum.IconName.html).
9///
10/// ```
11/// use gpui::*;
12/// use gpui_component_assets::Assets;
13///
14/// let app = Application::new().with_assets(Assets);
15/// ```
16#[derive(RustEmbed)]
17#[folder = "assets"]
18#[include = "icons/**/*.svg"]
19pub struct Assets;
20
21impl AssetSource for Assets {
22    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
23        if path.is_empty() {
24            return Ok(None);
25        }
26
27        Self::get(path)
28            .map(|f| Some(f.data))
29            .ok_or_else(|| anyhow!("could not find asset at path \"{path}\""))
30    }
31
32    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
33        Ok(Self::iter()
34            .filter_map(|p| p.starts_with(path).then(|| p.into()))
35            .collect())
36    }
37}