MCPcopy Create free account
hub / github.com/aws/clock-bound / DurationParse

Interface DurationParse

clock-bound/src/daemon/time/inner/string_parse.rs:6–60  ·  view source on GitHub ↗

String parse functionality for time types developers note. Functionality is needed for setting values in `ff-tester` for high level parameters. Do not need to set femtosecond level granularity

Source from the content-addressed store, hash-verified

4// in `ff-tester` for high level parameters. Do not need to set
5// sub-nanosecond level granularity
6pub trait DurationParse: Sized {
7 fn from_nanoseconds(nanos: i64) -> Self;
8
9 /// Parse from a duration of time
10 ///
11 /// Takes in a signed integer plus a suffix of
12 /// - days
13 /// - hours
14 /// - minutes
15 /// - seconds
16 /// - milliseconds
17 /// - microseconds
18 /// - nanoseconds
19 ///
20 /// Without a suffix, nanoseconds are the default type
21 ///
22 /// # Errors
23 /// Errors if
24 fn parse_from_duration(s: &str) -> Result<Self, String> {
25 let s = s.trim();
26 let (rest, duration) =
27 nom::character::complete::digit1::<_, ()>(s).map_err(|_| "Failed to parse duration")?;
28 let duration = duration.parse::<i64>().map_err(|_| "i64 parsing failed")?;
29
30 let suffix = rest.trim();
31
32 match suffix.to_lowercase().as_str() {
33 "" => Ok(Self::from_nanoseconds(duration)),
34 "ns" | "nano" | "nanos" | "nanosecond" | "nanoseconds" => {
35 Ok(Self::from_nanoseconds(duration))
36 }
37 "us" | "micro" | "micros" | "microsecond" | "microseconds" => {
38 Ok(Self::from_nanoseconds(duration * NANOS_PER_MICRO))
39 }
40 "ms" | "milli" | "millis" | "millisecond" | "milliseconds" => {
41 Ok(Self::from_nanoseconds(duration * NANOS_PER_MILLI))
42 }
43 "s" | "sec" | "secs" | "second" | "seconds" => {
44 Ok(Self::from_nanoseconds(duration * NANOS_PER_SEC))
45 }
46 "m" | "min" | "mins" | "minute" | "minutes" => Ok(Self::from_nanoseconds(
47 duration * NANOS_PER_SEC * SECS_PER_MINUTE,
48 )),
49 "h" | "hr" | "hrs" | "hour" | "hours" => Ok(Self::from_nanoseconds(
50 duration * NANOS_PER_SEC * SECS_PER_MINUTE * MINS_PER_HOUR,
51 )),
52 "d" | "day" | "days" => Ok(Self::from_nanoseconds(
53 duration * NANOS_PER_SEC * SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY,
54 )),
55 _ => Err(format!("Unknown suffix: {suffix}")),
56 }
57 }
58}
59
60impl<T: super::NanoType + Copy> DurationParse for super::Time<T> {
61 fn from_nanoseconds(nanos: i64) -> Self {
62 Self::from_nanos(nanos)
63 }

Callers

nothing calls this directly

Implementers 1

string_parse.rsclock-bound/src/daemon/time/inner/stri

Calls

no outgoing calls

Tested by

no test coverage detected