Expand description
§kick-rs-assets
Typed asset manifest + compile-time embedding for kick-rs.
Two primitives for shipping static assets with a kick-rs app:
AssetManifest— load a flat{key → hashed-filename}JSON manifest (the kind webpack / vite / esbuild emit), then resolve logical keys to cache-busted URLs.embed_assets!— a re-export of theinclude_dir!macro that bundles a directory tree into the binary at compile time. Gated on the defaultembedfeature.
HTTP serving (responding to GET /static/... with the right
content-type, cache headers, etc.) lives in kick-rs-http’s
AssetsPlugin so this crate stays free of axum.
§Manifest
use kick_rs_assets::AssetManifest;
let m = AssetManifest::load("dist/.vite/manifest.json")?
.with_url_prefix("/static");
let url = m.resolve("app.js")?;
// "/static/app.a1b2c3.js"Two JSON shapes are accepted out of the box.
Flat — the lowest-common-denominator format:
{
"app.js": "app.a1b2c3.js",
"app.css": "app.d4e5f6.css"
}Vite’s full manifest — what vite build emits when
build.manifest = true. Use AssetManifest::from_vite_json(...):
{
"src/main.js": {
"file": "assets/main.4889e940.js",
"src": "src/main.js",
"isEntry": true,
"imports": ["_shared.83069a53.js"],
"css": ["assets/main.b82dbe22.css"]
}
}We reduce each entry to its file field; resolve("src/main.js")
returns the hashed JS URL. CSS / imports / dynamic assets aren’t
surfaced separately yet — coming in a follow-up.
Errors are KickError-typed: RK_C_IO (read failure), RK_C_PARSE
(bad JSON), RK_C_UNKNOWN_ASSET (key not in manifest, hint includes
the catalog).
§Embedded assets (default-on)
use kick_rs_assets::{embed_assets, EmbeddedAssets, content_type_for};
static ASSETS: EmbeddedAssets = embed_assets!("$CARGO_MANIFEST_DIR/dist");
if let Some(file) = ASSETS.get_file("app.a1b2c3.js") {
let mime = content_type_for("app.a1b2c3.js");
// application/javascript; charset=utf-8
serve(mime, file.contents());
}Disable with default-features = false if you only need the
manifest loader without the proc-macro compile cost.
The embed_assets! macro emits paths through kick_rs_assets itself
(via proc-macro-crate resolution), so adopters need only
kick-rs-assets in their Cargo.toml — no include_dir or any
other vendored dep.
§Install
[dependencies]
kick-rs-assets = "0.1.0"
# or via the umbrella crate with the `assets` feature:
kick-rs = { version = "0.1.0", features = ["assets"] }§License
MIT — see the workspace root.
Macros§
- embed_
assets - Embed a directory tree into the binary at compile time.
Structs§
- Asset
Manifest - Map of logical asset keys (
"app.js") to their hashed filenames ("app.a1b2c3.js"), plus an optional URL prefix prepended at resolve time. - Embedded
Assets - One bundled directory. Static — every field lives in
'staticmemory; iteration is cheap and allocation-free. - Embedded
File - One bundled file.
Enums§
- Embedded
Entry - Entry in an embedded tree — either a file or a sub-directory.
Functions§
- content_
type_ for - Best-effort content-type guess from a file extension. Returns
application/octet-streamfor unknown extensions so the caller always has something safe to send. - read_
embedded - Read a file from the embedded tree as a
&[u8]. Errors withRK_C_UNKNOWN_ASSETif the path isn’t bundled.