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
37
38
39
use std::collections::HashMap;
use std::path::Path;

use crate::{Mime, EntityTag};
use crate::functions::{compute_data_etag, guess_mime};

#[derive(Debug)]
/// Static resources.
pub struct StaticResources {
    resources: HashMap<&'static str, (Mime, &'static [u8], EntityTag)>
}

impl StaticResources {
    #[inline]
    /// Create an instance of `StaticResources`.
    pub fn new() -> StaticResources {
        StaticResources {
            resources: HashMap::new()
        }
    }

    #[inline]
    /// Register a static resource.
    pub fn register_resource_static<P: AsRef<Path>>(&mut self, name: &'static str, path: P, data: &'static [u8]) {
        let etag = compute_data_etag(data);

        let mime = guess_mime(path);

        self.resources.insert(name, (mime, data, etag));
    }

    #[inline]
    /// Get the specific resource.
    pub fn get_resource<S: AsRef<str>>(&self, name: S) -> Option<(&Mime, &'static [u8], &EntityTag)> {
        let name = name.as_ref();

        self.resources.get(name).map(|(mime, data, etag)| (mime, *data, etag))
    }
}