forked from mike-engel/jwt-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
34 lines (28 loc) · 982 Bytes
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::fs;
use std::path::Path;
pub fn slurp_file(file_name: &str) -> Vec<u8> {
fs::read(file_name).unwrap_or_else(|_| panic!("Unable to read file {}", file_name))
}
pub fn write_file(path: &Path, content: &[u8]) {
fs::write(path, content).unwrap_or_else(|_| panic!("Unable to write file {}", path.display()))
}
pub fn parse_duration_string(val: &str) -> Result<i64, String> {
let mut base_val = val.replace(" ago", "");
if val.starts_with('-') {
base_val = base_val.replacen('-', "", 1);
}
match parse_duration::parse(&base_val) {
Ok(parsed_duration) => {
let is_past = val.starts_with('-') || val.contains("ago");
let seconds = parsed_duration.as_secs() as i64;
if is_past {
Ok(-seconds)
} else {
Ok(seconds)
}
}
Err(_) => Err(String::from(
"must be a UNIX timestamp or systemd.time string",
)),
}
}