manganis_common/
manifest.rs

1use crate::package::PackageAssets;
2
3/// A manifest of all assets collected from dependencies
4#[derive(Debug, PartialEq, Default, Clone)]
5pub struct AssetManifest {
6    pub(crate) packages: Vec<PackageAssets>,
7}
8
9impl AssetManifest {
10    /// Creates a new asset manifest
11    pub fn new(packages: Vec<PackageAssets>) -> Self {
12        Self { packages }
13    }
14
15    /// Returns all assets collected from dependencies
16    pub fn packages(&self) -> &Vec<PackageAssets> {
17        &self.packages
18    }
19
20    #[cfg(feature = "html")]
21    /// Returns the HTML that should be injected into the head of the page
22    pub fn head(&self) -> String {
23        let mut head = String::new();
24        for package in self.packages() {
25            for asset in package.assets() {
26                if let crate::AssetType::File(file) = asset {
27                    match file.options() {
28                        crate::FileOptions::Css(_) => {
29                            let asset_path = file.served_location();
30                            head.push_str(&format!(
31                                "<link rel=\"stylesheet\" href=\"{asset_path}\">\n"
32                            ))
33                        }
34                        crate::FileOptions::Image(image_options) => {
35                            if image_options.preload() {
36                                let asset_path = file.served_location();
37                                head.push_str(&format!(
38                                    "<link rel=\"preload\" as=\"image\" href=\"{asset_path}\">\n"
39                                ))
40                            }
41                        }
42                        _ => {}
43                    }
44                }
45            }
46        }
47        head
48    }
49}