Organize code into separate files

This commit is contained in:
Cameron Cordes
2020-05-19 22:44:26 -04:00
parent 0daa2f4149
commit 263dad91da
6 changed files with 99 additions and 74 deletions

16
Cargo.lock generated
View File

@@ -69,9 +69,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.33.0" version = "2.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"atty", "atty",
@@ -134,9 +134,9 @@ dependencies = [
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.12" version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@@ -149,9 +149,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.69" version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f"
[[package]] [[package]]
name = "rack" name = "rack"
@@ -223,9 +223,9 @@ checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.1" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "wasi" name = "wasi"

View File

@@ -7,3 +7,6 @@ edition = "2018"
[dependencies] [dependencies]
clap = "2.33.0" clap = "2.33.0"
term = "0.6.1" term = "0.6.1"
[profile.release]
lto = true

28
src/filematch.rs Normal file
View File

@@ -0,0 +1,28 @@
use super::config::Config;
use super::printer::*;
#[derive(Debug)]
pub struct FileMatch {
pub file_name: String,
pub matches: Vec<Match>,
}
impl FileMatch {
pub fn print(&self, config: &Config) {
let cp = ColorPrinter {};
cp.print(&config, &self);
}
}
#[derive(Debug, Clone)]
pub struct Match {
pub line_number: usize,
pub match_indexes: Vec<MatchIndex>,
pub text: String,
}
#[derive(Debug, Clone)]
pub struct MatchIndex {
pub start: usize,
pub end: usize,
}

3
src/lib.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod config;
pub mod filematch;
pub mod printer;

View File

@@ -1,3 +1,5 @@
use rack::config::Config;
use rack::filematch::*;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
@@ -5,10 +7,8 @@ use std::path::Path;
extern crate term; extern crate term;
mod config;
fn main() { fn main() {
let config = config::Config::from_args(); let config = Config::from_args();
let search_path = &config.path; let search_path = &config.path;
let path = match search_path.starts_with('/') { let path = match search_path.starts_with('/') {
@@ -22,15 +22,15 @@ fn main() {
let results = traverse_dir(&config, path, |config, path| check_file(config, path)); let results = traverse_dir(&config, path, |config, path| check_file(config, path));
match results { match results {
Result::Ok(matches) if matches.len() == 0 => println!("No matches :("), Result::Ok(matches) if matches.len() == 0 => println!("No matches :("),
Result::Ok(matches) => print_report(&matches), Result::Ok(matches) => print_report(&config, &matches),
Result::Err(msg) => println!("Error while parsing: {}", msg), Result::Err(msg) => println!("Error while parsing: {}", msg),
} }
} }
fn traverse_dir( fn traverse_dir(
config: &config::Config, config: &Config,
dir: &Path, dir: &Path,
operation: fn(&config::Config, &Path) -> io::Result<FileMatch>, operation: fn(&Config, &Path) -> io::Result<FileMatch>,
) -> io::Result<Vec<FileMatch>> { ) -> io::Result<Vec<FileMatch>> {
let mut matches: Vec<FileMatch> = Vec::new(); let mut matches: Vec<FileMatch> = Vec::new();
for entry in fs::read_dir(dir)? { for entry in fs::read_dir(dir)? {
@@ -60,64 +60,7 @@ fn traverse_dir(
Ok(matches) Ok(matches)
} }
#[derive(Debug)] fn check_file(config: &Config, path: &Path) -> io::Result<FileMatch> {
struct FileMatch {
file_name: String,
matches: Vec<Match>,
}
impl FileMatch {
fn print(&self) {
let mut term = term::stdout().unwrap();
term.fg(term::color::YELLOW).unwrap();
term.attr(term::Attr::Bold).unwrap();
println!("{}:", self.file_name);
term.reset().unwrap();
for mat in &self.matches {
term.attr(term::Attr::Bold).unwrap();
print!("{}: ", mat.line_number);
term.reset().unwrap();
let mut last_position = 0;
for (idx, match_index) in mat.match_indexes.iter().enumerate() {
print!(
"{}",
mat.text.get(last_position..match_index.start).unwrap()
);
term.fg(term::color::BRIGHT_RED).unwrap();
print!(
"{}",
mat.text.get(match_index.start..match_index.end).unwrap()
);
term.reset().unwrap();
if idx == mat.match_indexes.len() - 1 {
println!("{}", mat.text.get(match_index.end..mat.text.len()).unwrap());
last_position = 0;
} else {
last_position = match_index.end;
}
}
}
println!();
}
}
#[derive(Debug, Clone)]
struct Match {
line_number: usize,
match_indexes: Vec<MatchIndex>,
text: String,
}
#[derive(Debug, Clone)]
struct MatchIndex {
start: usize,
end: usize,
}
fn check_file(config: &config::Config, path: &Path) -> io::Result<FileMatch> {
let mut pattern = config.pattern.clone(); let mut pattern = config.pattern.clone();
let file = File::open(&path)?; let file = File::open(&path)?;
@@ -155,8 +98,8 @@ fn check_file(config: &config::Config, path: &Path) -> io::Result<FileMatch> {
}) })
} }
fn print_report(matches: &Vec<FileMatch>) { fn print_report(config: &Config, matches: &Vec<FileMatch>) {
for file in matches { for file in matches {
file.print() file.print(config)
} }
} }

48
src/printer.rs Normal file
View File

@@ -0,0 +1,48 @@
extern crate term;
use super::config::Config;
use super::filematch::FileMatch;
pub trait Printer {
fn print(&self, config: &Config, file_match: &FileMatch);
}
pub struct ColorPrinter;
impl Printer for ColorPrinter {
fn print(&self, _config: &Config, mat: &FileMatch) {
let mut term = term::stdout().unwrap();
term.fg(term::color::YELLOW).unwrap();
term.attr(term::Attr::Bold).unwrap();
println!("{}:", mat.file_name);
term.reset().unwrap();
for mat in &mat.matches {
term.attr(term::Attr::Bold).unwrap();
print!("{}: ", mat.line_number);
term.reset().unwrap();
let mut last_position = 0;
for (idx, match_index) in mat.match_indexes.iter().enumerate() {
print!(
"{}",
mat.text.get(last_position..match_index.start).unwrap()
);
term.fg(term::color::BRIGHT_RED).unwrap();
print!(
"{}",
mat.text.get(match_index.start..match_index.end).unwrap()
);
term.reset().unwrap();
if idx == mat.match_indexes.len() - 1 {
println!("{}", mat.text.get(match_index.end..mat.text.len()).unwrap());
last_position = 0;
} else {
last_position = match_index.end;
}
}
}
println!();
}
}