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::{ use std::{
ffi::OsString, ffi::OsString,
path::{PathBuf, MAIN_SEPARATOR}, path::{PathBuf, MAIN_SEPARATOR},
time::Duration,
}; };
use crate::config::{Config, ConfigBuilder}; use crate::config::{Config, ConfigBuilder};
@ -205,11 +206,11 @@ where
builder.ignores(ignores); builder.ignores(ignores);
if args.occurrences_of("poll") > 0 { 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 { 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") { builder.on_busy_update(if args.is_present("restart") {

View File

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

View File

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

View File

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