From 4449258e0a0927ffd0d92f79db1f49341fbbecda Mon Sep 17 00:00:00 2001 From: Asen Alexandrov Date: Fri, 26 May 2023 11:28:58 +0300 Subject: [PATCH] chore: Address review comments by @Angelmmiguel and @ereslibre --- Cargo.lock | 40 +-------------------------------- crates/router/Cargo.toml | 4 +++- crates/router/src/files.rs | 45 ++++++++++++++++++++------------------ crates/router/src/lib.rs | 2 +- src/main.rs | 2 +- 5 files changed, 30 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af13cff1..fb03120d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,15 +426,6 @@ dependencies = [ "alloc-stdlib", ] -[[package]] -name = "brownstone" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5839ee4f953e811bfdcf223f509cb2c6a3e1447959b0bff459405575bc17f22" -dependencies = [ - "arrayvec", -] - [[package]] name = "bstr" version = "0.2.17" @@ -1373,12 +1364,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indent_write" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cfe9645a18782869361d9c8732246be7b410ad4e919d3609ebabdac00ba12c3" - [[package]] name = "indexmap" version = "1.9.2" @@ -1487,12 +1472,6 @@ dependencies = [ "libc", ] -[[package]] -name = "joinery" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72167d68f5fce3b8655487b8038691a3c9984ee769590f93f2a631f4ad64e4f5" - [[package]] name = "js-sys" version = "0.3.61" @@ -1704,19 +1683,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "nom-supreme" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bd3ae6c901f1959588759ff51c95d24b491ecb9ff91aa9c2ef4acc5b1dcab27" -dependencies = [ - "brownstone", - "indent_write", - "joinery", - "memchr", - "nom", -] - [[package]] name = "nom8" version = "0.2.0" @@ -3254,17 +3220,13 @@ dependencies = [ [[package]] name = "wax" version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06c7a3bac6110ac062b7b422a442b7ee23e07209e2784a036654cab1e71bbafc" +source = "git+https://github.com/olson-sean-k/wax.git#800917d6e40b0d2b9066ceda1671ea8c57504d99" dependencies = [ - "bstr", "const_format", "itertools", "nom", - "nom-supreme", "pori", "regex", - "smallvec", "thiserror", "walkdir", ] diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml index b0bb70b2..7ab877ba 100644 --- a/crates/router/Cargo.toml +++ b/crates/router/Cargo.toml @@ -16,7 +16,9 @@ wws-runtimes-manager = { workspace = true } wws-worker = { workspace = true } lazy_static = "1.4.0" regex = "1" -wax = "0.5.0" + +# This commit fixes an issue with Walk::not, and is not yet released +wax = { git = "https://github.com/olson-sean-k/wax.git", ref = "6d66a10" } [dev-dependencies] path-slash = "0.2.1" diff --git a/crates/router/src/files.rs b/crates/router/src/files.rs index bb0820f0..2a5de25c 100644 --- a/crates/router/src/files.rs +++ b/crates/router/src/files.rs @@ -12,16 +12,17 @@ const IGNORE_PATH_PREFIX: &str = "_"; /// It uses glob patterns to detect the workers and /// provide utilities to work with public folders and /// other related resources. -pub struct Files { +pub struct Files<'t> { /// Root path root: PathBuf, /// Defines pattern for files considered as workers - include_pattern: String, + include_pattern: Glob<'t>, /// Defines patterns to exclude when traversing for workers - ignore_patterns: Vec, + ignore_patterns: Vec>, } -impl Files { +impl<'t> Files<'t> { + const PUBLIC_ASSETS_FOLDER: &str = "public"; const DEFAULT_EXTENSIONS: [&str; 2] = ["js", "wasm"]; /// Initializes a new files instance. It will detect @@ -29,45 +30,47 @@ impl Files { pub fn new(root: &Path, file_extensions: Vec, ignore_patterns: Vec) -> Self { Self { root: root.to_path_buf(), - include_pattern: Self::construct_include_pattern(file_extensions), - ignore_patterns: Self::construct_ignore_patterns(ignore_patterns), + include_pattern: Self::build_include_pattern(file_extensions), + ignore_patterns: Self::build_ignore_patterns(ignore_patterns), } } /// Walk through all the different files associated to this /// project using a Glob pattern pub fn walk(&self) -> Vec { - let include_pattern = Glob::from_str(self.include_pattern.as_str()).expect( - "Failed to parse include pattern when processing files in the current directory", - ); - - return include_pattern + return self + .include_pattern .walk(&self.root) - .not(self.ignore_patterns.iter().map(|s| s.as_str())) - .expect( - "Failed to parse ignore patterns when processing files in the current directory", - ) + .not(self.ignore_patterns.clone()) + .expect("Failed to walk the tree when processing files in the current directory") .map(|e| e.unwrap()) .collect(); } - fn construct_include_pattern(file_extensions: Vec) -> String { + fn build_include_pattern(file_extensions: Vec) -> Glob<'t> { let mut file_extensions = file_extensions; for default_extension in Self::DEFAULT_EXTENSIONS { file_extensions.push(default_extension.to_string()); } - format!("**/*.{{{}}}", file_extensions.join(",")) + let include_pattern = format!("**/*.{{{}}}", file_extensions.join(",")); + Glob::from_str(include_pattern.as_str()) + .expect(format!("Failed to parse include pattern '{}'!", include_pattern).as_str()) } - fn construct_ignore_patterns(ignore_patterns: Vec) -> Vec { - let mut result = vec![ - "**/public/**".to_string(), + fn build_ignore_patterns(ignore_patterns: Vec) -> Vec> { + let default_ignore_patterns = vec![ + format!("**/{}/**", Self::PUBLIC_ASSETS_FOLDER), format!("**/{}/**", STORE_FOLDER), format!("**/{}*/**", IGNORE_PATH_PREFIX), ]; + + let mut result = default_ignore_patterns; result.extend(ignore_patterns); result + .iter() + .map(|s| Glob::from_str(s.as_str()).expect("Failed to parse ignore pattern")) + .collect() } } @@ -177,7 +180,7 @@ mod tests { } #[test] - fn walk_ignore2() { + fn walk_ignore_multiple_patterns() { let files = Files::new( Path::new("tests/data/files"), vec!["ext".to_string(), "none".to_string()], diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rs index e60d4a9c..2bc134bd 100644 --- a/crates/router/src/lib.rs +++ b/crates/router/src/lib.rs @@ -12,7 +12,7 @@ mod route; use files::Files; use route::{Route, RouteAffinity}; use std::path::{Path, PathBuf}; -use std::time::{Instant}; +use std::time::Instant; use wws_config::Config; /// Contains all registered routes diff --git a/src/main.rs b/src/main.rs index 964d5499..9b9578cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ pub struct Args { prefix: String, /// Patterns to ignore when looking for worker files - #[arg(long, default_value = "", value_delimiter=';', num_args= 0..)] + #[arg(long, default_value = "")] ignore: Vec, /// Manage language runtimes in your project