embed_resources/structs/
resource_container.rs

1use crate::Resource;
2use std::io;
3use std::path::Path;
4
5pub struct ResourceContainer<'a> {
6    storage_dir: &'a Path,
7    struct_output_path: &'a Path,
8    struct_name: String,
9    resources: Vec<(String, Resource, bool)>, // (Name, Resource, Compress)
10}
11
12impl<'a> ResourceContainer<'a> {
13    /// Creates a new empty container.
14    pub fn new(storage_dir: &'a Path, struct_output_path: &'a Path, struct_name: &str) -> Self {
15        Self {
16            storage_dir,
17            struct_output_path,
18            struct_name: struct_name.to_string(),
19            resources: Vec::new(),
20        }
21    }
22
23    /// Adds a resource to the container.
24    pub fn add_resource(&mut self, name: &str, resource: Resource, compress: bool) {
25        self.resources.push((name.to_string(), resource, compress));
26    }
27
28    /// Processes all resources and writes them to the embed directory.
29    pub fn embed_all(&self) -> io::Result<()> {
30        let mut byte_arrays = Vec::new();
31
32        for (name, resource, compress) in &self.resources {
33            let bytes = resource.fetch(*compress)?;
34            byte_arrays.push((name.as_str(), bytes));
35        }
36
37        // Use `embed-bytes` to write the byte arrays
38        embed_bytes::write_byte_arrays(self.storage_dir, self.struct_output_path, self.struct_name.as_str(), byte_arrays)
39    }
40}