remove doctests for mangnais 07

This commit is contained in:
Jonathan Kelley committed 2026-01-16 20:00:57 -08:00
1 parent 22b06badde
commit 97a670af0f
7 files changed
-280

No files matched your search

@@ -2,18 +2,10 @@ use crate::AssetOptions;
use const_serialize_07::{ConstStr, SerializeConst}; use const_serialize_07::{ConstStr, SerializeConst};
use std::{fmt::Debug, hash::Hash}; use std::{fmt::Debug, hash::Hash};
/// An asset that should be copied by the bundler with some options. This type will be
/// serialized into the binary.
/// 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, Eq, Clone, Copy, SerializeConst, serde::Serialize, serde::Deserialize)] #[derive(Debug, Eq, Clone, Copy, SerializeConst, serde::Serialize, serde::Deserialize)]
pub struct BundledAsset { pub struct BundledAsset {
/// The absolute path of the asset
absolute_source_path: ConstStr, absolute_source_path: ConstStr,
/// The bundled path of the asset
bundled_path: ConstStr, bundled_path: ConstStr,
/// The options for the asset
options: AssetOptions, options: AssetOptions,
} }
@@ -52,8 +44,6 @@ impl Hash for BundledAsset {
impl BundledAsset { impl BundledAsset {
#[doc(hidden)] #[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset
pub const fn new( pub const fn new(
absolute_source_path: &str, absolute_source_path: &str,
bundled_path: &str, bundled_path: &str,
@@ -66,17 +56,14 @@ impl BundledAsset {
} }
} }
/// Get the bundled name of the asset. This identifier cannot be used to read the asset directly
pub fn bundled_path(&self) -> &str { pub fn bundled_path(&self) -> &str {
self.bundled_path.as_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 { pub fn absolute_source_path(&self) -> &str {
self.absolute_source_path.as_str() self.absolute_source_path.as_str()
} }
/// Get the options for the asset
pub const fn options(&self) -> &AssetOptions { pub const fn options(&self) -> &AssetOptions {
&self.options &self.options
} }
-37
View File
@@ -1,7 +1,6 @@
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant}; use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
use const_serialize_07::SerializeConst; use const_serialize_07::SerializeConst;
/// Options for a css asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -27,12 +26,10 @@ impl Default for CssAssetOptions {
} }
impl CssAssetOptions { impl CssAssetOptions {
/// Create a new css asset using the builder
pub const fn new() -> AssetOptionsBuilder<CssAssetOptions> { pub const fn new() -> AssetOptionsBuilder<CssAssetOptions> {
AssetOptions::css() AssetOptions::css()
} }
/// Create a default css asset options
pub const fn default() -> Self { pub const fn default() -> Self {
Self { Self {
preload: false, preload: false,
@@ -41,76 +38,42 @@ impl CssAssetOptions {
} }
} }
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool { pub const fn preloaded(&self) -> bool {
self.preload self.preload
} }
/// Check if the asset is statically created
pub const fn static_head(&self) -> bool { pub const fn static_head(&self) -> bool {
self.static_head self.static_head
} }
/// Check if the asset is minified
pub const fn minified(&self) -> bool { pub const fn minified(&self) -> bool {
self.minify self.minify
} }
} }
impl AssetOptions { impl AssetOptions {
/// Create a new css asset builder
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css());
/// ```
pub const fn css() -> AssetOptionsBuilder<CssAssetOptions> { pub const fn css() -> AssetOptionsBuilder<CssAssetOptions> {
AssetOptionsBuilder::variant(CssAssetOptions::default()) AssetOptionsBuilder::variant(CssAssetOptions::default())
} }
} }
impl AssetOptionsBuilder<CssAssetOptions> { impl AssetOptionsBuilder<CssAssetOptions> {
/// 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, AssetOptions};
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_minify(false));
/// ```
pub const fn with_minify(mut self, minify: bool) -> Self { pub const fn with_minify(mut self, minify: bool) -> Self {
self.variant.minify = minify; self.variant.minify = minify;
self self
} }
/// Make the asset statically inserted (default: false)
///
/// Statically insert the file at compile time.
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_static_head(true));
/// ```
#[allow(unused)] #[allow(unused)]
pub const fn with_static_head(mut self, static_head: bool) -> Self { pub const fn with_static_head(mut self, static_head: bool) -> Self {
self.variant.static_head = static_head; self.variant.static_head = static_head;
self self
} }
/// Make the asset preloaded
///
/// Preloading css will make the file 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, AssetOptions};
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_preload(true));
/// ```
pub const fn with_preload(mut self, preload: bool) -> Self { pub const fn with_preload(mut self, preload: bool) -> Self {
self.variant.preload = preload; self.variant.preload = preload;
self self
} }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: true, add_hash: true,
@@ -2,7 +2,6 @@ use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
use const_serialize_07::SerializeConst; use const_serialize_07::SerializeConst;
use std::collections::HashSet; use std::collections::HashSet;
/// Options for a css module asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -29,12 +28,10 @@ impl Default for CssModuleAssetOptions {
} }
impl CssModuleAssetOptions { impl CssModuleAssetOptions {
/// Create a new css asset using the builder
pub const fn new() -> AssetOptionsBuilder<CssModuleAssetOptions> { pub const fn new() -> AssetOptionsBuilder<CssModuleAssetOptions> {
AssetOptions::css_module() AssetOptions::css_module()
} }
/// Create a default css module asset options
pub const fn default() -> Self { pub const fn default() -> Self {
Self { Self {
preload: false, preload: false,
@@ -42,47 +39,32 @@ impl CssModuleAssetOptions {
} }
} }
/// Check if the asset is minified
pub const fn minified(&self) -> bool { pub const fn minified(&self) -> bool {
self.minify self.minify
} }
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool { pub const fn preloaded(&self) -> bool {
self.preload self.preload
} }
} }
impl AssetOptions { impl AssetOptions {
/// Create a new css module asset builder
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css_module());
/// ```
pub const fn css_module() -> AssetOptionsBuilder<CssModuleAssetOptions> { pub const fn css_module() -> AssetOptionsBuilder<CssModuleAssetOptions> {
AssetOptionsBuilder::variant(CssModuleAssetOptions::default()) AssetOptionsBuilder::variant(CssModuleAssetOptions::default())
} }
} }
impl AssetOptionsBuilder<CssModuleAssetOptions> { impl AssetOptionsBuilder<CssModuleAssetOptions> {
/// Sets whether the css should be minified (default: true)
///
/// Minifying the css can make your site load faster by loading less data
pub const fn with_minify(mut self, minify: bool) -> Self { pub const fn with_minify(mut self, minify: bool) -> Self {
self.variant.minify = minify; self.variant.minify = minify;
self self
} }
/// 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
pub const fn with_preload(mut self, preload: bool) -> Self { pub const fn with_preload(mut self, preload: bool) -> Self {
self.variant.preload = preload; self.variant.preload = preload;
self self
} }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: self.add_hash, add_hash: self.add_hash,
@@ -91,44 +73,23 @@ impl AssetOptionsBuilder<CssModuleAssetOptions> {
} }
} }
/// Collect CSS classes & ids.
///
/// This is a rudementary css classes & ids collector.
/// Idents used only in media queries will not be collected. (not support yet)
///
/// There are likely a number of edge cases that will show up.
///
/// Returns `(HashSet<Classes>, HashSet<Ids>)`
pub fn collect_css_idents(css: &str) -> (HashSet<String>, HashSet<String>) { pub fn collect_css_idents(css: &str) -> (HashSet<String>, HashSet<String>) {
const ALLOWED: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"; const ALLOWED: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
let mut classes = HashSet::new(); let mut classes = HashSet::new();
let mut ids = HashSet::new(); let mut ids = HashSet::new();
// Collected ident name and true for ids.
let mut start: Option<(String, bool)> = None; let mut start: Option<(String, bool)> = None;
// True if we have the first comment start delimiter `/`
let mut comment_start = false; let mut comment_start = false;
// True if we have the first comment end delimiter '*'
let mut comment_end = false; let mut comment_end = false;
// True if we're in a comment scope.
let mut in_comment_scope = false; let mut in_comment_scope = false;
// True if we're in a block scope: `#hi { this is block scope }`
let mut in_block_scope = false; let mut in_block_scope = false;
// If we are currently collecting an ident:
// - Check if the char is allowed, put it into the ident string.
// - If not allowed, finalize the ident string and reset start.
// Otherwise:
// Check if character is a `.` or `#` representing a class or string, and start collecting.
for (_byte_index, c) in css.char_indices() { for (_byte_index, c) in css.char_indices() {
if let Some(ident) = start.as_mut() { if let Some(ident) = start.as_mut() {
if ALLOWED.find(c).is_some() { if ALLOWED.find(c).is_some() {
// CSS ignore idents that start with a number.
// 1. Difficult to process
// 2. Avoid false positives (transition: 0.5s)
if ident.0.is_empty() && c.is_numeric() { if ident.0.is_empty() && c.is_numeric() {
start = None; start = None;
continue; continue;
@@ -144,36 +105,28 @@ pub fn collect_css_idents(css: &str) -> (HashSet<String>, HashSet<String>) {
start = None; start = None;
} }
} else { } else {
// Handle entering an exiting scopede.
match c { match c {
// Mark as comment scope if we have comment start: /*
'*' if comment_start => { '*' if comment_start => {
comment_start = false; comment_start = false;
in_comment_scope = true; in_comment_scope = true;
} }
// Mark start of comment end if in comment scope: */
'*' if in_comment_scope => comment_end = true, '*' if in_comment_scope => comment_end = true,
// Mark as comment start if not in comment scope and no comment start, mark comment_start
'/' if !in_comment_scope => { '/' if !in_comment_scope => {
comment_start = true; comment_start = true;
} }
// If we get the closing delimiter, mark as non-comment scope.
'/' if comment_end => { '/' if comment_end => {
in_comment_scope = false; in_comment_scope = false;
comment_start = false; comment_start = false;
comment_end = false; comment_end = false;
} }
// Entering & Exiting block scope.
'{' => in_block_scope = true, '{' => in_block_scope = true,
'}' => in_block_scope = false, '}' => in_block_scope = false,
// Any other character, reset comment start and end if not in scope.
_ => { _ => {
comment_start = false; comment_start = false;
comment_end = false; comment_end = false;
} }
} }
// No need to process this char if in bad scope.
if in_comment_scope || in_block_scope { if in_comment_scope || in_block_scope {
continue; continue;
} }
@@ -2,7 +2,6 @@ use const_serialize_07::SerializeConst;
use crate::{AssetOptions, AssetOptionsBuilder}; use crate::{AssetOptions, AssetOptionsBuilder};
/// The builder for a folder asset.
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -24,31 +23,22 @@ impl Default for FolderAssetOptions {
} }
impl FolderAssetOptions { impl FolderAssetOptions {
/// Create a new folder asset builder
pub const fn new() -> AssetOptionsBuilder<FolderAssetOptions> { pub const fn new() -> AssetOptionsBuilder<FolderAssetOptions> {
AssetOptions::folder() AssetOptions::folder()
} }
/// Create a default folder asset options
pub const fn default() -> Self { pub const fn default() -> Self {
Self {} Self {}
} }
} }
impl AssetOptions { impl AssetOptions {
/// Create a new folder asset builder
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets", AssetOptions::folder());
/// ```
pub const fn folder() -> AssetOptionsBuilder<FolderAssetOptions> { pub const fn folder() -> AssetOptionsBuilder<FolderAssetOptions> {
AssetOptionsBuilder::variant(FolderAssetOptions::default()) AssetOptionsBuilder::variant(FolderAssetOptions::default())
} }
} }
impl AssetOptionsBuilder<FolderAssetOptions> { impl AssetOptionsBuilder<FolderAssetOptions> {
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: false, add_hash: false,
@@ -2,7 +2,6 @@ use const_serialize_07::SerializeConst;
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant}; use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
/// 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( #[derive(
Debug, Debug,
Eq, Eq,
@@ -17,19 +16,13 @@ use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
)] )]
#[repr(u8)] #[repr(u8)]
pub enum ImageFormat { pub enum ImageFormat {
/// A png image. Png images cannot contain transparency and tend to compress worse than other formats
Png, Png,
/// A jpg image. Jpg images can contain transparency and tend to compress better than png images
Jpg, Jpg,
/// A webp image. Webp images can contain transparency and tend to compress better than jpg images
Webp, Webp,
/// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
Avif, Avif,
/// An unknown image type
Unknown, Unknown,
} }
/// The size of an image asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -44,18 +37,13 @@ pub enum ImageFormat {
)] )]
#[repr(C, u8)] #[repr(C, u8)]
pub enum ImageSize { pub enum ImageSize {
/// A manual size in pixels
Manual { Manual {
/// The width of the image in pixels
width: u32, width: u32,
/// The height of the image in pixels
height: u32, height: u32,
}, },
/// The size will be automatically determined from the image source
Automatic, Automatic,
} }
/// Options for an image asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -82,12 +70,10 @@ impl Default for ImageAssetOptions {
} }
impl ImageAssetOptions { impl ImageAssetOptions {
/// Create a new builder for image asset options
pub const fn new() -> AssetOptionsBuilder<ImageAssetOptions> { pub const fn new() -> AssetOptionsBuilder<ImageAssetOptions> {
AssetOptions::image() AssetOptions::image()
} }
/// Create a default image asset options
pub const fn default() -> Self { pub const fn default() -> Self {
Self { Self {
ty: ImageFormat::Unknown, ty: ImageFormat::Unknown,
@@ -97,17 +83,14 @@ impl ImageAssetOptions {
} }
} }
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool { pub const fn preloaded(&self) -> bool {
self.preload self.preload
} }
/// Get the format of the image
pub const fn format(&self) -> ImageFormat { pub const fn format(&self) -> ImageFormat {
self.ty self.ty
} }
/// Get the size of the image
pub const fn size(&self) -> ImageSize { pub const fn size(&self) -> ImageSize {
self.size self.size
} }
@@ -124,108 +107,43 @@ impl ImageAssetOptions {
} }
impl AssetOptions { impl AssetOptions {
/// Create a new image asset builder
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image());
/// ```
pub const fn image() -> AssetOptionsBuilder<ImageAssetOptions> { pub const fn image() -> AssetOptionsBuilder<ImageAssetOptions> {
AssetOptionsBuilder::variant(ImageAssetOptions::default()) AssetOptionsBuilder::variant(ImageAssetOptions::default())
} }
} }
impl AssetOptionsBuilder<ImageAssetOptions> { impl AssetOptionsBuilder<ImageAssetOptions> {
/// 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, AssetOptions};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_preload(true));
/// ```
pub const fn with_preload(mut self, preload: bool) -> Self { pub const fn with_preload(mut self, preload: bool) -> Self {
self.variant.preload = preload; self.variant.preload = preload;
self self
} }
/// 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, AssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_format(ImageFormat::Webp));
/// ```
pub const fn with_format(mut self, format: ImageFormat) -> Self { pub const fn with_format(mut self, format: ImageFormat) -> Self {
self.variant.ty = format; self.variant.ty = format;
self 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, AssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_avif());
/// ```
pub const fn with_avif(self) -> Self { pub const fn with_avif(self) -> Self {
self.with_format(ImageFormat::Avif) 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, AssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_webp());
/// ```
pub const fn with_webp(self) -> Self { pub const fn with_webp(self) -> Self {
self.with_format(ImageFormat::Webp) 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, AssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_jpg());
/// ```
pub const fn with_jpg(self) -> Self { pub const fn with_jpg(self) -> Self {
self.with_format(ImageFormat::Jpg) 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, AssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_png());
/// ```
pub const fn with_png(self) -> Self { pub const fn with_png(self) -> Self {
self.with_format(ImageFormat::Png) self.with_format(ImageFormat::Png)
} }
/// 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, AssetOptions, ImageSize};
/// const _: Asset = asset!("/assets/image.png", AssetOptions::image().with_size(ImageSize::Manual { width: 512, height: 512 }));
/// ```
pub const fn with_size(mut self, size: ImageSize) -> Self { pub const fn with_size(mut self, size: ImageSize) -> Self {
self.variant.size = size; self.variant.size = size;
self self
} }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: self.add_hash, add_hash: self.add_hash,
-37
View File
@@ -2,7 +2,6 @@ use const_serialize_07::SerializeConst;
use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant}; use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
/// Options for a javascript asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -28,12 +27,10 @@ impl Default for JsAssetOptions {
} }
impl JsAssetOptions { impl JsAssetOptions {
/// Create a new js asset options builder
pub const fn new() -> AssetOptionsBuilder<JsAssetOptions> { pub const fn new() -> AssetOptionsBuilder<JsAssetOptions> {
AssetOptions::js() AssetOptions::js()
} }
/// Create a default js asset options
pub const fn default() -> Self { pub const fn default() -> Self {
Self { Self {
preload: false, preload: false,
@@ -42,78 +39,44 @@ impl JsAssetOptions {
} }
} }
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool { pub const fn preloaded(&self) -> bool {
self.preload self.preload
} }
/// Check if the asset is statically created
pub const fn static_head(&self) -> bool { pub const fn static_head(&self) -> bool {
self.static_head self.static_head
} }
/// Check if the asset is minified
pub const fn minified(&self) -> bool { pub const fn minified(&self) -> bool {
self.minify self.minify
} }
} }
impl AssetOptions { impl AssetOptions {
/// Create a new js asset builder
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js());
/// ```
pub const fn js() -> AssetOptionsBuilder<JsAssetOptions> { pub const fn js() -> AssetOptionsBuilder<JsAssetOptions> {
AssetOptionsBuilder::variant(JsAssetOptions::default()) AssetOptionsBuilder::variant(JsAssetOptions::default())
} }
} }
impl AssetOptionsBuilder<JsAssetOptions> { impl AssetOptionsBuilder<JsAssetOptions> {
/// 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, AssetOptions};
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_minify(false));
/// ```
#[allow(unused)] #[allow(unused)]
pub const fn with_minify(mut self, minify: bool) -> Self { pub const fn with_minify(mut self, minify: bool) -> Self {
self.variant.minify = minify; self.variant.minify = minify;
self self
} }
/// Make the asset statically inserted (default: false)
///
/// Statically insert the file at compile time.
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_static_head(true));
/// ```
#[allow(unused)] #[allow(unused)]
pub const fn with_static_head(mut self, static_head: bool) -> Self { pub const fn with_static_head(mut self, static_head: bool) -> Self {
self.variant.static_head = static_head; self.variant.static_head = static_head;
self self
} }
/// 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, AssetOptions};
/// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_preload(true));
/// ```
#[allow(unused)] #[allow(unused)]
pub const fn with_preload(mut self, preload: bool) -> Self { pub const fn with_preload(mut self, preload: bool) -> Self {
self.variant.preload = preload; self.variant.preload = preload;
self self
} }
/// Convert the builder into asset options with the given variant
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: self.add_hash, add_hash: self.add_hash,
@@ -4,7 +4,6 @@ use crate::{
CssAssetOptions, CssModuleAssetOptions, FolderAssetOptions, ImageAssetOptions, JsAssetOptions, CssAssetOptions, CssModuleAssetOptions, FolderAssetOptions, ImageAssetOptions, JsAssetOptions,
}; };
/// Settings for a generic asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -19,29 +18,23 @@ use crate::{
)] )]
#[non_exhaustive] #[non_exhaustive]
pub struct AssetOptions { pub struct AssetOptions {
/// If a hash should be added to the asset path
pub(crate) add_hash: bool, pub(crate) add_hash: bool,
/// The variant of the asset
pub(crate) variant: AssetVariant, pub(crate) variant: AssetVariant,
} }
impl AssetOptions { impl AssetOptions {
/// Create a new asset options builder
pub const fn builder() -> AssetOptionsBuilder<()> { pub const fn builder() -> AssetOptionsBuilder<()> {
AssetOptionsBuilder::new() AssetOptionsBuilder::new()
} }
/// Get the variant of the asset
pub const fn variant(&self) -> &AssetVariant { pub const fn variant(&self) -> &AssetVariant {
&self.variant &self.variant
} }
/// Check if a hash should be added to the asset path
pub const fn hash_suffix(&self) -> bool { pub const fn hash_suffix(&self) -> bool {
self.add_hash self.add_hash
} }
/// 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> { pub const fn extension(&self) -> Option<&'static str> {
match self.variant { match self.variant {
AssetVariant::Image(image) => image.extension(), AssetVariant::Image(image) => image.extension(),
@@ -53,26 +46,13 @@ impl AssetOptions {
} }
} }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
self self
} }
} }
/// A builder for [`AssetOptions`]
///
/// ```rust
/// # use manganis::{AssetOptions, Asset, asset};
/// static ASSET: Asset = asset!(
/// "/assets/style.css",
/// AssetOptions::builder()
/// .with_hash_suffix(false)
/// );
/// ```
pub struct AssetOptionsBuilder<T> { pub struct AssetOptionsBuilder<T> {
/// If a hash should be added to the asset path
pub(crate) add_hash: bool, pub(crate) add_hash: bool,
/// The variant of the asset
pub(crate) variant: T, pub(crate) variant: T,
} }
@@ -83,7 +63,6 @@ impl Default for AssetOptionsBuilder<()> {
} }
impl AssetOptionsBuilder<()> { impl AssetOptionsBuilder<()> {
/// Create a new asset options builder with an unknown variant
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
add_hash: true, add_hash: true,
@@ -91,12 +70,10 @@ impl AssetOptionsBuilder<()> {
} }
} }
/// Create a default asset options builder
pub const fn default() -> Self { pub const fn default() -> Self {
Self::new() Self::new()
} }
/// Convert the builder into asset options with the given variant
pub const fn into_asset_options(self) -> AssetOptions { pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions { AssetOptions {
add_hash: self.add_hash, add_hash: self.add_hash,
@@ -106,7 +83,6 @@ impl AssetOptionsBuilder<()> {
} }
impl<T> AssetOptionsBuilder<T> { impl<T> AssetOptionsBuilder<T> {
/// Create a new asset options builder with the given variant
pub(crate) const fn variant(variant: T) -> Self { pub(crate) const fn variant(variant: T) -> Self {
Self { Self {
add_hash: true, add_hash: true,
@@ -114,36 +90,12 @@ impl<T> AssetOptionsBuilder<T> {
} }
} }
/// Set whether a hash should be added to the asset path. Manganis adds hashes to asset paths by default
/// for [cache busting](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#cache_busting).
/// With hashed assets, you can serve the asset with a long expiration time, and when the asset changes,
/// the hash in the path will change, causing the browser to fetch the new version.
///
/// This method will only effect if the hash is added to the bundled asset path. If you are using the asset
/// macro, the asset struct still needs to be used in your rust code to ensure the asset is included in the binary.
///
/// <div class="warning">
///
/// If you are using an asset outside of rust code where you know what the asset hash will be, you must use the
/// `#[used]` attribute to ensure the asset is included in the binary even if it is not referenced in the code.
///
/// ```rust
/// #[used]
/// static ASSET: manganis::Asset = manganis::asset!(
/// "/assets/style.css",
/// manganis::AssetOptions::builder()
/// .with_hash_suffix(false)
/// );
/// ```
///
/// </div>
pub const fn with_hash_suffix(mut self, add_hash: bool) -> Self { pub const fn with_hash_suffix(mut self, add_hash: bool) -> Self {
self.add_hash = add_hash; self.add_hash = add_hash;
self self
} }
} }
/// Settings for a specific type of asset
#[derive( #[derive(
Debug, Debug,
Eq, Eq,
@@ -159,16 +111,10 @@ impl<T> AssetOptionsBuilder<T> {
#[repr(C, u8)] #[repr(C, u8)]
#[non_exhaustive] #[non_exhaustive]
pub enum AssetVariant { pub enum AssetVariant {
/// An image asset
Image(ImageAssetOptions), Image(ImageAssetOptions),
/// A folder asset
Folder(FolderAssetOptions), Folder(FolderAssetOptions),
/// A css asset
Css(CssAssetOptions), Css(CssAssetOptions),
/// A css module asset
CssModule(CssModuleAssetOptions), CssModule(CssModuleAssetOptions),
/// A javascript asset
Js(JsAssetOptions), Js(JsAssetOptions),
/// An unknown asset
Unknown, Unknown,
} }