Skip to main content

nu_protocol/config/
duration_max_unit.rs

1use super::{config_update_string_enum, prelude::*};
2
3use crate::{self as nu_protocol};
4
5/// The largest time unit used when formatting durations for display.
6///
7/// When set to a smaller unit, durations that would previously show weeks
8/// will instead show the equivalent number of days, hours, etc.
9#[derive(
10    Clone, Copy, Default, Debug, IntoValue, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
11)]
12pub enum DurationMaxUnit {
13    #[default]
14    #[nu_value(rename = "wk")]
15    Week,
16    #[nu_value(rename = "day")]
17    Day,
18    #[nu_value(rename = "hr")]
19    Hour,
20    #[nu_value(rename = "min")]
21    Minute,
22    #[nu_value(rename = "sec")]
23    Second,
24    #[nu_value(rename = "ms")]
25    Millisecond,
26    #[nu_value(rename = "us")]
27    Microsecond,
28    #[nu_value(rename = "ns")]
29    Nanosecond,
30}
31
32impl FromStr for DurationMaxUnit {
33    type Err = &'static str;
34
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        match s {
37            "wk" => Ok(Self::Week),
38            "day" => Ok(Self::Day),
39            "hr" => Ok(Self::Hour),
40            "min" => Ok(Self::Minute),
41            "sec" => Ok(Self::Second),
42            "ms" => Ok(Self::Millisecond),
43            "us" | "µs" => Ok(Self::Microsecond),
44            "ns" => Ok(Self::Nanosecond),
45            _ => Err("'wk', 'day', 'hr', 'min', 'sec', 'ms', 'us', 'µs', or 'ns'"),
46        }
47    }
48}
49
50impl UpdateFromValue for DurationMaxUnit {
51    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
52        config_update_string_enum(self, value, path, errors)
53    }
54}