manganis_core/
css.rs

1use const_serialize::SerializeConst;
2
3use crate::AssetOptions;
4
5/// Options for a css asset
6#[derive(
7    Debug,
8    PartialEq,
9    PartialOrd,
10    Clone,
11    Copy,
12    Hash,
13    SerializeConst,
14    serde::Serialize,
15    serde::Deserialize,
16)]
17pub struct CssAssetOptions {
18    minify: bool,
19    preload: bool,
20}
21
22impl Default for CssAssetOptions {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl CssAssetOptions {
29    /// Create a new css asset using the builder
30    pub const fn new() -> Self {
31        Self {
32            preload: false,
33            minify: true,
34        }
35    }
36
37    /// Sets whether the css should be minified (default: true)
38    ///
39    /// Minifying the css can make your site load faster by loading less data
40    ///
41    /// ```rust
42    /// # use manganis::{asset, Asset, CssAssetOptions};
43    /// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_minify(false));
44    /// ```
45    #[allow(unused)]
46    pub const fn with_minify(self, minify: bool) -> Self {
47        Self { minify, ..self }
48    }
49
50    /// Check if the asset is minified
51    pub const fn minified(&self) -> bool {
52        self.minify
53    }
54
55    /// Make the asset preloaded
56    ///
57    /// 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
58    ///
59    /// ```rust
60    /// # use manganis::{asset, Asset, CssAssetOptions};
61    /// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_preload(true));
62    /// ```
63    #[allow(unused)]
64    pub const fn with_preload(self, preload: bool) -> Self {
65        Self { preload, ..self }
66    }
67
68    /// Check if the asset is preloaded
69    pub const fn preloaded(&self) -> bool {
70        self.preload
71    }
72
73    /// Convert the options into options for a generic asset
74    pub const fn into_asset_options(self) -> AssetOptions {
75        AssetOptions::Css(self)
76    }
77}