Skip to main content

Crate mbed

Crate mbed 

Source
Expand description

§mbed

Embed and transform assets into your Rust crate.

mbed provides macros for turning files and generated outputs into Artifact values. Artifacts carry their bytes, MIME type, and Id, and can be grouped into a Manifest for lookup by identifier.

§Basic usage

Requires the macros feature.

pub const LOGO: Artifact<[u8]> = mbed::include_bytes!["../assets/logo.svg"];
pub const INDEX: Artifact<str> = mbed::include_str!["../pages/index.html"];

let id = LOGO.id();
assert_eq!(LOGO.mime(), "image/svg+xml");

§Collections and manifests

collect! groups artifacts inside a module. manifest! can then reference individual artifacts or whole collections with ::*.

Requires the macros feature.

pub mod pages {
    pub const INDEX: Artifact<str> = mbed::include_str!["../pages/index.html"];
    pub const ABOUT: Artifact<str> = mbed::include_str!["../pages/about.html"];

    mbed::collect![INDEX, ABOUT];
}

pub mod images {
    pub const LOGO: Artifact<[u8]> = mbed::include_bytes!["../assets/logo.svg"];

    mbed::collect![LOGO];
}

pub const ASSETS: Manifest = mbed::manifest![pages::*, images::*];

let artifact = ASSETS.get(pages::INDEX.id()).unwrap();
assert_eq!(artifact.mime(), pages::INDEX.mime());

§Images

Requires the image feature.

pub const LOGO: Artifact<Image> = mbed::image::include! {
    resize: Scale(0.5),
    path: "../assets/logo.png",
    format: WebP,
};

assert_eq!(LOGO.format(), mbed::image::Format::WebP);
let bytes = LOGO.as_bytes();

§JavaScript bundles

Requires the bundle feature.

pub const APP: Artifact<Bundle> = mbed::js::bundle!["../frontend/app.js"];

let code = APP.code();
let sourcemap = APP.sourcemap();

§Tailwind CSS

Requires the tailwindcss feature.

pub const STYLES: Artifact<str> = mbed::css::tailwindcss!("../tailwind.css");

§Feature flags

Modules§

csscss
CSS asset support.
imageimage
Image asset support.
jsjs
JavaScript asset support.

Macros§

collectmacros
Groups artifacts into a module-local collection.
include_bytesmacros
Embeds a UTF-8 file as an Artifact<str>.
include_strmacros
Embeds a file as an [Artifact<[u8]>].
manifestmacros
Builds a Manifest from artifacts and collections.

Structs§

Artifact
An embedded asset and its metadata.
Id
A SHA-224 artifact identifier.
Iter
An iterator over embedded artifacts.
Manifest
A collection of embedded artifacts indexed by Id.