nushell/crates/nu-protocol/src/config/datetime_format.rs
Ian Manske fce6146576
Refactor config updates (#13802)
# Description
This PR standardizes updates to the config through a new
`UpdateFromValue` trait. For now, this trait is private in case we need
to make changes to it.

Note that this PR adds some additional `ShellError` cases to create
standard error messages for config errors. A follow-up PR will move
usages of the old error cases to these new ones. This PR also uses
`Type::custom` in lots of places (e.g., for string enums). Not sure if
this is something we want to encourage.

# User-Facing Changes
Should be none.
2024-10-11 18:40:32 +02:00

40 lines
1.3 KiB
Rust

use super::prelude::*;
use crate as nu_protocol;
#[derive(Clone, Debug, Default, IntoValue, Serialize, Deserialize)]
pub struct DatetimeFormatConfig {
pub normal: Option<String>,
pub table: Option<String>,
}
impl UpdateFromValue for DatetimeFormatConfig {
fn update<'a>(
&mut self,
value: &'a Value,
path: &mut ConfigPath<'a>,
errors: &mut ConfigErrors,
) {
let Value::Record { val: record, .. } = value else {
errors.type_mismatch(path, Type::record(), value);
return;
};
for (col, val) in record.iter() {
let path = &mut path.push(col);
match col.as_str() {
"normal" => match val {
Value::Nothing { .. } => self.normal = None,
Value::String { val, .. } => self.normal = Some(val.clone()),
_ => errors.type_mismatch(path, Type::custom("string or nothing"), val),
},
"table" => match val {
Value::Nothing { .. } => self.table = None,
Value::String { val, .. } => self.table = Some(val.clone()),
_ => errors.type_mismatch(path, Type::custom("string or nothing"), val),
},
_ => errors.unknown_option(path, val),
}
}
}
}