Skip to main content

axonyx_ui/
lib.rs

1//! Axonyx UI asset crate.
2//!
3//! This crate is the Cargo-side package for the same Foundry contract that is
4//! published to npm as `@axonyx/ui`. Build tools can depend on this crate to
5//! copy CSS, JavaScript helpers, and Axonyx-native `.ax` components without
6//! shelling out to npm or cloning the UI repository.
7
8/// An embedded Axonyx UI source asset.
9#[derive(Debug, Clone, Copy, Eq, PartialEq)]
10pub struct Asset {
11    /// Package-relative path without the leading `src/`.
12    ///
13    /// Examples: `css/index.css`, `foundry/Button.ax`, `js/dialog.js`.
14    pub path: &'static str,
15    /// UTF-8 asset contents.
16    pub contents: &'static str,
17}
18
19include!(concat!(env!("OUT_DIR"), "/assets.rs"));
20
21/// Returns all Foundry CSS assets.
22pub fn css_assets() -> &'static [Asset] {
23    CSS_ASSETS
24}
25
26/// Returns optional JavaScript helpers used by interactive Foundry primitives.
27pub fn js_assets() -> &'static [Asset] {
28    JS_ASSETS
29}
30
31/// Returns Axonyx-native Foundry component assets.
32pub fn foundry_assets() -> &'static [Asset] {
33    FOUNDRY_ASSETS
34}
35
36/// Iterates through every embedded asset in a stable package order.
37pub fn all_assets() -> impl Iterator<Item = &'static Asset> {
38    CSS_ASSETS.iter().chain(JS_ASSETS).chain(FOUNDRY_ASSETS)
39}
40
41/// Finds an embedded asset by package-relative path.
42pub fn asset(path: &str) -> Option<&'static Asset> {
43    all_assets().find(|asset| asset.path == path)
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn exposes_core_css_asset() {
52        let index = asset("css/index.css").expect("index css should be embedded");
53        assert!(index.contents.contains("tokens.css"));
54    }
55
56    #[test]
57    fn exposes_foundry_component_asset() {
58        let button = asset("foundry/Button.ax").expect("button component should be embedded");
59        assert!(button.contents.contains("page Button"));
60    }
61}