woocraft 0.4.5

GPUI components lib for Woocraft design system.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::{borrow::Cow, collections::HashSet, marker::PhantomData, sync::Arc};

use anyhow::Context;
use gpui::{AssetSource, Result, SharedString};
use rust_embed::RustEmbed;

use crate::IconNamed;

pub const BUILTIN_ASSET_PREFIX: &str = "tech.woooo.woocraft/assets";

const FONT_ASSET_PREFIX: &str = "fonts/";
const ZSTD_EXTENSION: &str = ".zst";

/// Embedded application assets for woocraft.
#[derive(RustEmbed)]
#[folder = "src/assets"]
#[include = "icons/**/*.svg"]
#[include = "fonts/**/*.ttf.zst"]
#[include = "fonts/**/*.otf.zst"]
pub struct Assets;

impl AssetSource for Assets {
  fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
    load_builtin_asset(path)
  }

  fn list(&self, path: &str) -> Result<Vec<SharedString>> {
    Ok(list_builtin_assets(path))
  }
}

/// Adapter that turns any [`RustEmbed`] type into a [`gpui::AssetSource`].
#[derive(Default, Clone, Copy)]
pub struct EmbeddedSource<T>(PhantomData<T>);

impl<T> EmbeddedSource<T> {
  pub const fn new() -> Self {
    Self(PhantomData)
  }
}

impl<T> AssetSource for EmbeddedSource<T>
where
  T: RustEmbed + Send + Sync + 'static,
{
  fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
    load_embedded_asset::<T>(path)
  }

  fn list(&self, path: &str) -> Result<Vec<SharedString>> {
    Ok(list_embedded_assets::<T>(path))
  }
}

/// A source that queries multiple asset sources in order.
///
/// The first source returning a value wins when loading assets.
#[derive(Default, Clone)]
pub struct CombinedSource {
  sources: Vec<Arc<dyn AssetSource>>,
}

impl CombinedSource {
  pub fn new() -> Self {
    Self::default()
  }

  pub fn with(mut self, source: impl AssetSource) -> Self {
    self.push(source);
    self
  }

  pub fn with_shared(mut self, source: Arc<dyn AssetSource>) -> Self {
    self.push_shared(source);
    self
  }

  pub fn push(&mut self, source: impl AssetSource) -> &mut Self {
    self.sources.push(Arc::new(source));
    self
  }

  pub fn push_shared(&mut self, source: Arc<dyn AssetSource>) -> &mut Self {
    self.sources.push(source);
    self
  }

  pub fn is_empty(&self) -> bool {
    self.sources.is_empty()
  }
}

impl AssetSource for CombinedSource {
  fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
    for source in &self.sources {
      if let Some(bytes) = source.load(path)? {
        return Ok(Some(bytes));
      }
    }
    Ok(None)
  }

  fn list(&self, path: &str) -> Result<Vec<SharedString>> {
    let mut seen = HashSet::<String>::new();
    let mut merged = Vec::new();

    for source in &self.sources {
      for asset_path in source.list(path)? {
        if seen.insert(asset_path.to_string()) {
          merged.push(asset_path);
        }
      }
    }

    Ok(merged)
  }
}

fn is_font_asset(path: &str) -> bool {
  path.starts_with(FONT_ASSET_PREFIX)
}

fn is_compressed_font_asset(path: &str) -> bool {
  is_font_asset(path) && path.ends_with(ZSTD_EXTENSION)
}

fn compressed_font_asset_path(path: &str) -> Option<String> {
  if is_font_asset(path) && !path.ends_with('/') && !path.ends_with(ZSTD_EXTENSION) {
    Some(format!("{path}{ZSTD_EXTENSION}"))
  } else {
    None
  }
}

fn visible_asset_path(path: &str) -> &str {
  if is_compressed_font_asset(path) {
    path
      .strip_suffix(ZSTD_EXTENSION)
      .expect("compressed font asset should end with .zst")
  } else {
    path
  }
}

