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    zstd: bool,
13}
14
15impl Default for Config {
16    fn default() -> Self {
17        Self {
18            #[cfg(feature = "include-exclude")]
19            include: vec![],
20            #[cfg(feature = "include-exclude")]
21            exclude: vec![],
22            gzip: true,
23            br: true,
24            #[cfg(feature = "compression-zstd")]
25            zstd: true,
26            #[cfg(not(feature = "compression-zstd"))]
27            zstd: false,
28        }
29    }
30}
31
32impl Config {
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    // Builder functions
38    #[cfg(feature = "include-exclude")]
39    pub fn add_include(&mut self, pattern: String) {
40        self.include.push(
41            Glob::new(&pattern)
42                .expect("Failed to parse glob pattern for include")
43                .compile_matcher(),
44        );
45    }
46
47    #[cfg(feature = "include-exclude")]
48    pub fn add_exclude(&mut self, pattern: String) {
49        self.exclude.push(
50            Glob::new(&pattern)
51                .expect("Failed to parse glob pattern for exclude")
52                .compile_matcher(),
53        );
54    }
55
56    pub fn set_gzip(&mut self, status: bool) {
57        self.gzip = status;
58    }
59
60    pub fn set_br(&mut self, status: bool) {
61        self.br = status;
62    }
63
64    /// Enable or disable zstd compression for embedded files.
65    pub fn set_zstd(&mut self, status: bool) {
66        self.zstd = status;
67    }
68
69    #[cfg(feature = "include-exclude")]
70    pub fn get_includes(&self) -> &Vec<GlobMatcher> {
71        &self.include
72    }
73
74    #[cfg(feature = "include-exclude")]
75    pub fn get_excludes(&self) -> &Vec<GlobMatcher> {
76        &self.exclude
77    }
78
79    /// Check if a file at some path should be included based on this config.
80    ///
81    /// When deciding, includes always have priority over excludes. That means
82    /// you typically will list paths you want excluded, then add includes to
83    /// make an exception for some subset of files.
84    #[allow(unused_variables)]
85    pub fn should_include(&self, path: &str) -> bool {
86        #[cfg(feature = "include-exclude")]
87        {
88            // Includes have priority.
89            self.include
90            .iter()
91            .any(|include| include.is_match(path))
92            // If not, then we check if the file has been excluded. Any file
93            // that is not explicitly excluded will be 
94            || !self
95                .exclude
96                .iter()
97                .any(|exclude| exclude.is_match(path))
98        }
99        #[cfg(not(feature = "include-exclude"))]
100        {
101            true
102        }
103    }
104
105    pub fn should_gzip(&self) -> bool {
106        self.gzip
107    }
108
109    pub fn should_br(&self) -> bool {
110        self.br
111    }
112
113    /// Check if zstd compression should be used for embedded files.
114    ///
115    /// Returns `false` when the compression-zstd feature is not enabled,
116    /// even if the config value is set to `true`.
117    pub fn should_zstd(&self) -> bool {
118        #[cfg(feature = "compression-zstd")]
119        {
120            self.zstd
121        }
122        #[cfg(not(feature = "compression-zstd"))]
123        {
124            false
125        }
126    }
127}