manganis_core/
css.rs

1use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
2use const_serialize_07 as const_serialize;
3use const_serialize_08::SerializeConst;
4
5/// Options for a css asset
6#[derive(
7    Debug,
8    Eq,
9    PartialEq,
10    PartialOrd,
11    Clone,
12    Copy,
13    Hash,
14    SerializeConst,
15    const_serialize::SerializeConst,
16    serde::Serialize,
17    serde::Deserialize,
18)]
19#[const_serialize(crate = const_serialize_08)]
20pub struct CssAssetOptions {
21    minify: bool,
22    preload: bool,
23    static_head: bool,
24}
25
26impl Default for CssAssetOptions {
27    fn default() -> Self {
28        Self::default()
29    }
30}
31
32impl CssAssetOptions {
33    /// Create a new css asset using the builder
34    pub const fn new() -> AssetOptionsBuilder<CssAssetOptions> {
35        AssetOptions::css()
36    }
37
38    /// Create a default css asset options
39    pub const fn default() -> Self {
40        Self {
41            preload: false,
42            minify: true,
43            static_head: false,
44        }
45    }
46
47    /// Check if the asset is preloaded
48    pub const fn preloaded(&self) -> bool {
49        self.preload
50    }
51
52    /// Check if the asset is statically created
53    pub const fn static_head(&self) -> bool {
54        self.static_head
55    }
56
57    /// Check if the asset is minified
58    pub const fn minified(&self) -> bool {
59        self.minify
60    }
61}
62
63impl AssetOptions {
64    /// Create a new css asset builder
65    ///
66    /// ```rust
67    /// # use manganis::{asset, Asset, AssetOptions};
68    /// const _: Asset = asset!("/assets/style.css", AssetOptions::css());
69    /// ```
70    pub const fn css() -> AssetOptionsBuilder<CssAssetOptions> {
71        AssetOptionsBuilder::variant(CssAssetOptions::default())
72    }
73}
74
75impl AssetOptionsBuilder<CssAssetOptions> {
76    /// Sets whether the css should be minified (default: true)
77    ///
78    /// Minifying the css can make your site load faster by loading less data
79    ///
80    /// ```rust
81    /// # use manganis::{asset, Asset, AssetOptions};
82    /// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_minify(false));
83    /// ```
84    pub const fn with_minify(mut self, minify: bool) -> Self {
85        self.variant.minify = minify;
86        self
87    }
88
89    /// Make the asset statically inserted (default: false)
90    ///
91    /// Statically insert the file at compile time.
92    ///
93    /// ```rust
94    /// # use manganis::{asset, Asset, AssetOptions};
95    /// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_static_head(true));
96    /// ```
97    #[allow(unused)]
98    pub const fn with_static_head(mut self, static_head: bool) -> Self {
99        self.variant.static_head = static_head;
100        self
101    }
102
103    /// Make the asset preloaded
104    ///
105    /// 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
106    ///
107    /// ```rust
108    /// # use manganis::{asset, Asset, AssetOptions};
109    /// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_preload(true));
110    /// ```
111    pub const fn with_preload(mut self, preload: bool) -> Self {
112        self.variant.preload = preload;
113        self
114    }
115
116    /// Convert the options into options for a generic asset
117    pub const fn into_asset_options(self) -> AssetOptions {
118        AssetOptions {
119            add_hash: true,
120            variant: AssetVariant::Css(self.variant),
121        }
122    }
123}