aperture_shared/utils/
mod.rs1pub mod syscalls;
4pub mod time;
5
6use anyhow::Result;
7
8pub fn bytes_to_hex(bytes: &[u8]) -> String {
10 bytes.iter().map(|b| format!("{:02x}", b)).collect()
11}
12
13pub fn parse_duration(s: &str) -> Result<std::time::Duration> {
15 let s = s.trim();
16
17 if let Some(num_str) = s.strip_suffix('s') {
18 let secs: u64 = num_str.parse()?;
19 Ok(std::time::Duration::from_secs(secs))
20 } else if let Some(num_str) = s.strip_suffix('m') {
21 let mins: u64 = num_str.parse()?;
22 Ok(std::time::Duration::from_secs(mins * 60))
23 } else if let Some(num_str) = s.strip_suffix('h') {
24 let hours: u64 = num_str.parse()?;
25 Ok(std::time::Duration::from_secs(hours * 3600))
26 } else {
27 let secs: u64 = s.parse()?;
29 Ok(std::time::Duration::from_secs(secs))
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn test_parse_duration() {
39 assert_eq!(parse_duration("30s").unwrap().as_secs(), 30);
40 assert_eq!(parse_duration("5m").unwrap().as_secs(), 300);
41 assert_eq!(parse_duration("1h").unwrap().as_secs(), 3600);
42 assert_eq!(parse_duration("60").unwrap().as_secs(), 60);
43 }
44
45 #[test]
46 fn test_bytes_to_hex() {
47 assert_eq!(bytes_to_hex(&[0xde, 0xad, 0xbe, 0xef]), "deadbeef");
48 }
49}