clean-up first implementation of modification date filter
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Karim SENHAJI · committed by David Peter · parent 54c117d · Browse files · 6 files·+102 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+102 added−58 removed
- tests:+16 added−15 removed
- other:+86 added−43 removed
Provenance
- Commit
abe8aa5- Tree
91fedda- Author
Karim SENHAJI <karim.senhaji@amadeus.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
54c117d
6 files changed+102 added−58 removed
modified:src/internal.rs+66 added-17 removed
106 unmodified lines107 After(SystemTime),108 }109 110 impl TimeFilter {111- fn from_str(s: &str) -> Option<SystemTime> {111+ fn from_str(ref_time: &SystemTime, s: &str) -> Option<SystemTime> {112 use humantime;113 humantime::parse_duration(s)114- .map(|duration| SystemTime::now() - duration)114+ .map(|duration| *ref_time - duration)115 .or_else(|_| humantime::parse_rfc3339_weak(s))116 .ok()117 }118 119- pub fn before(s: &str) -> Option<TimeFilter> {120- Some(TimeFilter::Before(TimeFilter::from_str(s)?))119+ pub fn before(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {120+ TimeFilter::from_str(ref_time, s).map(TimeFilter::Before)121 }122 123- pub fn after(s: &str) -> Option<TimeFilter> {124- Some(TimeFilter::After(TimeFilter::from_str(s)?))123+ pub fn after(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {124+ TimeFilter::from_str(ref_time, s).map(TimeFilter::After)125 }126 127- pub fn is_within(&self, t: &SystemTime) -> bool {127+ pub fn applies_to(&self, t: &SystemTime) -> bool {128 match self {129 TimeFilter::Before(limit) => t <= limit,130 TimeFilter::After(limit) => t >= limit,131 }63 unmodified lines195 /// The given constraints on the size of returned files196 pub size_constraints: Vec<SizeFilter>,197 198 /// Constraints on last modification time of files199- pub modification_constraints: Vec<TimeFilter>,199+ pub time_constraints: Vec<TimeFilter>,200 }201 202 /// Print error message to stderr.203 pub fn print_error(message: &str) {300 unmodified lines504 assert!(f.is_within(1000));505 }506 507 #[test]508- fn is_time_within() {509- let now = SystemTime::now();510- assert!(TimeFilter::after("1min").unwrap().is_within(&now));511- assert!(!TimeFilter::before("1min").unwrap().is_within(&now));508+ fn is_time_filter_applicable() {509+ use humantime;510 513- let t1m_ago = SystemTime::now() - time::Duration::from_secs(60);514- assert!(!TimeFilter::after("30sec").unwrap().is_within(&t1m_ago));515- assert!(TimeFilter::after("2min").unwrap().is_within(&t1m_ago));511+ let ref_time = humantime::parse_rfc3339("2010-10-10T10:10:10Z").unwrap();512+ assert!(513+ TimeFilter::after(&ref_time, "1min")514+ .unwrap()515+ .applies_to(&ref_time)516+ );517+ assert!(518+ !TimeFilter::before(&ref_time, "1min")519+ .unwrap()520+ .applies_to(&ref_time)521+ );522 517- assert!(TimeFilter::before("30sec").unwrap().is_within(&t1m_ago));518- assert!(!TimeFilter::before("2min").unwrap().is_within(&t1m_ago));523+ let t1m_ago = ref_time - time::Duration::from_secs(60);524+ assert!(525+ !TimeFilter::after(&ref_time, "30sec")526+ .unwrap()527+ .applies_to(&t1m_ago)528+ );529+ assert!(530+ TimeFilter::after(&ref_time, "2min")531+ .unwrap()532+ .applies_to(&t1m_ago)533+ );534+535+ assert!(536+ TimeFilter::before(&ref_time, "30sec")537+ .unwrap()538+ .applies_to(&t1m_ago)539+ );540+ assert!(541+ !TimeFilter::before(&ref_time, "2min")542+ .unwrap()543+ .applies_to(&t1m_ago)544+ );545+546+ let t10s_before = "2010-10-10 10:10:00";547+ assert!(548+ !TimeFilter::before(&ref_time, t10s_before)549+ .unwrap()550+ .applies_to(&ref_time)551+ );552+ assert!(553+ TimeFilter::before(&ref_time, t10s_before)554+ .unwrap()555+ .applies_to(&t1m_ago)556+ );557+558+ assert!(559+ TimeFilter::after(&ref_time, t10s_before)560+ .unwrap()561+ .applies_to(&ref_time)562+ );563+ assert!(564+ !TimeFilter::after(&ref_time, t10s_before)565+ .unwrap()566+ .applies_to(&t1m_ago)567+ );568 }569 }modified:src/main.rs+7 added-8 removed
17 unmodified lines18 extern crate libc;19 extern crate num_cpus;20 extern crate regex;21 extern crate regex_syntax;22-#[macro_use]23-extern crate if_chain;22 23 mod app;24 mod exec;25 mod exit_codes;124 unmodified lines150 .collect()151 })152 .unwrap_or_else(|| vec![]);153 154+ let now = time::SystemTime::now();156- let mut modification_constraints: Vec<TimeFilter> = Vec::new();155+ let mut time_constraints: Vec<TimeFilter> = Vec::new();156 if let Some(t) = matches.value_of("changed-within") {158- if let Some(f) = TimeFilter::after(t) {159- modification_constraints.push(f);157+ if let Some(f) = TimeFilter::after(&now, t) {158+ time_constraints.push(f);159 } else {160 print_error_and_exit(&format!("Error: {} is not a valid time.", t));161 }162 }163 if let Some(t) = matches.value_of("changed-before") {165- if let Some(f) = TimeFilter::before(t) {166- modification_constraints.push(f);164+ if let Some(f) = TimeFilter::before(&now, t) {165+ time_constraints.push(f);166 } else {167 print_error_and_exit(&format!("Error: {} is not a valid time.", t));168 }169 }73 unmodified lines243 .values_of("ignore-file")244 .map(|vs| vs.map(PathBuf::from).collect())245 .unwrap_or_else(|| vec![]),246 size_constraints: size_limits,248- modification_constraints,247+ time_constraints,248 };249 250 match RegexBuilder::new(&pattern_regex)251 .case_insensitive(!config.case_sensitive)14 unmodified linesmodified:src/walk.rs+13 added-10 removed
288 unmodified lines289 }290 }291 292 // Filter out unwanted modification times293- if !config.modification_constraints.is_empty() {294- if_chain!{295- if entry_path.is_file();296- if let Ok(metadata) = entry_path.metadata();297- if let Ok(modified) = metadata.modified();298- if config.modification_constraints.iter().all(|tf| tf.is_within(&modified));299- then {300- // When all is good, we just continue with pattern match301- } else {302- return ignore::WalkState::Continue;293+ if !config.time_constraints.is_empty() {294+ let mut matched = false;295+ if entry_path.is_file() {296+ if let Ok(metadata) = entry_path.metadata() {297+ if let Ok(modified) = metadata.modified() {298+ matched = config299+ .time_constraints300+ .iter()301+ .all(|tf| tf.applies_to(&modified));302+ }303 }304 }305+ if !matched {306+ return ignore::WalkState::Continue;307+ }308 }309 310 let search_str_o = if config.search_full_path {311 match fshelper::path_absolute_form(entry_path) {31 unmodified linesmodified:tests/tests.rs+16 added-15 removed
1123 unmodified lines1124 1125 #[test]1126 fn test_modified_relative() {1127 let te = TestEnv::new(&[], &[]);1128- create_file_with_modified(te.test_root().join("0_now"), 0);1129- create_file_with_modified(te.test_root().join("1_min"), 60);1130- create_file_with_modified(te.test_root().join("10_min"), 600);1131- create_file_with_modified(te.test_root().join("1_h"), 60 * 60);1132- create_file_with_modified(te.test_root().join("2_h"), 2 * 60 * 60);1133- create_file_with_modified(te.test_root().join("1_day"), 24 * 60 * 60);1128+ create_file_with_modified(te.test_root().join("foo_0_now"), 0);1129+ create_file_with_modified(te.test_root().join("bar_1_min"), 60);1130+ create_file_with_modified(te.test_root().join("foo_10_min"), 600);1131+ create_file_with_modified(te.test_root().join("bar_1_h"), 60 * 60);1132+ create_file_with_modified(te.test_root().join("foo_2_h"), 2 * 60 * 60);1133+ create_file_with_modified(te.test_root().join("bar_1_day"), 24 * 60 * 60);1134 1135 te.assert_output(1136 &["", "--changed-within", "15min"],1137- "0_now1138- 1_min1139- 10_min",1137+ "foo_0_now1138+ bar_1_min1139+ foo_10_min",1140 );1141 1142 te.assert_output(1143 &["", "--changed-before", "15min"],1144- "1_h1145- 2_h1146- 1_day",1144+ "bar_1_h1145+ foo_2_h1146+ bar_1_day",1147 );1148 1149 te.assert_output(1150- &["min", "--changed-within", "12h"],1151- "1_min1152- 10_min",1150+ &["foo", "--changed-within", "12h"],1151+ "foo_0_now1152+ foo_10_min1153+ foo_2_h",1154 );1155 }1156 1157 #[cfg(test)]21 unmodified linesmodified:Cargo.lock-7 removed
82 unmodified lines83 "ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)",84 "diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",85 "filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",86 "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",87- "if_chain 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",87 "ignore 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",88 "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",89 "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",90 "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",52 unmodified lines143 "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",144 ]145 146 [[package]]148-name = "if_chain"149-version = "0.1.3"150-source = "registry+https://github.com/rust-lang/crates.io-index"151-152-[[package]]147 name = "ignore"148 version = "0.4.3"149 source = "registry+https://github.com/rust-lang/crates.io-index"150 dependencies = [266 unmodified lines417 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"418 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"419 "checksum globset 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8e49edbcc9c7fc5beb8c0a54e7319ff8bed353a2b55e85811c6281188c2a6c84"420 "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"427-"checksum if_chain 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4bac95d9aa0624e7b78187d6fb8ab012b41d9f6f54b1bcb61e61c4845f8357ec"421 "checksum ignore 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3e9faa7c84064f07b40da27044af629f578bc7994b650d3e458d0c29183c1d91"422 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"423 "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"424 "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"30 unmodified linesmodified:Cargo.toml-1 removed
39 unmodified lines4041424344444546478 unmodified lines39 unmodified linesregex = "1.0.0"regex-syntax = "0.6"ctrlc = "3.1"humantime = "1.1.1"if_chain = "0.1.3"
[dependencies.clap]version = "2.31.2"features = ["suggestions", "color", "wrap_help"]8 unmodified lines