rust_embed_for_web_utils/
config.rs

1#[cfg(feature = "include-exclude")]
2use globset::{Glob, GlobMatcher};
3
4#[derive(Debug)]
5pub struct Config {
6    #[cfg(feature = "include-exclude")]
7    include: Vec<GlobMatcher>,
8    #[cfg(feature = "include-exclude")]
9    exclude: Vec<GlobMatcher>,
10    gzip: bool,
11    br: bool,
12}
13
14impl Default for Config {
15    fn default() -> Self {
16        Self {
17            #[cfg(feature = "include-exclude")]
18            include: vec![],
19            #[cfg(feature = "include-exclude")]
20            exclude: vec![],
21            gzip: true,
22            br: true,
23        }
24    }
25}
26
27impl Config {
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    // Builder functions
33    #[cfg(feature = "include-exclude")]
34    pub fn add_include(&mut self, pattern: String) {
35        self.include.push(
36            Glob::new(&pattern)
37                .expect("Failed to parse glob pattern for include")
38                .compile_matcher(),
39        );
40    }
41
42    #[cfg(feature = "include-exclude")]
43    pub fn add_exclude(&mut self, pattern: String) {
44        self.exclude.push(
45            Glob::new(&pattern)
46                .expect("Failed to parse glob pattern for exclude")
47                .compile_matcher(),
48        );
49    }
50
51    pub fn set_gzip(&mut self, status: bool) {
52        self.gzip = status;
53    }
54
55    pub fn set_br(&mut self, status: bool) {
56        self.br = status;
57    }
58
59    #[cfg(feature = "include-exclude")]
60    pub fn get_includes(&self) -> &Vec<GlobMatcher> {
61        &self.include
62    }
63
64    #[cfg(feature = "include-exclude")]
65    pub fn get_excludes(&self) -> &Vec<GlobMatcher> {
66        &self.exclude
67    }
68
69    /// Check if a file at some path should be included based on this config.
70    ///
71    /// When deciding, includes always have priority over excludes. That means
72    /// you typically will list paths you want excluded, then add includes to
73    /// make an exception for some subset of files.
74    #[allow(unused_variables)]
75    pub fn should_include(&self, path: &str) -> bool {
76        #[cfg(feature = "include-exclude")]
77        {
78            // Includes have priority.
79            self.include
80            .iter()
81            .any(|include| include.is_match(path))
82            // If not, then we check if the file has been excluded. Any file
83            // that is not explicitly excluded will be 
84            || !self
85                .exclude
86                .iter()
87                .any(|exclude| exclude.is_match(path))
88        }
89        #[cfg(not(feature = "include-exclude"))]
90        {
91            true
92        }
93    }
94
95    pub fn should_gzip(&self) -> bool {
96        self.gzip
97    }
98
99    pub fn should_br(&self) -> bool {
100        self.br
101    }
102}