nu_protocol/config/
output.rs

1use super::{config_update_string_enum, prelude::*};
2
3use crate::{self as nu_protocol};
4
5#[derive(Clone, Copy, Default, Debug, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
6pub enum ErrorStyle {
7    #[default]
8    Fancy,
9    Plain,
10    Short,
11    Nested,
12}
13
14impl FromStr for ErrorStyle {
15    type Err = &'static str;
16
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        match s.to_ascii_lowercase().as_str() {
19            "fancy" => Ok(Self::Fancy),
20            "plain" => Ok(Self::Plain),
21            "short" => Ok(Self::Short),
22            "nested" => Ok(Self::Nested),
23            _ => Err("'fancy', 'plain', 'short' or 'nested'"),
24        }
25    }
26}
27
28impl UpdateFromValue for ErrorStyle {
29    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
30        config_update_string_enum(self, value, path, errors)
31    }
32}
33
34/// Option: show_banner
35#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
36pub enum BannerKind {
37    /// No banner on startup
38    None,
39    /// Abbreviated banner just containing the startup-time
40    Short,
41    /// The full banner including Ellie
42    #[default]
43    Full,
44}
45
46impl IntoValue for BannerKind {
47    fn into_value(self, span: Span) -> Value {
48        match self {
49            // This uses a custom implementation to reflect common config
50            // bool: true, false was used for a long time
51            // string: short was added later
52            BannerKind::None => Value::bool(false, span),
53            BannerKind::Short => Value::string("short", span),
54            BannerKind::Full => Value::bool(true, span),
55        }
56    }
57}
58
59impl UpdateFromValue for BannerKind {
60    fn update<'a>(
61        &mut self,
62        value: &'a Value,
63        path: &mut ConfigPath<'a>,
64        errors: &mut ConfigErrors,
65    ) {
66        match value {
67            Value::Bool { val, .. } => match val {
68                true => {
69                    *self = BannerKind::Full;
70                }
71                false => {
72                    *self = BannerKind::None;
73                }
74            },
75            Value::String { val, .. } => match val.as_str() {
76                "true" => {
77                    *self = BannerKind::Full;
78                }
79                "full" => {
80                    *self = BannerKind::Full;
81                }
82                "short" => {
83                    *self = BannerKind::Short;
84                }
85                "false" => {
86                    *self = BannerKind::None;
87                }
88                "none" => {
89                    *self = BannerKind::None;
90                }
91                _ => {
92                    errors.invalid_value(path, "true/'full', 'short', false/'none'", value);
93                }
94            },
95            _ => {
96                errors.invalid_value(path, "true/'full', 'short', false/'none'", value);
97            }
98        }
99    }
100}