fn decode_embedded_asset(path: &str, data: Cow<'static, [u8]>) -> Result<Cow<'static, [u8]>> {
  if !is_compressed_font_asset(path) {
    return Ok(data);
  }

  let decompressed = zstd::stream::decode_all(data.as_ref())
    .with_context(|| format!("failed to decompress embedded font asset: {path}"))?;

  Ok(Cow::Owned(decompressed))
}

fn embedded_asset_exists<T: RustEmbed>(path: &str) -> bool {
  T::get(path).is_some()
    || compressed_font_asset_path(path)
      .is_some_and(|compressed_path| T::get(compressed_path.as_str()).is_some())
}

fn load_embedded_asset<T: RustEmbed>(path: &str) -> Result<Option<Cow<'static, [u8]>>> {
  if path.is_empty() {
    return Ok(None);
  }

  if let Some(file) = T::get(path) {
    return decode_embedded_asset(path, file.data).map(Some);
  }

  let Some(compressed_path) = compressed_font_asset_path(path) else {
    return Ok(None);
  };

  T::get(compressed_path.as_str())
    .map(|file| decode_embedded_asset(compressed_path.as_str(), file.data))
    .transpose()
}

fn list_embedded_assets<T: RustEmbed>(path: &str) -> Vec<SharedString> {
  let mut seen = HashSet::<String>::new();

  T::iter()
    .filter_map(|asset_path| {
      let asset_path = asset_path.as_ref();
      let visible_path = visible_asset_path(asset_path);

      (asset_path.starts_with(path) || visible_path.starts_with(path))
        .then(|| visible_path.to_owned())
    })
    .filter_map(|asset_path| seen.insert(asset_path.clone()).then(|| asset_path.into()))
    .collect()
}

fn strip_builtin_asset_prefix(path: &str) -> Option<&str> {
  if path == BUILTIN_ASSET_PREFIX {
    Some("")
  } else {
    path
      .strip_prefix(BUILTIN_ASSET_PREFIX)
      .and_then(|rest| rest.strip_prefix('/'))
  }
}

fn prefix_builtin_asset_path(path: &str) -> SharedString {
  if path.is_empty() {
    BUILTIN_ASSET_PREFIX.into()
  } else {
    format!("{BUILTIN_ASSET_PREFIX}/{path}").into()
  }
}

fn load_builtin_asset(path: &str) -> Result<Option<Cow<'static, [u8]>>> {
  let Some(internal_path) = strip_builtin_asset_prefix(path) else {
    return Ok(None);
  };

  if internal_path.is_empty() {
    return Ok(None);
  }

  load_embedded_asset::<Assets>(internal_path)
}

fn list_builtin_assets(path: &str) -> Vec<SharedString> {
  let internal_prefix = if path.is_empty() {
    ""
  } else if let Some(internal_prefix) = strip_builtin_asset_prefix(path) {
    internal_prefix
  } else {
    return Vec::new();
  };

  list_embedded_assets::<Assets>(internal_prefix)
    .into_iter()
    .map(|path| prefix_builtin_asset_path(path.as_ref()))
    .collect()
}

pub fn has_asset(path: &str) -> bool {
  strip_builtin_asset_prefix(path)
    .filter(|internal_path| !internal_path.is_empty())
    .is_some_and(embedded_asset_exists::<Assets>)
}

pub fn list_assets(path_prefix: &str) -> Vec<SharedString> {
  list_builtin_assets(path_prefix)
}

pub fn has_icon(icon: crate::IconName) -> bool {
  has_asset(icon.path().as_ref())
}

pub fn list_icons() -> Vec<crate::IconName> {
  crate::IconName::all()
    .iter()
    .copied()
    .filter(|icon| has_icon(*icon))
    .collect()
}

pub fn register_fonts(text_system: &gpui::TextSystem) -> Result<()> {
  let mut fonts = Vec::new();

  for path in Assets::iter().filter(|path| is_font_asset(path.as_ref())) {
    if let Some(font) = load_embedded_asset::<Assets>(path.as_ref())? {
      fonts.push(font);
    }
  }

  text_system.add_fonts(fonts)?;

  Ok(())
}

#[cfg(test)]
mod tests {
  use std::{borrow::Cow, collections::HashMap};

  use gpui::{AssetSource, Result, SharedString};

  use crate::IconNamed;

  #[cfg(feature = "resources")]
  #[test]
  fn all_registered_icons_exist_in_assets() {
    for icon in crate::IconName::all() {
      assert!(
        super::has_icon(*icon),
        "missing icon asset: {}",
        (*icon).path()
      );
    }
  }

