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