embed_resources/structs/
resource_container.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::Resource;
use std::io;
use std::path::Path;

pub struct ResourceContainer<'a> {
    output_path: &'a Path,
    resources: Vec<(String, Resource, bool)>, // (Name, Resource, Compress)
}

impl<'a> ResourceContainer<'a> {
    /// Creates a new empty container.
    pub fn new(output_path: &'a Path) -> Self {
        Self {
            output_path,
            resources: Vec::new(),
        }
    }

    /// Adds a resource to the container.
    pub fn add_resource(&mut self, name: &str, resource: Resource, compress: bool) {
        self.resources.push((name.to_string(), resource, compress));
    }

    /// Processes all resources and writes them to the embed directory.
    pub fn embed_all(&self) -> io::Result<()> {
        let mut byte_arrays = Vec::new();

        for (name, resource, compress) in &self.resources {
            let bytes = resource.fetch(*compress)?;
            byte_arrays.push((name.as_str(), bytes));
        }

        // Use `embed-bytes` to write the byte arrays
        embed_bytes::write_byte_arrays(self.output_path, byte_arrays)
    }
}