Restore manganis optimizations (#3195)

* Create a crate for constant serialization of config structs for manganis
* use SerializeConst for the image asset builder
* switch to a declarative macro for assets
* add serializable options for each asset type
* implement the new manganis macro
* optimize assets in the CLI
* Fix assets with dioxus formattable
* reduce fuzzing test limits
* use the new syntax in examples
* fix avif support
* Fix manganis doc tests
* cache asset optimization
* Split out asset and bundled asset
* make hash pointer width independent
* Fix manganis macro docs
* add constvec::is_empty method to fix clippy lint
* remove nasm feature
* simplify test_rsplit_once test so typos passes
* revert example change from clippy fix
* always accept unix paths
* fix windows path seperator
* fix folder asset hash
* Optimize assets in a blocking task
* Fix asset options docs
* Document Asset and BundledAsset
* simplify the linker macro a bit
* add more docs to AssetParser expansion
* add image format helpers
* Split by  both unix and windows path separators and take the smallest one
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-11-26 18:12:24 +01:00
1 parent 31d72db82d
commit f9617c8d33
64 files changed
+6233 -704

No files matched your search

+9 -1
View File
@@ -13,5 +13,13 @@ keywords = ["dom", "ui", "gui", "assets"]
# be careful with dependencies you add here - these need to get compiled for the proc macro and therefore
# prevent the main code from compiling!
[dependencies]
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
const-serialize = { workspace = true, features = ["serde"] }
dioxus-core-types = { workspace = true, optional = true }
[dev-dependencies]
manganis = { workspace = true }
dioxus = { workspace = true }
[features]
dioxus = ["dep:dioxus-core-types"]
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
+150 -112
View File
@@ -1,118 +1,156 @@
use serde::{Deserialize, Serialize};
use std::{
hash::{Hash, Hasher},
path::PathBuf,
time::SystemTime,
};
use crate::AssetOptions;
use const_serialize::{ConstStr, SerializeConst};
use std::path::PathBuf;
/// The location we'll write to the link section - needs to be serializable
///
/// This basically is 1:1 with `manganis/Asset` but with more metadata to be useful to the macro and cli
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ResourceAsset {
/// The input path `/assets/blah.css`
pub input: PathBuf,
/// The canonicalized asset
///
/// `Users/dioxus/dev/app/assets/blah.css`
pub absolute: PathBuf,
/// The post-bundle name of the asset - do we include the `assets` name?
///
/// `blahcss123.css`
pub bundled: String,
/// An asset that should be copied by the bundler with some options. This type will be
/// serialized into the binary and added to the link section [`LinkSection::CURRENT`](crate::linker::LinkSection::CURRENT).
/// CLIs that support manganis, should pull out the assets from the link section, optimize,
/// and write them to the filesystem at [`BundledAsset::bundled_path`] for the application
/// to use.
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct BundledAsset {
/// The absolute path of the asset
absolute_source_path: ConstStr,
/// The bundled path of the asset
bundled_path: ConstStr,
/// The options for the asset
options: AssetOptions,
}
impl ResourceAsset {
pub fn parse_any(raw: &str) -> Result<Self, AssetParseError> {
// get the location where the asset is absolute, relative to
//
// IE
// /users/dioxus/dev/app/
// is the root of
// /users/dioxus/dev/app/assets/blah.css
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap();
// 1. the input file should be a pathbuf
let input = PathBuf::from(raw);
// 2. absolute path to the asset
let absolute = manifest_dir.join(raw.trim_start_matches('/'));
let absolute =
absolute
.canonicalize()
.map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: absolute,
})?;
// 3. the bundled path is the unique name of the asset
let bundled = Self::make_unique_name(absolute.clone())?;
Ok(Self {
input,
absolute,
bundled,
})
}
fn make_unique_name(file_path: PathBuf) -> Result<String, AssetParseError> {
// Create a hasher
let mut hash = std::collections::hash_map::DefaultHasher::new();
// Open the file to get its options
let file = std::fs::File::open(&file_path).map_err(AssetParseError::FailedToReadAsset)?;
let modified = file
.metadata()
.and_then(|metadata| metadata.modified())
.unwrap_or(SystemTime::UNIX_EPOCH);
// Hash a bunch of metadata
// name, options, modified time, and maybe the version of our crate
// Hash the last time the file was updated and the file source. If either of these change, we need to regenerate the unique name
modified.hash(&mut hash);
file_path.hash(&mut hash);
let uuid = hash.finish();
let file_name = file_path
.file_stem()
.expect("file_path should have a file_stem")
.to_string_lossy();
let extension = file_path
.extension()
.map(|f| f.to_string_lossy())
.unwrap_or_default();
Ok(format!("{file_name}-{uuid:x}.{extension}"))
}
}
#[derive(Debug)]
pub enum AssetParseError {
ParseError(String),
AssetDoesntExist {
err: std::io::Error,
path: std::path::PathBuf,
},
FailedToReadAsset(std::io::Error),
FailedToReadMetadata(std::io::Error),
}
impl std::fmt::Display for AssetParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetParseError::ParseError(err) => write!(f, "Failed to parse asset: {}", err),
AssetParseError::AssetDoesntExist { err, path } => {
write!(f, "Asset at {} doesn't exist: {}", path.display(), err)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
AssetParseError::FailedToReadMetadata(err) => {
write!(f, "Failed to read asset metadata: {}", err)
}
impl BundledAsset {
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset
pub const fn new(
absolute_source_path: &'static str,
bundled_path: &'static str,
options: AssetOptions,
) -> Self {
Self {
absolute_source_path: ConstStr::new(absolute_source_path),
bundled_path: ConstStr::new(bundled_path),
options,
}
}
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset but with a relative path
///
/// This method is deprecated and will be removed in a future release.
#[deprecated(
note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`"
)]
pub const fn new_relative(
absolute_source_path: &'static str,
bundled_path: &'static str,
options: AssetOptions,
) -> Self {
Self::new(absolute_source_path, bundled_path, options)
}
/// Get the bundled name of the asset. This identifier cannot be used to read the asset directly
pub fn bundled_path(&self) -> &str {
self.bundled_path.as_str()
}
/// Get the absolute path of the asset source. This path will not be available when the asset is bundled
pub fn absolute_source_path(&self) -> &str {
self.absolute_source_path.as_str()
}
/// Get the options for the asset
pub const fn options(&self) -> &AssetOptions {
&self.options
}
}
/// A bundled asset with some options. The asset can be used in rsx! to reference the asset.
/// It should not be read directly with [`std::fs::read`] because the path needs to be resolved
/// relative to the bundle
///
/// ```rust
/// # use manganis::{asset, Asset};
/// # use dioxus::prelude::*;
/// const ASSET: Asset = asset!("/assets/image.png");
/// rsx! {
/// img { src: ASSET }
/// };
/// ```
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Asset {
/// The bundled asset
bundled: BundledAsset,
/// The link section for the asset
keep_link_section: fn() -> u8,
}
impl Asset {
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset from the bundled form of the asset and the link section
pub const fn new(bundled: BundledAsset, keep_link_section: fn() -> u8) -> Self {
Self {
bundled,
keep_link_section,
}
}
/// Get the bundled asset
pub const fn bundled(&self) -> &BundledAsset {
&self.bundled
}
/// Return a canonicalized path to the asset
///
/// Attempts to resolve it against an `assets` folder in the current directory.
/// If that doesn't exist, it will resolve against the cargo manifest dir
pub fn resolve(&self) -> PathBuf {
// Force a volatile read of the asset link section to ensure the symbol makes it into the binary
(self.keep_link_section)();
#[cfg(feature = "dioxus")]
// If the asset is relative, we resolve the asset at the current directory
if !dioxus_core_types::is_bundled_app() {
return PathBuf::from(self.bundled.absolute_source_path.as_str());
}
// Otherwise presumably we're bundled and we can use the bundled path
PathBuf::from("/assets/").join(PathBuf::from(
self.bundled.bundled_path.as_str().trim_start_matches('/'),
))
}
}
impl From<Asset> for String {
fn from(value: Asset) -> Self {
value.to_string()
}
}
impl From<Asset> for Option<String> {
fn from(value: Asset) -> Self {
Some(value.to_string())
}
}
impl std::fmt::Display for Asset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.resolve().display())
}
}
#[cfg(feature = "dioxus")]
impl dioxus_core_types::DioxusFormattable for Asset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
@@ -0,0 +1,77 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// Options for a css asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct CssAssetOptions {
minify: bool,
preload: bool,
}
impl Default for CssAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl CssAssetOptions {
/// Create a new css asset using the builder
pub const fn new() -> Self {
Self {
preload: false,
minify: true,
}
}
/// Sets whether the css should be minified (default: true)
///
/// Minifying the css can make your site load faster by loading less data
///
/// ```rust
/// # use manganis::{asset, Asset, CssAssetOptions};
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_minify(false));
/// ```
#[allow(unused)]
pub const fn with_minify(self, minify: bool) -> Self {
Self { minify, ..self }
}
/// Check if the asset is minified
pub const fn minified(&self) -> bool {
self.minify
}
/// Make the asset preloaded
///
/// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, CssAssetOptions};
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_preload(true));
/// ```
#[allow(unused)]
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Css(self)
}
}
@@ -0,0 +1,35 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// The builder for [`FolderAsset`]
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct FolderAssetOptions {}
impl Default for FolderAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl FolderAssetOptions {
/// Create a new folder asset using the builder
pub const fn new() -> Self {
Self {}
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Folder(self)
}
}
@@ -0,0 +1,225 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(u8)]
pub enum ImageFormat {
/// A png image. Png images cannot contain transparency and tend to compress worse than other formats
Png,
/// A jpg image. Jpg images can contain transparency and tend to compress better than png images
Jpg,
/// A webp image. Webp images can contain transparency and tend to compress better than jpg images
Webp,
/// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
Avif,
/// An unknown image type
Unknown,
}
/// The size of an image asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(C, u8)]
pub enum ImageSize {
/// A manual size in pixels
Manual {
/// The width of the image in pixels
width: u32,
/// The height of the image in pixels
height: u32,
},
/// The size will be automatically determined from the image source
Automatic,
}
/// Options for an image asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct ImageAssetOptions {
ty: ImageFormat,
low_quality_preview: bool,
size: ImageSize,
preload: bool,
}
impl Default for ImageAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl ImageAssetOptions {
/// Create a new image asset options
pub const fn new() -> Self {
Self {
ty: ImageFormat::Unknown,
low_quality_preview: false,
size: ImageSize::Automatic,
preload: false,
}
}
/// Make the asset preloaded
///
/// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
/// ```
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Sets the format of the image
///
/// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Webp));
/// ```
pub const fn with_format(self, format: ImageFormat) -> Self {
Self { ty: format, ..self }
}
/// Sets the format of the image to [`ImageFormat::Avif`]
///
/// Avif images tend to be a good default for most images rendered in browser because
/// they compress images well
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_avif());
/// ```
pub const fn with_avif(self) -> Self {
self.with_format(ImageFormat::Avif)
}
/// Sets the format of the image to [`ImageFormat::Webp`]
///
/// Webp images tend to be a good default for most images rendered in browser because
/// they compress images well
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_webp());
/// ```
pub const fn with_webp(self) -> Self {
self.with_format(ImageFormat::Webp)
}
/// Sets the format of the image to [`ImageFormat::Jpg`]
///
/// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`]
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_jpg());
/// ```
pub const fn with_jpg(self) -> Self {
self.with_format(ImageFormat::Jpg)
}
/// Sets the format of the image to [`ImageFormat::Png`]
///
/// Png images don't compress very well, so they are not recommended for large images
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_png());
/// ```
pub const fn with_png(self) -> Self {
self.with_format(ImageFormat::Png)
}
/// Get the format of the image
pub const fn format(&self) -> ImageFormat {
self.ty
}
/// Sets the size of the image
///
/// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 512, height: 512 }));
/// ```
pub const fn with_size(self, size: ImageSize) -> Self {
Self { size, ..self }
}
/// Get the size of the image
pub const fn size(&self) -> ImageSize {
self.size
}
// LQIP is currently disabled until we have the CLI set up to inject the low quality image preview after the crate is built through the linker
// /// Make the image use a low quality preview
// ///
// /// A low quality preview is a small version of the image that will load faster. This is useful for large images on mobile devices that may take longer to load
// ///
// /// ```rust
// /// # use manganis::{asset, Asset, ImageAssetOptions};
// /// const _: Asset = manganis::asset!("/assets/image.png", ImageAssetOptions::new().with_low_quality_image_preview());
// /// ```
//
// pub const fn with_low_quality_image_preview(self, low_quality_preview: bool) -> Self {
// Self {
// low_quality_preview,
// ..self
// }
// }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Image(self)
}
pub(crate) const fn extension(&self) -> Option<&'static str> {
match self.ty {
ImageFormat::Png => Some("png"),
ImageFormat::Jpg => Some("jpg"),
ImageFormat::Webp => Some("webp"),
ImageFormat::Avif => Some("avif"),
ImageFormat::Unknown => None,
}
}
}
+77
View File
@@ -0,0 +1,77 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// Options for a javascript asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct JsAssetOptions {
minify: bool,
preload: bool,
}
impl Default for JsAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl JsAssetOptions {
/// Create a new js asset builder
pub const fn new() -> Self {
Self {
minify: true,
preload: false,
}
}
/// Sets whether the js should be minified (default: true)
///
/// Minifying the js can make your site load faster by loading less data
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_minify(false));
/// ```
#[allow(unused)]
pub const fn with_minify(self, minify: bool) -> Self {
Self { minify, ..self }
}
/// Check if the asset is minified
pub const fn minified(&self) -> bool {
self.minify
}
/// Make the asset preloaded
///
/// Preloading the javascript will make the javascript start to load as soon as possible. This is useful for javascript that will be used soon after the page loads or javascript that may not be used immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_preload(true));
/// ```
#[allow(unused)]
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Js(self)
}
}
+17 -3
View File
@@ -1,5 +1,19 @@
mod asset;
mod linker;
mod folder;
pub use folder::*;
mod images;
pub use images::*;
mod options;
pub use options::*;
mod css;
pub use css::*;
mod js;
pub use js::*;
mod asset;
pub use asset::*;
pub use linker::*;
pub mod linker;
@@ -1,3 +1,5 @@
//! Utilities for working with Manganis assets in the linker. This module defines [`LinkSection`] which has information about what section manganis assets are stored in on each platform.
/// Information about the manganis link section for a given platform
#[derive(Debug, Clone, Copy)]
pub struct LinkSection {
@@ -0,0 +1,48 @@
use const_serialize::SerializeConst;
use crate::{CssAssetOptions, FolderAssetOptions, ImageAssetOptions, JsAssetOptions};
/// Settings for a generic asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(C, u8)]
#[non_exhaustive]
pub enum AssetOptions {
/// An image asset
Image(ImageAssetOptions),
/// A folder asset
Folder(FolderAssetOptions),
/// A css asset
Css(CssAssetOptions),
/// A javascript asset
Js(JsAssetOptions),
/// An unknown asset
Unknown,
}
impl AssetOptions {
/// Try to get the extension for the asset. If the asset options don't define an extension, this will return None
pub const fn extension(&self) -> Option<&'static str> {
match self {
AssetOptions::Image(image) => image.extension(),
AssetOptions::Css(_) => Some("css"),
AssetOptions::Js(_) => Some("js"),
AssetOptions::Folder(_) => None,
AssetOptions::Unknown => None,
}
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> Self {
self
}
}
+3 -2
View File
@@ -18,9 +18,10 @@ proc-macro = true
proc-macro2 = { version = "1.0" }
quote = "1.0"
syn = { version = "2.0", features = ["full", "extra-traits"] }
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
manganis-core = { workspace = true }
[features]
default = []
[dev-dependencies]
manganis.workspace = true
Whitespace-only changes.
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
+155 -51
View File
@@ -1,14 +1,107 @@
use manganis_core::{AssetParseError, ResourceAsset};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use std::{
hash::Hasher,
io::Read,
path::{Path, PathBuf},
};
use syn::{
parse::{Parse, ParseStream},
LitStr,
LitStr, Token,
};
fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
// Get the location of the root of the crate which is where all assets are relative to
//
// IE
// /users/dioxus/dev/app/
// is the root of
// /users/dioxus/dev/app/assets/blah.css
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap();
// 1. the input file should be a pathbuf
let input = PathBuf::from(raw);
// 2. absolute path to the asset
manifest_dir
.join(raw.trim_start_matches('/'))
.canonicalize()
.map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: input.clone(),
})
}
fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {
// Create a hasher
let mut hash = std::collections::hash_map::DefaultHasher::new();
// If this is a folder, hash the folder contents
if file_path.is_dir() {
let files =
std::fs::read_dir(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
for file in files.flatten() {
let path = file.path();
hash_file_contents(&path)?;
}
return Ok(hash.finish());
}
// Otherwise, open the file to get its contents
let mut file =
std::fs::File::open(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
// We add a hash to the end of the file so it is invalidated when the bundled version of the file changes
// The hash includes the file contents, the options, and the version of manganis. From the macro, we just
// know the file contents, so we only include that hash
let mut buffer = [0; 8192];
loop {
let read = file
.read(&mut buffer)
.map_err(AssetParseError::FailedToReadAsset)?;
if read == 0 {
break;
}
hash.write(&buffer[..read]);
}
Ok(hash.finish())
}
#[derive(Debug)]
pub(crate) enum AssetParseError {
AssetDoesntExist {
err: std::io::Error,
path: std::path::PathBuf,
},
FailedToReadAsset(std::io::Error),
}
impl std::fmt::Display for AssetParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetParseError::AssetDoesntExist { err, path } => {
write!(f, "Asset at {} doesn't exist: {}", path.display(), err)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
}
}
}
pub struct AssetParser {
/// The span of the source string
path_span: proc_macro2::Span,
/// The asset itself
asset: Result<ResourceAsset, AssetParseError>,
asset: Result<PathBuf, AssetParseError>,
/// The source of the trailing options
options: TokenStream2,
@@ -26,8 +119,8 @@ impl Parse for AssetParser {
// ```
// asset!(
// "/assets/myfile.png",
// asset::image()
// .format(ImageType::Jpg)
// ImageAssetOptions::new()
// .format(ImageFormat::Jpg)
// .size(512, 512)
// )
// ```
@@ -36,29 +129,27 @@ impl Parse for AssetParser {
fn parse(input: ParseStream) -> syn::Result<Self> {
// And then parse the options
let src = input.parse::<LitStr>()?;
let asset = ResourceAsset::parse_any(&src.value());
let path_span = src.span();
let asset = resolve_path(&src.value());
let _comma = input.parse::<Token![,]>();
let options = input.parse()?;
Ok(Self { asset, options })
Ok(Self {
path_span,
asset,
options,
})
}
}
impl ToTokens for AssetParser {
// Need to generate:
//
// - 1. absolute file path on the user's system: `/users/dioxus/dev/project/assets/blah.css`
// - 2. original input in case that's useful: `../blah.css`
// - 3. path relative to the CARGO_MANIFEST_DIR - and then we'll add a `/`: `/assets/blah.css
// - 4. file from which this macro was called: `/users/dioxus/dev/project/src/lib.rs`
// - 5: The link section containing all this data
// - 6: the input tokens such that the builder gets validated by the const code
// - 7: the bundled name `/blahcss123.css`
//
// Not that we'll use everything, but at least we have this metadata for more post-processing.
//
// For now, `2` and `3` will be the same since we don't support relative paths... a bit of
// a limitation from rust itself. We technically could support them but not without some hoops
// to jump through
// The manganis macro outputs info to two different places:
// 1) The crate the macro was invoked in
// - It needs the hashed contents of the file, the file path, and the file options
// - Most of this is just forwarding the input, the only thing that the macro needs to do is hash the file contents
// 2) A bundler that supports manganis (currently just dioxus-cli)
// - The macro needs to output the absolute path to the asset for the bundler to find later
// - It also needs to serialize the bundled asset along with the asset options for the bundler to use later
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let asset = match self.asset.as_ref() {
Ok(asset) => asset,
@@ -68,47 +159,60 @@ impl ToTokens for AssetParser {
return;
}
};
let asset_str = asset.display().to_string();
let mut asset_str = proc_macro2::Literal::string(&asset_str);
asset_str.set_span(self.path_span);
// 1. the link section itself
let link_section = crate::generate_link_section(&asset);
let hash = match hash_file_contents(asset) {
Ok(hash) => hash,
Err(err) => {
let err = err.to_string();
tokens.append_all(quote! { compile_error!(#err) });
return;
}
};
// 2. original
let input = asset.input.display().to_string();
// 3. resolved on the user's system
let local = asset.absolute.display().to_string();
// 4. bundled
let bundled = asset.bundled.to_string();
// 5. source tokens
let option_source = &self.options;
// Generate the link section for the asset
// The link section includes the source path and the output path of the asset
let link_section = crate::generate_link_section(quote!(__ASSET));
// generate the asset::new method to deprecate the `./assets/blah.css` syntax
let method = if !asset.input.starts_with("/") {
let constructor = if asset.is_relative() {
quote::quote! { new_relative }
} else {
quote::quote! { new }
};
let options = if self.options.is_empty() {
quote! { manganis::AssetOptions::Unknown }
} else {
self.options.clone()
};
tokens.extend(quote! {
Asset::#method(
{
#link_section
manganis::Asset {
// "/assets/blah.css"
input: #input,
{
// We keep a hash of the contents of the asset for cache busting
const __ASSET_HASH: u64 = #hash;
// The source is used by the CLI to copy the asset
const __ASSET_SOURCE_PATH: &'static str = #asset_str;
// The options give the CLI info about how to process the asset
// Note: into_asset_options is not a trait, so we cannot accept the options directly
// in the constructor. Stable rust doesn't have support for constant functions in traits
const __ASSET_OPTIONS: manganis::AssetOptions = #options.into_asset_options();
// We calculate the bundled path from the hash and any transformations done by the options
// This is the final path that the asset will be written to
const __ASSET_BUNDLED_PATH: manganis::macro_helpers::const_serialize::ConstStr = manganis::macro_helpers::generate_unique_path(__ASSET_SOURCE_PATH, __ASSET_HASH, &__ASSET_OPTIONS);
// Get the reference to the string that was generated. We cannot return &'static str from
// generate_unique_path because it would return a reference to data generated in the function
const __ASSET_BUNDLED_PATH_STR: &'static str = __ASSET_BUNDLED_PATH.as_str();
// Create the asset that the crate will use. This is used both in the return value and
// added to the linker for the bundler to copy later
const __ASSET: manganis::BundledAsset = manganis::BundledAsset::#constructor(__ASSET_SOURCE_PATH, __ASSET_BUNDLED_PATH_STR, __ASSET_OPTIONS);
// "/users/dioxus/dev/app/assets/blah.css"
local: #local,
#link_section
// "/blahcss123.css"
bundled: #bundled,
metadata: __volatile_reader as fn() -> u8,
}
}
) #option_source
manganis::Asset::new(__ASSET, __keep_link_section)
}
})
}
}
@@ -11,6 +11,42 @@ pub(crate) mod linker;
use linker::generate_link_section;
/// The asset macro collects assets that will be included in the final binary
///
/// # Files
///
/// The file builder collects an arbitrary file. Relative paths are resolved relative to the package root
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/asset.txt");
/// ```
/// Or you can use URLs to read the asset at build time from a remote location
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/image.png");
/// ```
///
/// # Images
///
/// You can collect images which will be automatically optimized with the image builder:
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/image.png");
/// ```
/// Resize the image at compile time to make the assets file size smaller:
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
/// ```
/// Or convert the image at compile time to a web friendly format:
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
/// ```
/// You can mark images as preloaded to make them load faster in your app
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
/// ```
#[proc_macro]
pub fn asset(input: TokenStream) -> TokenStream {
let asset = parse_macro_input!(input as asset::AssetParser);
+21 -13
View File
@@ -1,25 +1,33 @@
use proc_macro2::TokenStream as TokenStream2;
use serde::Serialize;
use quote::ToTokens;
/// this new approach will store the assets descriptions *inside the executable*.
/// The trick is to use the `link_section` attribute.
/// We force rust to store a json representation of the asset description
/// We store description of the assets an application uses in the executable.
/// We use the `link_section` attribute embed an extra section in the executable.
/// We force rust to store a serialized representation of the asset description
/// inside a particular region of the binary, with the label "manganis".
/// After linking, the "manganis" sections of the different executables will be merged.
pub fn generate_link_section(asset: &impl Serialize) -> TokenStream2 {
/// After linking, the "manganis" sections of the different object files will be merged.
pub fn generate_link_section(asset: impl ToTokens) -> TokenStream2 {
let position = proc_macro2::Span::call_site();
let asset_description = serde_json::to_string(asset).unwrap();
let len = asset_description.as_bytes().len();
let asset_bytes = syn::LitByteStr::new(asset_description.as_bytes(), position);
let section_name = syn::LitStr::new(manganis_core::LinkSection::CURRENT.link_section, position);
let section_name = syn::LitStr::new(
manganis_core::linker::LinkSection::CURRENT.link_section,
position,
);
quote::quote! {
// First serialize the asset into a constant sized buffer
const __BUFFER: manganis::macro_helpers::const_serialize::ConstVec<u8> = manganis::macro_helpers::serialize_asset(&#asset);
// Then pull out the byte slice
const __BYTES: &[u8] = __BUFFER.as_ref();
// And the length of the byte slice
const __LEN: usize = __BYTES.len();
// Now that we have the size of the asset, copy the bytes into a static array
#[link_section = #section_name]
#[used]
static ASSET: [u8; #len] = * #asset_bytes;
static __LINK_SECTION: [u8; __LEN] = manganis::macro_helpers::copy_bytes(__BYTES);
fn __volatile_reader() -> u8 {
unsafe { std::ptr::read_volatile(ASSET.as_ptr()) }
fn __keep_link_section() -> u8 {
unsafe { std::ptr::read_volatile(__LINK_SECTION.as_ptr()) }
}
}
}
+5 -24
View File
@@ -13,29 +13,10 @@ keywords = ["assets"]
[lib]
[dependencies]
manganis-macro = { workspace = true, optional = true }
dioxus-core-types = { workspace = true }
# dunce = "1.0.2"
# [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
# core-foundation = "0.10.0"
# [target.'cfg(target_os = "macos")'.dependencies]
# core-foundation = "0.9.3"
# [target.'cfg(target_os ="macos")'.dependencies]
# infer = { workspace = true }
# dirs = "5.0.1"
# infer = { workspace = true }
# manganis-common = { workspace = true }
once_cell = "1.19.0"
dunce = "1.0.2"
serde = { version = "1.0.183", features = ["derive"] }
anyhow = "1"
base64 = { workspace = true }
const-serialize = { workspace = true }
manganis-core = { workspace = true }
manganis-macro = { workspace = true }
[features]
default = ["macro"]
html = []
# url-encoding = ["manganis-macro/url-encoding"]
macro = ["dep:manganis-macro"]
default = []
dioxus = ["manganis-core/dioxus"]
+31
View File
@@ -0,0 +1,31 @@
# Manganis
The Manganis allows you to submit assets to any build tool that supports collecting assets. It makes it easy to self-host assets that are distributed throughout your libraries. Manganis also handles optimizing, converting, and fetching assets.
If you defined this in a component library:
```rust
use manganis::{Asset, asset};
const AVIF_ASSET: Asset = manganis::asset!("/assets/image.png");
```
AVIF_ASSET will be set to a new file name that will be served by some CLI. That file can be collected by any package that depends on the component library.
```rust
use manganis::{ImageFormat, ImageAssetOptions, Asset, asset, ImageSize};
// You can collect arbitrary files. Absolute paths are resolved relative to the package root
const _: Asset = asset!("/assets/script.js");
// You can collect images which will be automatically optimized
pub const PNG_ASSET: Asset =
asset!("/assets/image.png");
// Resize the image at compile time to make the assets smaller
pub const RESIZED_PNG_ASSET: Asset =
asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
// Or convert the image at compile time to a web friendly format
pub const AVIF_ASSET: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
```
## Adding Support to Your CLI
To add support for your CLI, you need to integrate with the [manganis_cli_support](https://github.com/DioxusLabs/manganis/tree/main/cli-support) crate. This crate provides utilities to collect assets that integrate with the Manganis macro. It makes it easy to integrate an asset collection and optimization system into a build tool.
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
-87
View File
@@ -1,87 +0,0 @@
use dioxus_core_types::DioxusFormattable;
use std::path::PathBuf;
/// Asset
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub struct Asset {
/// The input URI given to the macro
pub input: &'static str,
/// The absolute path to the asset on the filesystem
pub local: &'static str,
/// The asset location after its been bundled
///
/// `blah-123.css``
pub bundled: &'static str,
/// The metadata for this asset plumbed by the compiler
pub metadata: fn() -> u8,
}
impl Asset {
/// Create a new asset
pub const fn new(self) -> Self {
self
}
/// Create a new asset but with a relative path
///
/// This method is deprecated and will be removed in a future release.
#[deprecated(
note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`"
)]
pub const fn new_relative(self) -> Self {
self
}
/// Get the path to the asset
pub fn path(&self) -> PathBuf {
PathBuf::from(self.input.to_string())
}
/// Get the path to the asset
pub fn relative_path(&self) -> PathBuf {
PathBuf::from(self.input.trim_start_matches('/').to_string())
}
/// Return a canonicalized path to the asset
///
/// Attempts to resolve it against an `assets` folder in the current directory.
/// If that doesn't exist, it will resolve against the cargo manifest dir
pub fn resolve(&self) -> PathBuf {
// Force a volatile read of the metadata to ensure the symbol makes it into the binary
(self.metadata)();
// If the asset is relative, we resolve the asset at the current directory
if !dioxus_core_types::is_bundled_app() {
return PathBuf::from(self.local);
}
// Otherwise presumably we're bundled and we can use the bundled path
PathBuf::from("/assets/").join(PathBuf::from(self.bundled.trim_start_matches('/')))
}
}
impl From<Asset> for String {
fn from(value: Asset) -> Self {
value.to_string()
}
}
impl From<Asset> for Option<String> {
fn from(value: Asset) -> Self {
Some(value.to_string())
}
}
impl std::fmt::Display for Asset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.resolve().display())
}
}
impl DioxusFormattable for Asset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
-31
View File
@@ -1,31 +0,0 @@
use crate::Asset;
/// This is basically a compile-time version of ResourceAsset
/// A struct that contains the relative and absolute paths of an asset
#[derive(Debug, PartialEq, PartialOrd, Clone, Hash)]
pub struct FolderAsset {
src: Asset,
}
impl Asset {
/// Create a new folder using `Asset` as the base type
pub const fn folder(self) -> FolderAsset {
FolderAsset::new(self)
}
}
impl FolderAsset {
/// Create a new folder asset from an `Asset`
pub const fn new(src: Asset) -> Self {
Self { src }
}
}
/// The builder for [`FolderAsset`]
pub struct FolderAssetBuilder;
/// Create an folder asset from the local path
#[allow(unused)]
pub const fn folder(src: &'static str) -> FolderAssetBuilder {
FolderAssetBuilder {}
}
+39
View File
@@ -0,0 +1,39 @@
use const_serialize::{serialize_const, ConstVec, SerializeConst};
// From rustchash - https://github.com/rust-lang/rustc-hash/blob/6745258da00b7251bed4a8461871522d0231a9c7/src/lib.rs#L98
const K: u64 = 0xf1357aea2e62a9c5;
pub(crate) struct ConstHasher {
hash: u64,
}
impl ConstHasher {
pub const fn new() -> Self {
Self { hash: 0 }
}
pub const fn finish(&self) -> u64 {
self.hash
}
pub const fn write(mut self, bytes: &[u8]) -> Self {
let mut i = 0;
while i < bytes.len() {
self = self.write_byte(bytes[i]);
i += 1;
}
self
}
pub const fn write_byte(mut self, byte: u8) -> Self {
self.hash = self.hash.wrapping_add(byte as u64).wrapping_mul(K);
self
}
pub const fn hash_by_bytes<T: SerializeConst>(self, item: &T) -> Self {
let mut bytes = ConstVec::new();
bytes = serialize_const(item, bytes);
let bytes = bytes.as_ref();
self.write(bytes)
}
}
-141
View File
@@ -1,141 +0,0 @@
use dioxus_core_types::DioxusFormattable;
use crate::Asset;
/// An image asset that is built by the [`asset!`] macro
#[derive(Debug, PartialEq, PartialOrd, Clone, Hash, Copy)]
pub struct ImageAsset {
/// The path to the image
asset: Asset,
/// A low quality preview of the image that is URL encoded
preview: Option<&'static str>,
/// A caption for the image
caption: Option<&'static str>,
/// The format of the image
format: Option<ImageType>,
}
impl Asset {
/// Convert this asset into an image asset
pub const fn image(self) -> ImageAsset {
ImageAsset::new(self)
}
}
impl ImageAsset {
/// Creates a new image asset
pub const fn new(path: Asset) -> Self {
Self {
asset: path,
preview: None,
caption: None,
format: None,
}
}
/// Returns the path to the image
pub const fn path(&self) -> &'static str {
self.asset.bundled
}
/// Returns the preview of the image
pub const fn preview(&self) -> Option<&'static str> {
self.preview
}
/// Sets the preview of the image
pub const fn with_preview(self, preview: Option<&'static str>) -> Self {
Self { preview, ..self }
}
/// Returns the caption of the image
pub const fn caption(&self) -> Option<&'static str> {
self.caption
}
/// Sets the caption of the image
pub const fn with_caption(self, caption: Option<&'static str>) -> Self {
Self { caption, ..self }
}
/// Sets the format of the image
pub const fn format(self, format: Option<ImageType>) -> Self {
Self { format, ..self }
}
}
impl std::ops::Deref for ImageAsset {
type Target = Asset;
fn deref(&self) -> &Self::Target {
&self.asset
}
}
impl std::fmt::Display for ImageAsset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.asset.fmt(f)
}
}
impl DioxusFormattable for ImageAsset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub enum ImageType {
/// A png image. Png images cannot contain transparency and tend to compress worse than other formats
Png,
/// A jpg image. Jpg images can contain transparency and tend to compress better than png images
Jpg,
/// A webp image. Webp images can contain transparency and tend to compress better than jpg images
Webp,
/// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
Avif,
}
/// A builder for an image asset. This must be used in the [`asset!`] macro.
///
/// > **Note**: This will do nothing outside of the `asset!` macro
pub struct ImageAssetBuilder;
impl ImageAssetBuilder {
/// Sets the format of the image
#[allow(unused)]
pub const fn format(self, format: ImageType) -> Self {
Self
}
/// Sets the size of the image
#[allow(unused)]
pub const fn size(self, x: u32, y: u32) -> Self {
Self
}
/// Make the image use a low quality preview
#[allow(unused)]
pub const fn low_quality_preview(self) -> Self {
Self
}
/// Make the image preloaded
#[allow(unused)]
pub const fn preload(self) -> Self {
Self
}
/// Make the image URL encoded
#[allow(unused)]
pub const fn url_encoded(self) -> Self {
Self
}
}
/// Create an image asset from the local path or url to the image
#[allow(unused)]
pub const fn image(path: &'static str) -> ImageAssetBuilder {
ImageAssetBuilder
}
+9 -12
View File
@@ -1,15 +1,12 @@
//! Manganis is a Rust library for bundling assets into a final binary.
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#[cfg(feature = "macro")]
pub use manganis_macro::*;
mod hash;
#[doc(hidden)]
pub mod macro_helpers;
pub use manganis_macro::asset;
mod folder;
pub use folder::*;
mod images;
pub use images::*;
mod builder;
pub use builder::*;
pub use manganis_core::{
Asset, AssetOptions, BundledAsset, CssAssetOptions, FolderAssetOptions, ImageAssetOptions,
ImageFormat, ImageSize, JsAssetOptions,
};
@@ -0,0 +1,144 @@
pub use const_serialize;
use const_serialize::{serialize_const, ConstStr, ConstVec};
use manganis_core::{AssetOptions, BundledAsset};
use crate::hash::ConstHasher;
/// Format the input path with a hash to create an unique output path for the macro in the form `{input_path}-{hash}.{extension}`
pub const fn generate_unique_path(
input_path: &str,
content_hash: u64,
asset_config: &AssetOptions,
) -> ConstStr {
// Format the unique path with the format `{input_path}-{hash}.{extension}`
// Start with the input path
let mut input_path = ConstStr::new(input_path);
// Then strip the prefix from the input path. The path comes from the build platform, but
// in wasm, we don't know what the path separator is from the build platform. We need to
// split by both unix and windows paths and take the smallest one
let mut extension = None;
match (input_path.rsplit_once('/'), input_path.rsplit_once('\\')) {
(Some((_, unix_new_input_path)), Some((_, windows_new_input_path))) => {
input_path = if unix_new_input_path.len() < windows_new_input_path.len() {
unix_new_input_path
} else {
windows_new_input_path
};
}
(Some((_, unix_new_input_path)), _) => {
input_path = unix_new_input_path;
}
(_, Some((_, windows_new_input_path))) => {
input_path = windows_new_input_path;
}
_ => {}
}
if let Some((new_input_path, new_extension)) = input_path.rsplit_once('.') {
extension = Some(new_extension);
input_path = new_input_path;
}
// Then add a dash
let mut macro_output_path = input_path.push_str("-");
// Hash the contents along with the asset config to create a unique hash for the asset
// When this hash changes, the client needs to re-fetch the asset
let mut hasher = ConstHasher::new();
hasher = hasher.write(&content_hash.to_le_bytes());
hasher = hasher.hash_by_bytes(asset_config);
let hash = hasher.finish();
// Then add the hash in hex form
let hash_bytes = hash.to_le_bytes();
let mut i = 0;
while i < hash_bytes.len() {
let byte = hash_bytes[i];
let first = byte >> 4;
let second = byte & 0x0f;
const fn byte_to_char(byte: u8) -> char {
match char::from_digit(byte as u32, 16) {
Some(c) => c,
None => panic!("byte must be a valid digit"),
}
}
macro_output_path = macro_output_path.push(byte_to_char(first));
macro_output_path = macro_output_path.push(byte_to_char(second));
i += 1;
}
// Finally add the extension
match asset_config.extension() {
Some(extension) => {
macro_output_path = macro_output_path.push('.');
macro_output_path = macro_output_path.push_str(extension)
}
None => {
if let Some(extension) = extension {
macro_output_path = macro_output_path.push('.');
macro_output_path = macro_output_path.push_str(extension.as_str())
}
}
}
macro_output_path
}
#[test]
fn test_unique_path() {
use manganis_core::{ImageAssetOptions, ImageFormat};
use std::path::PathBuf;
let mut input_path = PathBuf::from("some");
input_path.push("prefix");
input_path.push("test.png");
let content_hash = 123456789;
let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif));
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif");
// Changing the path without changing the contents shouldn't change the hash
let mut input_path = PathBuf::from("some");
input_path.push("prefix");
input_path.push("prefix");
input_path.push("test.png");
let content_hash = 123456789;
let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif));
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif");
let mut input_path = PathBuf::from("test");
input_path.push("ing");
input_path.push("test");
let content_hash = 123456789;
let asset_config = AssetOptions::Unknown;
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-c8c4cfad21cac262");
// Just changing the content hash should change the total hash
let mut input_path = PathBuf::from("test");
input_path.push("ing");
input_path.push("test");
let content_hash = 123456780;
let asset_config = AssetOptions::Unknown;
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-7bced03789ff865c");
}
/// Serialize an asset to a const buffer
pub const fn serialize_asset(asset: &BundledAsset) -> ConstVec<u8> {
let write = ConstVec::new();
serialize_const(asset, write)
}
/// Copy a slice into a constant sized buffer at compile time
pub const fn copy_bytes<const N: usize>(bytes: &[u8]) -> [u8; N] {
let mut out = [0; N];
let mut i = 0;
while i < N {
out[i] = bytes[i];
i += 1;
}
out
}