use crate::{ResourceId, ResourceRegistry};
pub trait ResourceBundle {
fn get(&self, id: ResourceId) -> Option<&'static [u8]>;
fn contains(&self, id: ResourceId) -> bool {
self.get(id).is_some()
}
}
#[derive(Copy, Clone, Debug)]
pub struct EmbeddedBundle {
entries: &'static [(ResourceId, &'static [u8])],
}
impl EmbeddedBundle {
pub const fn new(entries: &'static [(ResourceId, &'static [u8])]) -> Self {
Self { entries }
}
pub fn register_with(&self, registry: &mut ResourceRegistry) {
for (id, bytes) in self.entries {
registry.register_bytes(*id, bytes);
}
}
}
impl ResourceBundle for EmbeddedBundle {
fn get(&self, id: ResourceId) -> Option<&'static [u8]> {
for (entry_id, bytes) in self.entries {
if *entry_id == id {
return Some(bytes);
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::generated::EMBEDDED;
#[test]
fn embedded_png_roundtrip() {
let expected: &[u8] =
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/resources/test.png"));
let id = ResourceId::from_bytes_le(b"test.png");
let bytes = EMBEDDED.get(id).expect("test.png should be embedded");
assert_eq!(bytes, expected);
}
}