  #[derive(Default)]
  struct StaticSource {
    files: HashMap<&'static str, &'static [u8]>,
  }

  impl StaticSource {
    fn with_file(mut self, path: &'static str, data: &'static [u8]) -> Self {
      self.files.insert(path, data);
      self
    }
  }

  impl AssetSource for StaticSource {
    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
      Ok(self.files.get(path).map(|bytes| Cow::Borrowed(*bytes)))
    }

    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
      Ok(
        self
          .files
          .keys()
          .filter(|&asset_path| asset_path.starts_with(path))
          .map(|asset_path| (*asset_path).into())
          .collect(),
      )
    }
  }

  #[test]
  fn combined_source_loads_from_fallback_sources() {
    let source = super::CombinedSource::new()
      .with(StaticSource::default().with_file("icons/a.svg", b"a"))
      .with(StaticSource::default().with_file("icons/b.svg", b"b"));

    let bytes = source
      .load("icons/b.svg")
      .expect("combined source should not fail")
      .expect("asset should be loaded from fallback source");
    assert_eq!(bytes.as_ref(), b"b");
  }

  #[test]
  fn combined_source_lists_without_duplicates() {
    let source = super::CombinedSource::new()
      .with(
        StaticSource::default()
          .with_file("icons/a.svg", b"a")
          .with_file("icons/b.svg", b"b"),
      )
      .with(
        StaticSource::default()
          .with_file("icons/b.svg", b"b")
          .with_file("icons/c.svg", b"c"),
      );

    let listed = source.list("icons/").expect("list should succeed");
    let mut listed = listed.iter().map(|path| path.as_ref()).collect::<Vec<_>>();
    listed.sort();

    assert_eq!(listed, vec!["icons/a.svg", "icons/b.svg", "icons/c.svg"]);
  }

  #[cfg(feature = "resources")]
  #[test]
  fn builtin_assets_are_namespaced() {
    let icon = crate::IconName::all()[0];
    let prefixed_path = icon.path();

    assert!(prefixed_path.starts_with(super::BUILTIN_ASSET_PREFIX));
    assert!(super::has_asset(prefixed_path.as_ref()));

    let unprefixed_path = prefixed_path
      .as_ref()
      .strip_prefix(&(super::BUILTIN_ASSET_PREFIX.to_owned() + "/"))
      .expect("icon path should include built-in prefix");
    assert!(!super::has_asset(unprefixed_path));
  }

  #[cfg(feature = "resources")]
  #[test]
  fn builtin_fonts_are_listed_without_compression_suffix() {
    let font_assets = super::list_assets(super::BUILTIN_ASSET_PREFIX)
      .into_iter()
      .filter(|path| path.as_ref().contains("/fonts/"))
      .collect::<Vec<_>>();

    assert!(
      font_assets
        .iter()
        .any(|path| path.as_ref().ends_with("fonts/default-font.ttf")),
      "compressed font should be exposed via its original extension"
    );
    assert!(
      font_assets
        .iter()
        .all(|path| !path.as_ref().ends_with(".zst")),
      "compression suffix should stay internal to asset loading"
    );
    assert!(super::has_asset(&format!(
      "{}/fonts/default-font.ttf",
      super::BUILTIN_ASSET_PREFIX
    )));
  }

  #[cfg(feature = "resources")]
  #[test]
  fn builtin_fonts_are_decompressed_on_load() {
    let raw_font = super::Assets::get("fonts/default-font.ttf.zst")
      .expect("compressed font should be embedded in test builds");

    assert!(
      raw_font.data.as_ref().starts_with(b"\x28\xb5\x2f\xfd"),
      "embedded font should be stored in zstd format"
    );

    let font_path = format!("{}/fonts/default-font.ttf", super::BUILTIN_ASSET_PREFIX);
    let loaded_font = super::load_builtin_asset(&font_path)
      .expect("font load should succeed")
      .expect("font asset should exist");

    assert_ne!(loaded_font.as_ref(), raw_font.data.as_ref());
    assert!(
      !loaded_font.as_ref().starts_with(b"\x28\xb5\x2f\xfd"),
      "font bytes should be decompressed before registration"
    );
  }
}