Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Nov 28, 2023
1 parent a6ba021 commit 7d73e73
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 149 deletions.
7 changes: 3 additions & 4 deletions src/ambr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use amber::pipeline_replacer::PipelineReplacer;
use amber::pipeline_sorter::PipelineSorter;
use amber::util::{as_secsf64, decode_error, exit, read_from_file};
use crossbeam::channel::unbounded;
use dirs;
use lazy_static::lazy_static;
use serde::Deserialize;
use std::cmp;
Expand Down Expand Up @@ -357,7 +356,7 @@ fn main() {
let keyword = if opt.key_from_file {
match read_from_file(&opt.keyword) {
Ok(x) => {
if x.len() != 0 {
if !x.is_empty() {
x
} else {
console.write(
Expand Down Expand Up @@ -562,12 +561,12 @@ fn main() {
}

if opt.statistics {
console.write(ConsoleTextKind::Info, &format!("\nStatistics\n"));
console.write(ConsoleTextKind::Info, "\nStatistics\n");
console.write(
ConsoleTextKind::Info,
&format!(" Max threads: {}\n\n", opt.max_threads),
);
console.write(ConsoleTextKind::Info, &format!(" Consumed time ( busy / total )\n"));
console.write(ConsoleTextKind::Info, " Consumed time ( busy / total )\n");
console.write(
ConsoleTextKind::Info,
&format!(" Find : {}s / {}s\n", sec_finder_bsy, sec_finder_all),
Expand Down
7 changes: 3 additions & 4 deletions src/ambs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use amber::pipeline_printer::PipelinePrinter;
use amber::pipeline_sorter::PipelineSorter;
use amber::util::{as_secsf64, decode_error, exit, read_from_file};
use crossbeam::channel::unbounded;
use dirs;
use lazy_static::lazy_static;
use serde::Deserialize;
use std::cmp;
Expand Down Expand Up @@ -335,7 +334,7 @@ fn main() {
let keyword = if opt.key_from_file {
match read_from_file(&opt.keyword) {
Ok(x) => {
if x.len() != 0 {
if !x.is_empty() {
x
} else {
console.write(
Expand Down Expand Up @@ -520,12 +519,12 @@ fn main() {
let sec_matcher_all = time_matcher_all.into_iter().map(as_secsf64).collect::<Vec<_>>();

if opt.statistics {
console.write(ConsoleTextKind::Info, &format!("\nStatistics\n"));
console.write(ConsoleTextKind::Info, "\nStatistics\n");
console.write(
ConsoleTextKind::Info,
&format!(" Max threads: {}\n\n", opt.max_threads),
);
console.write(ConsoleTextKind::Info, &format!(" Consumed time ( busy / total )\n"));
console.write(ConsoleTextKind::Info, " Consumed time ( busy / total )\n");
console.write(
ConsoleTextKind::Info,
&format!(" Find : {}s / {}s\n", sec_finder_bsy, sec_finder_all),
Expand Down
36 changes: 19 additions & 17 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub struct Console {
const CR: u8 = 0x0d;
const LF: u8 = 0x0a;

impl Default for Console {
fn default() -> Self {
Self::new()
}
}

impl Console {
pub fn new() -> Self {
Console {
Expand Down Expand Up @@ -163,22 +169,20 @@ impl Console {
if beg < m.beg {
self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[beg..m.beg]));
}
self.write(ConsoleTextKind::MatchText, &String::from_utf8_lossy(&rep));
self.write(ConsoleTextKind::MatchText, &String::from_utf8_lossy(rep));
if m.end < end {
self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[m.end..end]));
}
self.write(ConsoleTextKind::Text, "\n");
}

fn write_stdout(&mut self, val: &str, color: Color) {
if self.is_color {
if self.color_out != color {
self.term_stdout.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_out = color;
self.colored_out = true;
}
if self.is_color && self.color_out != color {
self.term_stdout.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_out = color;
self.colored_out = true;
}

write!(self.term_stdout, "{}", val).unwrap_or_else(|_| {
Expand All @@ -193,14 +197,12 @@ impl Console {
}

fn write_stderr(&mut self, val: &str, color: Color) {
if self.is_color {
if self.color_err != color {
self.term_stderr.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_err = color;
self.colored_err = true;
}
if self.is_color && self.color_err != color {
self.term_stderr.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_err = color;
self.colored_err = true;
}

write!(self.term_stderr, "{}", val).unwrap_or_else(|_| {
Expand Down
110 changes: 49 additions & 61 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use glob::{MatchOptions, Pattern};
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------------------------------------------------
// Ignore
// ---------------------------------------------------------------------------------------------------------------------

pub trait Ignore {
fn is_ignore(&self, path: &PathBuf, is_dir: bool) -> bool;
fn is_ignore(&self, path: &Path, is_dir: bool) -> bool;
}

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -20,6 +20,12 @@ pub struct IgnoreVcs {
vcs_dirs: Vec<String>,
}

impl Default for IgnoreVcs {
fn default() -> Self {
Self::new()
}
}

impl IgnoreVcs {
pub fn new() -> Self {
IgnoreVcs {
Expand All @@ -34,7 +40,7 @@ impl IgnoreVcs {
}

impl Ignore for IgnoreVcs {
fn is_ignore(&self, path: &PathBuf, is_dir: bool) -> bool {
fn is_ignore(&self, path: &Path, is_dir: bool) -> bool {
if is_dir {
for d in &self.vcs_dirs {
if path.ends_with(d) {
Expand Down Expand Up @@ -66,7 +72,7 @@ pub struct IgnoreGit {

impl IgnoreGit {
pub fn new(path: &PathBuf) -> Self {
let (f_name, f_path, d_name, d_path) = IgnoreGit::parse(&path);
let (f_name, f_path, d_name, d_path) = IgnoreGit::parse(path);
IgnoreGit {
file_name: f_name,
file_path: f_path,
Expand All @@ -93,7 +99,7 @@ impl IgnoreGit {
let mut dir_name = Vec::new();
let mut dir_path = Vec::new();

let f = if let Ok(x) = File::open(&path) {
let f = if let Ok(x) = File::open(path) {
x
} else {
return (file_name, file_path, dir_name, dir_path);
Expand All @@ -106,58 +112,42 @@ impl IgnoreGit {
let s = line.unwrap();
let s = s.trim().to_string();

if s == "" || s.starts_with("#") {
if s.is_empty() || s.starts_with('#') {
continue;
} else if s.starts_with("!") {
} else if s.starts_with('!') {
// not yet implemented
} else if !s.contains("/") {
} else if !s.contains('/') {
if let Ok(x) = Pattern::new(&s) {
let (head, tail) = IgnoreGit::extract_fix_pat(&s);
file_name.push(IgnoreGitPat {
pat: x.clone(),
head: head.clone(),
tail: tail.clone(),
head,
tail,
});
dir_name.push(IgnoreGitPat {
pat: x,
head: head.clone(),
tail: tail.clone(),
})
dir_name.push(IgnoreGitPat { pat: x, head, tail })
}
} else if s.ends_with("/") && s.find("/").unwrap() < s.len() - 1 {
} else if s.ends_with('/') && s.find('/').unwrap() < s.len() - 1 {
let p = IgnoreGit::concat_path(&base, &s);
if let Ok(x) = Pattern::new(&p) {
let (head, tail) = IgnoreGit::extract_fix_pat(&p);
dir_path.push(IgnoreGitPat {
pat: x,
head: head.clone(),
tail: tail.clone(),
})
dir_path.push(IgnoreGitPat { pat: x, head, tail })
}
} else if s.ends_with("/") {
} else if s.ends_with('/') {
let p = IgnoreGit::normalize(&s);
if let Ok(x) = Pattern::new(&p) {
let (head, tail) = IgnoreGit::extract_fix_pat(&p);
dir_name.push(IgnoreGitPat {
pat: x,
head: head.clone(),
tail: tail.clone(),
})
dir_name.push(IgnoreGitPat { pat: x, head, tail })
}
} else {
let p = IgnoreGit::concat_path(&base, &s);
if let Ok(x) = Pattern::new(&p) {
let (head, tail) = IgnoreGit::extract_fix_pat(&p);
file_path.push(IgnoreGitPat {
pat: x.clone(),
head: head.clone(),
tail: tail.clone(),
head,
tail,
});
dir_path.push(IgnoreGitPat {
pat: x,
head: head.clone(),
tail: tail.clone(),
})
dir_path.push(IgnoreGitPat { pat: x, head, tail })
}
}
}
Expand All @@ -166,7 +156,7 @@ impl IgnoreGit {
}

fn concat_path(s0: &str, s1: &str) -> String {
let ret = if s1.starts_with("/") {
let ret = if s1.starts_with('/') {
format!("{}{}", s0, s1)
} else {
format!("{}/{}", s0, s1)
Expand All @@ -175,7 +165,7 @@ impl IgnoreGit {
}

fn normalize(s: &str) -> String {
if s.ends_with("/") {
if s.ends_with('/') {
let mut s2 = String::from(s);
s2.truncate(s.len() - 1);
s2
Expand All @@ -187,14 +177,14 @@ impl IgnoreGit {
fn extract_fix_pat(p: &str) -> (u8, u8) {
let len = p.len();

let mut head_check = !p.starts_with("\\");
head_check &= !p.starts_with("*");
head_check &= !p.starts_with("?");
head_check &= !p.starts_with("[");
let mut head_check = !p.starts_with('\\');
head_check &= !p.starts_with('*');
head_check &= !p.starts_with('?');
head_check &= !p.starts_with('[');

let mut tail_check = !p.ends_with("*");
tail_check &= !p.ends_with("?");
tail_check &= !p.ends_with("]");
let mut tail_check = !p.ends_with('*');
tail_check &= !p.ends_with('?');
tail_check &= !p.ends_with(']');

let head = if head_check { p.as_bytes()[0] } else { 0 };

Expand All @@ -203,7 +193,7 @@ impl IgnoreGit {
(head, tail)
}

fn is_ignore_sub(&self, path: &PathBuf, names: &Vec<IgnoreGitPat>, paths: &Vec<IgnoreGitPat>) -> bool {
fn is_ignore_sub(&self, path: &Path, names: &Vec<IgnoreGitPat>, paths: &Vec<IgnoreGitPat>) -> bool {
let path_str = path.to_string_lossy();
let name_str = if let Some(x) = path.file_name() {
x.to_string_lossy()
Expand All @@ -217,28 +207,26 @@ impl IgnoreGit {
let path_end = (path_str.len() - 1) as isize;

for p in names {

if (p.head != 0) && (unsafe { *name_ptr } != p.head) {
continue;
}
if (p.tail != 0) && (unsafe { *name_ptr.offset(name_end) } != p.tail) {
continue;
}

if (p.head != 0) && (unsafe { *name_ptr } != p.head) {
continue;
}
if (p.tail != 0) && (unsafe { *name_ptr.offset(name_end) } != p.tail) {
continue;
}

if p.pat.matches_with(&name_str, self.opt) {
return true;
}
}

for p in paths {

if (p.head != 0) && (unsafe { *path_ptr } != p.head) {
continue;
}
if (p.tail != 0) && (unsafe { *path_ptr.offset(path_end) } != p.tail) {
continue;
}

if (p.head != 0) && (unsafe { *path_ptr } != p.head) {
continue;
}
if (p.tail != 0) && (unsafe { *path_ptr.offset(path_end) } != p.tail) {
continue;
}

if p.pat.matches_with(&path_str, self.opt) {
return true;
}
Expand All @@ -249,7 +237,7 @@ impl IgnoreGit {
}

impl Ignore for IgnoreGit {
fn is_ignore(&self, path: &PathBuf, is_dir: bool) -> bool {
fn is_ignore(&self, path: &Path, is_dir: bool) -> bool {
if is_dir {
self.is_ignore_sub(path, &self.dir_name, &self.dir_path)
} else {
Expand Down
Loading

0 comments on commit 7d73e73

Please sign in to comment.