rust_embed_for_web_utils/
config.rs1#[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 #[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 pub fn set_zstd(&mut self, status: bool) {
68 self.zstd = status;
69 }
70
71 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 #[allow(unused_variables)]
95 pub fn should_include(&self, path: &str) -> bool {
96 #[cfg(feature = "include-exclude")]
97 {
98 self.include
100 .iter()
101 .any(|include| include.is_match(path))
102 || !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 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 pub fn allow_missing(&self) -> bool {
140 self.allow_missing
141 }
142}