mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 14:41:00 +02:00
Make Hooks
fields non-optional to match the new config defaults (#14345)
# Description Follow up to #14341. Changes the fields of `Hooks` to `Vec` or `Hashmap` to match the new config defaults. # User-Facing Changes Mostly the same as #14341. `pre_prompt` and `pre_execution` must now be a list, and `env_change` must be a record.
This commit is contained in:
@ -1,13 +1,13 @@
|
||||
use super::prelude::*;
|
||||
use crate as nu_protocol;
|
||||
use crate::Record;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Definition of a parsed hook from the config object
|
||||
#[derive(Clone, Debug, IntoValue, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Hooks {
|
||||
pub pre_prompt: Option<Value>,
|
||||
pub pre_execution: Option<Value>,
|
||||
pub env_change: Option<Value>,
|
||||
pub pre_prompt: Vec<Value>,
|
||||
pub pre_execution: Vec<Value>,
|
||||
pub env_change: HashMap<String, Vec<Value>>,
|
||||
pub display_output: Option<Value>,
|
||||
pub command_not_found: Option<Value>,
|
||||
}
|
||||
@ -15,14 +15,14 @@ pub struct Hooks {
|
||||
impl Hooks {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pre_prompt: Some(Value::list(vec![], Span::unknown())),
|
||||
pre_execution: Some(Value::list(vec![], Span::unknown())),
|
||||
env_change: Some(Value::record(Record::default(), Span::unknown())),
|
||||
pre_prompt: Vec::new(),
|
||||
pre_execution: Vec::new(),
|
||||
env_change: HashMap::new(),
|
||||
display_output: Some(Value::string(
|
||||
"if (term size).columns >= 100 { table -e } else { table }",
|
||||
Span::unknown(),
|
||||
)),
|
||||
command_not_found: Some(Value::list(vec![], Span::unknown())),
|
||||
command_not_found: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -40,14 +40,6 @@ impl UpdateFromValue for Hooks {
|
||||
path: &mut ConfigPath<'a>,
|
||||
errors: &mut ConfigErrors,
|
||||
) {
|
||||
fn update_option(field: &mut Option<Value>, value: &Value) {
|
||||
if value.is_nothing() {
|
||||
*field = None;
|
||||
} else {
|
||||
*field = Some(value.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let Value::Record { val: record, .. } = value else {
|
||||
errors.type_mismatch(path, Type::record(), value);
|
||||
return;
|
||||
@ -56,11 +48,57 @@ impl UpdateFromValue for Hooks {
|
||||
for (col, val) in record.iter() {
|
||||
let path = &mut path.push(col);
|
||||
match col.as_str() {
|
||||
"pre_prompt" => update_option(&mut self.pre_prompt, val),
|
||||
"pre_execution" => update_option(&mut self.pre_execution, val),
|
||||
"env_change" => update_option(&mut self.env_change, val),
|
||||
"display_output" => update_option(&mut self.display_output, val),
|
||||
"command_not_found" => update_option(&mut self.command_not_found, val),
|
||||
"pre_prompt" => {
|
||||
if let Ok(hooks) = val.as_list() {
|
||||
self.pre_prompt = hooks.into()
|
||||
} else {
|
||||
errors.type_mismatch(path, Type::list(Type::Any), val);
|
||||
}
|
||||
}
|
||||
"pre_execution" => {
|
||||
if let Ok(hooks) = val.as_list() {
|
||||
self.pre_execution = hooks.into()
|
||||
} else {
|
||||
errors.type_mismatch(path, Type::list(Type::Any), val);
|
||||
}
|
||||
}
|
||||
"env_change" => {
|
||||
if let Ok(record) = val.as_record() {
|
||||
self.env_change = record
|
||||
.iter()
|
||||
.map(|(key, val)| {
|
||||
let old = self.env_change.remove(key).unwrap_or_default();
|
||||
let new = if let Ok(hooks) = val.as_list() {
|
||||
hooks.into()
|
||||
} else {
|
||||
errors.type_mismatch(
|
||||
&path.push(key),
|
||||
Type::list(Type::Any),
|
||||
val,
|
||||
);
|
||||
old
|
||||
};
|
||||
(key.as_str().into(), new)
|
||||
})
|
||||
.collect();
|
||||
} else {
|
||||
errors.type_mismatch(path, Type::record(), val);
|
||||
}
|
||||
}
|
||||
"display_output" => {
|
||||
self.display_output = if val.is_nothing() {
|
||||
None
|
||||
} else {
|
||||
Some(val.clone())
|
||||
}
|
||||
}
|
||||
"command_not_found" => {
|
||||
self.command_not_found = if val.is_nothing() {
|
||||
None
|
||||
} else {
|
||||
Some(val.clone())
|
||||
}
|
||||
}
|
||||
_ => errors.unknown_option(path, val),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user