bracket_embedding/
embedding.rs

1use lazy_static::lazy_static;
2use parking_lot::Mutex;
3use std::collections::HashMap;
4
5// These are included by default in `bracket-terminal`.
6const TERMINAL_8_8_BYTES: &[u8] =
7    include_bytes!("../resources/terminal8x8.png");
8const TERMINAL_8_16_BYTES: &[u8] = include_bytes!("../resources/vga8x16.png");
9
10lazy_static! {
11    pub static ref EMBED: Mutex<Dictionary> = Mutex::new(Dictionary::new());
12}
13
14/// Stores a dictionary of resources, generally added via `embedded_resource!` and `link_resource!` macros.
15#[derive(Default)]
16pub struct Dictionary {
17    entries: HashMap<String, &'static [u8]>,
18}
19
20impl Dictionary {
21    /// Create a new, empty dictionary.
22    #[must_use]
23    pub fn new() -> Dictionary {
24        let mut dict = Dictionary {
25            entries: HashMap::new(),
26        };
27        dict.add_resource("resources/terminal8x8.png".to_string(), TERMINAL_8_8_BYTES);
28        dict.add_resource("resources/vga8x16.png".to_string(), TERMINAL_8_16_BYTES);
29        dict
30    }
31
32    /// Request a resource, returning either a byte array or `None`.
33    #[must_use]
34    pub fn get_resource(&self, path: String) -> Option<&'static [u8]> {
35        let fixed_path = if std::path::MAIN_SEPARATOR == '/' {
36            path 
37        } else {
38            path.replace(std::path::MAIN_SEPARATOR, "/")
39        };
40
41        if self.entries.contains_key(&fixed_path) {
42            return Some(self.entries[&fixed_path]);
43        }
44        None
45    }
46
47    /// Insert a resource into the dictionary.
48    pub fn add_resource(&mut self, path: String, bytes: &'static [u8]) {
49        self.entries.insert(path, bytes);
50    }
51}