Skip to content

Commit

Permalink
Merge pull request #9 from avencera/explore-parallel
Browse files Browse the repository at this point in the history
Use rayon crate to change files in parallel
  • Loading branch information
praveenperera authored Nov 29, 2019
2 parents 322853c + 33f6509 commit 5d5f871
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 19 deletions.
133 changes: 133 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lazy_static = "1.4.0"
itertools = "0.8"
indoc = "0.3"
clap = "2.33"

rayon = "1.2"


[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ Any and all feedback and criticism is very welcome. If im doing anything stupid
1. Add `--allow-duplicates` to allow duplicate class name
2. ~~Add `--dry-run` option to print out all changes to console~~ [c975d1](https://github.com/avencera/rustywind/commit/c975d1f041f95b45e15760ccded24dbf62bf1f6f)

### Phase 4 (Performance)
### ~~Phase 4 (Performance)~~

1. Search and change files in parallel (parallel map?)
1. ~~Search and change files in parallel (parallel map?)~~ [#9](https://github.com/avencera/rustywind/pull/9)

### Later Possibilities...

Expand Down
37 changes: 21 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clap::{App, AppSettings, Arg};
use clap::{App, AppSettings, Arg, ArgMatches};
use ignore::WalkBuilder;
use indoc::indoc;
use rayon::prelude::*;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};

fn main() {
let matches = App::new("Rusty Wind")
Expand Down Expand Up @@ -57,28 +58,32 @@ fn main() {
),
}

let walker = WalkBuilder::new(&file_or_dir)
let mut file_paths: Vec<PathBuf> = vec![];
WalkBuilder::new(&file_or_dir)
.build()
.filter_map(Result::ok)
.filter(|f| f.path().is_file());
.filter(|f| f.path().is_file())
.for_each(|file| file_paths.push(file.path().to_owned()));

for file in walker {
let file_path = file.path();
file_paths
.par_iter()
.for_each(|file_path| run_on_file_paths(&file_path, &matches, file_or_dir))
}

match fs::read_to_string(file_path) {
Ok(contents) => {
if rustywind::has_classes(&contents) {
let sorted_content = rustywind::sort_file_contents(contents);
fn run_on_file_paths(file_path: &Path, matches: &ArgMatches, file_or_dir: &Path) {
match fs::read_to_string(file_path) {
Ok(contents) => {
if rustywind::has_classes(&contents) {
let sorted_content = rustywind::sort_file_contents(contents);

match (matches.is_present("write"), matches.is_present("dry_run")) {
(_, true) => print_file_name(file_path, file_or_dir),
(true, false) => write_to_file(file_path, file_or_dir, &sorted_content),
_ => print_file_contents(&sorted_content),
}
match (matches.is_present("write"), matches.is_present("dry_run")) {
(_, true) => print_file_name(file_path, file_or_dir),
(true, false) => write_to_file(file_path, file_or_dir, &sorted_content),
_ => print_file_contents(&sorted_content),
}
}
Err(_error) => (),
}
Err(_error) => (),
}
}

Expand Down

0 comments on commit 5d5f871

Please sign in to comment.