|
| 1 | +use super::{Filter, canonicalize}; |
| 2 | + |
| 3 | +pub fn invert(filter: &Filter) -> Filter { |
| 4 | + let inverted = match filter { |
| 5 | + Filter::Subdir(path) => Filter::Prefix(path.clone()), |
| 6 | + Filter::Prefix(path) => Filter::Subdir(path.clone()), |
| 7 | + Filter::Exclude(selectors) => Filter::Exclude(selectors.clone()), |
| 8 | + Filter::Compose(members) => Filter::Compose(members.iter().map(invert).collect()), |
| 9 | + Filter::Chain(ops) => Filter::Chain(ops.iter().rev().map(invert).collect()), |
| 10 | + Filter::Nop => Filter::Nop, |
| 11 | + Filter::Empty => Filter::Empty, |
| 12 | + }; |
| 13 | + canonicalize(inverted) |
| 14 | +} |
| 15 | + |
| 16 | +/// Computes the exact source paths for a filter already accepted for registration. |
| 17 | +/// |
| 18 | +/// Callers handling request-supplied filters must validate them before calling this |
| 19 | +/// function, because the exact result itself can be large for an unvalidated AST. |
| 20 | +pub fn src_paths(filter: &Filter) -> Vec<String> { |
| 21 | + let mut paths = normalized_paths(pull(filter, vec![Vec::new()])) |
| 22 | + .into_iter() |
| 23 | + .map(|path| display_path(&path)) |
| 24 | + .collect::<Vec<_>>(); |
| 25 | + paths.sort(); |
| 26 | + paths |
| 27 | +} |
| 28 | + |
| 29 | +pub(crate) fn src_paths_limited( |
| 30 | + filter: &Filter, |
| 31 | + max_paths: usize, |
| 32 | +) -> Result<Vec<String>, SrcPathLimitExceeded> { |
| 33 | + let paths = pull_limited(filter, vec![Vec::new()], max_paths)?; |
| 34 | + let paths = normalized_paths(paths); |
| 35 | + if paths.len() > max_paths { |
| 36 | + return Err(SrcPathLimitExceeded); |
| 37 | + } |
| 38 | + let mut paths = paths |
| 39 | + .into_iter() |
| 40 | + .map(|path| display_path(&path)) |
| 41 | + .collect::<Vec<_>>(); |
| 42 | + paths.sort(); |
| 43 | + Ok(paths) |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Clone, Copy, Debug)] |
| 47 | +pub(crate) struct SrcPathLimitExceeded; |
| 48 | + |
| 49 | +fn normalized_paths(mut paths: Vec<Vec<String>>) -> Vec<Vec<String>> { |
| 50 | + paths.sort(); |
| 51 | + paths.dedup(); |
| 52 | + let mut minimal: Vec<Vec<String>> = Vec::new(); |
| 53 | + for path in paths { |
| 54 | + if !minimal.iter().any(|ancestor| is_prefix(ancestor, &path)) { |
| 55 | + minimal.push(path); |
| 56 | + } |
| 57 | + } |
| 58 | + minimal |
| 59 | +} |
| 60 | + |
| 61 | +fn pull_limited( |
| 62 | + filter: &Filter, |
| 63 | + paths: Vec<Vec<String>>, |
| 64 | + max_paths: usize, |
| 65 | +) -> Result<Vec<Vec<String>>, SrcPathLimitExceeded> { |
| 66 | + let paths = match filter { |
| 67 | + Filter::Subdir(path) => paths |
| 68 | + .into_iter() |
| 69 | + .map(|suffix| join(path.segments(), &suffix)) |
| 70 | + .collect(), |
| 71 | + Filter::Prefix(path) => paths |
| 72 | + .into_iter() |
| 73 | + .filter_map(|path_at_output| pull_prefix(path.segments(), &path_at_output)) |
| 74 | + .collect(), |
| 75 | + Filter::Exclude(_) | Filter::Nop => paths, |
| 76 | + Filter::Empty => Vec::new(), |
| 77 | + Filter::Compose(members) => { |
| 78 | + let mut output = Vec::new(); |
| 79 | + for member in members { |
| 80 | + output.extend(pull_limited(member, paths.clone(), max_paths)?); |
| 81 | + output = normalized_paths(output); |
| 82 | + if output.len() > max_paths { |
| 83 | + return Err(SrcPathLimitExceeded); |
| 84 | + } |
| 85 | + } |
| 86 | + output |
| 87 | + } |
| 88 | + Filter::Chain(ops) => { |
| 89 | + let mut output = paths; |
| 90 | + for op in ops.iter().rev() { |
| 91 | + output = pull_limited(op, output, max_paths)?; |
| 92 | + output = normalized_paths(output); |
| 93 | + if output.len() > max_paths { |
| 94 | + return Err(SrcPathLimitExceeded); |
| 95 | + } |
| 96 | + } |
| 97 | + output |
| 98 | + } |
| 99 | + }; |
| 100 | + let paths = normalized_paths(paths); |
| 101 | + if paths.len() > max_paths { |
| 102 | + return Err(SrcPathLimitExceeded); |
| 103 | + } |
| 104 | + Ok(paths) |
| 105 | +} |
| 106 | + |
| 107 | +fn pull(filter: &Filter, paths: Vec<Vec<String>>) -> Vec<Vec<String>> { |
| 108 | + let paths = match filter { |
| 109 | + Filter::Subdir(path) => paths |
| 110 | + .into_iter() |
| 111 | + .map(|suffix| join(path.segments(), &suffix)) |
| 112 | + .collect(), |
| 113 | + Filter::Prefix(path) => paths |
| 114 | + .into_iter() |
| 115 | + .filter_map(|path_at_output| pull_prefix(path.segments(), &path_at_output)) |
| 116 | + .collect(), |
| 117 | + Filter::Exclude(_) | Filter::Nop => paths, |
| 118 | + Filter::Empty => Vec::new(), |
| 119 | + Filter::Compose(members) => { |
| 120 | + let mut output = Vec::new(); |
| 121 | + for member in members { |
| 122 | + output.extend(pull(member, paths.clone())); |
| 123 | + output = normalized_paths(output); |
| 124 | + } |
| 125 | + output |
| 126 | + } |
| 127 | + Filter::Chain(ops) => { |
| 128 | + let mut output = paths; |
| 129 | + for op in ops.iter().rev() { |
| 130 | + output = normalized_paths(pull(op, output)); |
| 131 | + } |
| 132 | + output |
| 133 | + } |
| 134 | + }; |
| 135 | + normalized_paths(paths) |
| 136 | +} |
| 137 | + |
| 138 | +fn pull_prefix(prefix: &[String], output: &[String]) -> Option<Vec<String>> { |
| 139 | + if is_prefix(prefix, output) { |
| 140 | + return Some(output[prefix.len()..].to_vec()); |
| 141 | + } |
| 142 | + if is_prefix(output, prefix) { |
| 143 | + return Some(Vec::new()); |
| 144 | + } |
| 145 | + None |
| 146 | +} |
| 147 | + |
| 148 | +fn join(prefix: &[String], suffix: &[String]) -> Vec<String> { |
| 149 | + prefix.iter().chain(suffix).cloned().collect::<Vec<_>>() |
| 150 | +} |
| 151 | + |
| 152 | +pub(crate) fn is_prefix(ancestor: &[String], descendant: &[String]) -> bool { |
| 153 | + ancestor.len() <= descendant.len() |
| 154 | + && ancestor |
| 155 | + .iter() |
| 156 | + .zip(descendant) |
| 157 | + .all(|(left, right)| left == right) |
| 158 | +} |
| 159 | + |
| 160 | +pub(crate) fn display_path(path: &[String]) -> String { |
| 161 | + if path.is_empty() { |
| 162 | + "/".to_owned() |
| 163 | + } else { |
| 164 | + format!("/{}", path.join("/")) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +pub(crate) fn path_segments(path: &str) -> Vec<String> { |
| 169 | + path.strip_prefix('/') |
| 170 | + .filter(|path| !path.is_empty()) |
| 171 | + .map(|path| path.split('/').map(ToOwned::to_owned).collect()) |
| 172 | + .unwrap_or_default() |
| 173 | +} |
| 174 | + |
| 175 | +pub(crate) fn all_compose_source_paths_limited( |
| 176 | + filter: &Filter, |
| 177 | + max_paths: usize, |
| 178 | +) -> Result<Vec<Vec<Vec<String>>>, SrcPathLimitExceeded> { |
| 179 | + let mut groups = Vec::new(); |
| 180 | + collect_compose_source_paths_limited(filter, max_paths, &mut groups)?; |
| 181 | + Ok(groups) |
| 182 | +} |
| 183 | + |
| 184 | +fn collect_compose_source_paths_limited( |
| 185 | + filter: &Filter, |
| 186 | + max_paths: usize, |
| 187 | + groups: &mut Vec<Vec<Vec<String>>>, |
| 188 | +) -> Result<(), SrcPathLimitExceeded> { |
| 189 | + match filter { |
| 190 | + Filter::Compose(members) => { |
| 191 | + let mut paths = Vec::new(); |
| 192 | + for member in members { |
| 193 | + paths.extend( |
| 194 | + src_paths_limited(member, max_paths)? |
| 195 | + .into_iter() |
| 196 | + .map(|path| path_segments(&path)), |
| 197 | + ); |
| 198 | + } |
| 199 | + groups.push(paths); |
| 200 | + for member in members { |
| 201 | + collect_compose_source_paths_limited(member, max_paths, groups)?; |
| 202 | + } |
| 203 | + } |
| 204 | + Filter::Chain(ops) => { |
| 205 | + for op in ops { |
| 206 | + collect_compose_source_paths_limited(op, max_paths, groups)?; |
| 207 | + } |
| 208 | + } |
| 209 | + _ => {} |
| 210 | + } |
| 211 | + Ok(()) |
| 212 | +} |
| 213 | + |
| 214 | +#[cfg(test)] |
| 215 | +mod tests { |
| 216 | + use super::{ |
| 217 | + super::{canonicalize, parse, print}, |
| 218 | + *, |
| 219 | + }; |
| 220 | + |
| 221 | + fn filter(text: &str) -> Filter { |
| 222 | + canonicalize(parse(text).unwrap()) |
| 223 | + } |
| 224 | + |
| 225 | + #[test] |
| 226 | + fn invert_table() { |
| 227 | + for (input, expected) in [ |
| 228 | + (":/p", ":prefix=p"), |
| 229 | + (":prefix=p", ":/p"), |
| 230 | + (":nop", ":nop"), |
| 231 | + (":empty", ":empty"), |
| 232 | + (":/a:prefix=x", ":/x:prefix=a"), |
| 233 | + ( |
| 234 | + ":[:/a:prefix=x,:/b:prefix=y]", |
| 235 | + ":[:/x:prefix=a,:/y:prefix=b]", |
| 236 | + ), |
| 237 | + (":exclude[::b/]", ":exclude[::b/]"), |
| 238 | + (":/a:exclude[::b/]", ":exclude[::b/]:prefix=a"), |
| 239 | + ] { |
| 240 | + assert_eq!(print(&invert(&filter(input))), expected, "{input}"); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + #[test] |
| 245 | + fn src_paths_examples() { |
| 246 | + for (input, expected) in [ |
| 247 | + (":exclude[::secret]", vec!["/"]), |
| 248 | + (":/a:exclude[::b/]", vec!["/a"]), |
| 249 | + (":prefix=x", vec!["/"]), |
| 250 | + (":/a:prefix=x", vec!["/a"]), |
| 251 | + (":/a:[:/b:prefix=x,:/c:prefix=y]", vec!["/a/b", "/a/c"]), |
| 252 | + (":[:/a:prefix=x,:/b:prefix=y]:/x", vec!["/a"]), |
| 253 | + (":prefix=x:[:/w:prefix=b,:/z:prefix=a]", Vec::<&str>::new()), |
| 254 | + (":exclude[::a]:/a", vec!["/a"]), |
| 255 | + (":nop", vec!["/"]), |
| 256 | + (":[:/B:prefix=b,:/a:prefix=a]", vec!["/B", "/a"]), |
| 257 | + (":[:/a/b:prefix=x,:/a.c:prefix=y]", vec!["/a.c", "/a/b"]), |
| 258 | + ] { |
| 259 | + assert_eq!(src_paths(&filter(input)), expected, "{input}"); |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments