rocket_include_static_resources/debug/
manager.rs

1use std::sync::{Mutex, PoisonError};
2
3use super::FileResources;
4use crate::{EtagIfNoneMatch, StaticResponse};
5
6/// To monitor the state of static resources.
7#[derive(Debug)]
8pub struct StaticContextManager {
9    pub resources: Mutex<FileResources>,
10}
11
12impl StaticContextManager {
13    #[inline]
14    pub(crate) fn new(resources: Mutex<FileResources>) -> StaticContextManager {
15        StaticContextManager {
16            resources,
17        }
18    }
19
20    /// Build a `StaticResponse`.
21    #[inline]
22    pub fn build<S: AsRef<str>>(
23        &self,
24        etag_if_none_match: &EtagIfNoneMatch<'_>,
25        name: S,
26    ) -> StaticResponse {
27        self.resources
28            .lock()
29            .unwrap_or_else(PoisonError::into_inner)
30            .get_resource(name.as_ref())
31            .map(|resource| {
32                if etag_if_none_match.weak_eq(resource.2) {
33                    StaticResponse::not_modified()
34                } else {
35                    StaticResponse::build(&resource.0, resource.1.clone(), resource.2)
36                }
37            })
38            .unwrap()
39    }
40}