allow inflight filter
This commit is contained in:
1 parent
aeb827b361
commit
8b111ddfdc
9 files changed
+956
-55
No files matched your search
@@ -1,4 +1,5 @@
|
||||
use anyhow::{bail, Result};
|
||||
use std::time::Duration;
|
||||
use std::path::Path;
|
||||
|
||||
/// Either a local filesystem path or a `[user@]host:path` remote target.
|
||||
@@ -128,6 +129,28 @@ pub fn parse_size(s: &str) -> Result<usize> {
|
||||
Ok(n * mult)
|
||||
}
|
||||
|
||||
/// Parses a flush/idle interval: a bare number is milliseconds, or an
|
||||
/// explicit "ms"/"s" suffix. "100", "100ms" and "0.1s" are not all spellings
|
||||
/// of the same thing -- only integers are accepted, so use "100ms" or "1s".
|
||||
pub fn parse_duration(s: &str) -> Result<Duration> {
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
bail!("empty interval");
|
||||
}
|
||||
let (digits, mult_ms) = if let Some(d) = s.strip_suffix("ms") {
|
||||
(d, 1)
|
||||
} else if let Some(d) = s.strip_suffix('s') {
|
||||
(d, 1000)
|
||||
} else {
|
||||
(s, 1)
|
||||
};
|
||||
let n: u64 = digits
|
||||
.trim()
|
||||
.parse()
|
||||
.map_err(|_| anyhow::anyhow!("invalid interval: {s:?} (try \"100ms\" or \"1s\")"))?;
|
||||
Ok(Duration::from_millis(n * mult_ms))
|
||||
}
|
||||
|
||||
/// Single-quote a string for safe inclusion as one argument in a remote shell
|
||||
/// command line, e.g. for `ssh host sh -c '...'`-style invocations.
|
||||
pub fn shell_quote(s: &str) -> String {
|
||||
@@ -251,6 +274,19 @@ mod tests {
|
||||
assert_eq!(d.temp_path, "cap.pcap.partial");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duration_parsing() {
|
||||
assert_eq!(parse_duration("100").unwrap(), Duration::from_millis(100));
|
||||
assert_eq!(parse_duration("100ms").unwrap(), Duration::from_millis(100));
|
||||
assert_eq!(parse_duration("2s").unwrap(), Duration::from_secs(2));
|
||||
assert_eq!(parse_duration(" 250ms ").unwrap(), Duration::from_millis(250));
|
||||
// 0 is legal: cut a chunk as soon as anything is kept.
|
||||
assert_eq!(parse_duration("0").unwrap(), Duration::ZERO);
|
||||
for bad in ["", "abc", "0.1s", "100m", "-5"] {
|
||||
assert!(parse_duration(bad).is_err(), "{bad:?} should be rejected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_parsing() {
|
||||
assert_eq!(parse_size("256k").unwrap(), 256 * 1024);
|
||||
|
||||
Reference in new issue
Block a user