Use Durations for durations in Config

This commit is contained in:
Félix Saparelli 2021-04-11 05:13:44 +12:00
parent 75ef0095ff
commit 15cdd7c754
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
4 changed files with 17 additions and 15 deletions

View File

@ -3,6 +3,7 @@ use log::LevelFilter;
use std::{
ffi::OsString,
path::{PathBuf, MAIN_SEPARATOR},
time::Duration,
};
use crate::config::{Config, ConfigBuilder};
@ -205,11 +206,11 @@ where
builder.ignores(ignores);
if args.occurrences_of("poll") > 0 {
builder.poll_interval(value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit()));
builder.poll_interval(Duration::from_millis(value_t!(args.value_of("poll"), u64).unwrap_or_else(|e| e.exit())));
}
if args.occurrences_of("debounce") > 0 {
builder.debounce(value_t!(args.value_of("debounce"), u64).unwrap_or_else(|e| e.exit()));
builder.debounce(Duration::from_millis(value_t!(args.value_of("debounce"), u64).unwrap_or_else(|e| e.exit())));
}
builder.on_busy_update(if args.is_present("restart") {

View File

@ -13,7 +13,7 @@
//! .expect("mission failed");
//! ```
use std::path::PathBuf;
use std::{time::Duration, path::PathBuf};
use crate::process::Shell;
use crate::run::OnBusyUpdate;
@ -43,9 +43,9 @@ pub struct Config {
/// Specify what to do when receiving updates while the command is running.
#[builder(default)]
pub on_busy_update: OnBusyUpdate,
/// Interval to debounce the changes. (milliseconds)
#[builder(default = "500")]
pub debounce: u64,
/// Interval to debounce the changes.
#[builder(default = "Duration::from_millis(500)")]
pub debounce: Duration,
/// Run the commands right after starting.
#[builder(default = "true")]
pub run_initially: bool,
@ -72,9 +72,9 @@ pub struct Config {
/// Force using the polling backend.
#[builder(default)]
pub poll: bool,
/// Interval for polling. (milliseconds)
#[builder(default = "1000")]
pub poll_interval: u32,
/// Interval for polling.
#[builder(default = "Duration::from_secs(1)")]
pub poll_interval: Duration,
}
impl ConfigBuilder {

View File

@ -131,7 +131,7 @@ where
})?;
if watcher.is_polling() {
warn!("Polling for changes every {} ms", args.poll_interval);
warn!("Polling for changes every {:?} ms", args.poll_interval);
}
// Call handler initially, if necessary
@ -278,7 +278,7 @@ pub fn run(args: Config) -> Result<()> {
fn wait_fs(
rx: &Receiver<Event>,
filter: &NotificationFilter,
debounce: u64,
debounce: Duration,
no_meta: bool,
) -> Vec<PathOp> {
let mut paths = Vec::new();
@ -310,8 +310,7 @@ fn wait_fs(
}
// Wait for filesystem activity to cool off
let timeout = Duration::from_millis(debounce);
while let Ok(e) = rx.recv_timeout(timeout) {
while let Ok(e) = rx.recv_timeout(debounce) {
if let Some(ref path) = e.path {
let pathop = PathOp::new(path, e.op.ok(), e.cookie);
if cache.contains_key(&pathop) {

View File

@ -1,4 +1,6 @@
use notify::{raw_watcher, PollWatcher, RecommendedWatcher, RecursiveMode};
use std::convert::TryFrom;
use std::time::Duration;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
@ -25,12 +27,12 @@ impl Watcher {
tx: Sender<Event>,
paths: &[PathBuf],
poll: bool,
interval_ms: u32,
interval: Duration,
) -> Result<Self, Error> {
use notify::Watcher;
let imp = if poll {
let mut watcher = PollWatcher::with_delay_ms(tx, interval_ms)?;
let mut watcher = PollWatcher::with_delay_ms(tx, u32::try_from(interval.as_millis()).unwrap_or(u32::MAX))?;
for path in paths {
watcher.watch(path, RecursiveMode::Recursive)?;
debug!("Watching {:?}", path);