Skip to main content

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