mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 23:23:12 +01:00
Show error on bad config, but keep going (#778)
This commit is contained in:
parent
d2d22815fb
commit
be8c905ca7
@ -104,77 +104,152 @@ pub enum FooterMode {
|
||||
|
||||
impl Value {
|
||||
pub fn into_config(self) -> Result<Config, ShellError> {
|
||||
let v = self.as_record()?;
|
||||
let v = self.as_record();
|
||||
|
||||
let mut config = Config::default();
|
||||
|
||||
for (key, value) in v.0.iter().zip(v.1) {
|
||||
match key.as_str() {
|
||||
"filesize_metric" => {
|
||||
config.filesize_metric = value.as_bool()?;
|
||||
}
|
||||
"table_mode" => {
|
||||
config.table_mode = value.as_string()?;
|
||||
}
|
||||
"use_ls_colors" => {
|
||||
config.use_ls_colors = value.as_bool()?;
|
||||
}
|
||||
"color_config" => {
|
||||
config.color_config = create_map(value, &config)?;
|
||||
}
|
||||
"use_grid_icons" => {
|
||||
config.use_grid_icons = value.as_bool()?;
|
||||
}
|
||||
"footer_mode" => {
|
||||
let val_str = value.as_string()?.to_lowercase();
|
||||
config.footer_mode = match val_str.as_ref() {
|
||||
"auto" => FooterMode::Auto,
|
||||
"never" => FooterMode::Never,
|
||||
"always" => FooterMode::Always,
|
||||
_ => match &val_str.parse::<u64>() {
|
||||
Ok(number) => FooterMode::RowCount(*number),
|
||||
_ => FooterMode::Never,
|
||||
},
|
||||
};
|
||||
}
|
||||
"animate_prompt" => {
|
||||
config.animate_prompt = value.as_bool()?;
|
||||
}
|
||||
"float_precision" => {
|
||||
config.float_precision = value.as_integer()?;
|
||||
}
|
||||
"use_ansi_coloring" => {
|
||||
config.use_ansi_coloring = value.as_bool()?;
|
||||
}
|
||||
"filesize_format" => {
|
||||
config.filesize_format = value.as_string()?.to_lowercase();
|
||||
}
|
||||
"env_conversions" => {
|
||||
let (env_vars, conversions) = value.as_record()?;
|
||||
let mut env_conversions = HashMap::new();
|
||||
|
||||
for (env_var, record) in env_vars.iter().zip(conversions) {
|
||||
// println!("{}: {:?}", env_var, record);
|
||||
env_conversions.insert(env_var.into(), EnvConversion::from_record(record)?);
|
||||
if let Ok(v) = v {
|
||||
for (key, value) in v.0.iter().zip(v.1) {
|
||||
match key.as_str() {
|
||||
"filesize_metric" => {
|
||||
if let Ok(b) = value.as_bool() {
|
||||
config.filesize_metric = b;
|
||||
} else {
|
||||
eprintln!("$config.filesize_metric is not a bool")
|
||||
}
|
||||
}
|
||||
"table_mode" => {
|
||||
if let Ok(v) = value.as_string() {
|
||||
config.table_mode = v;
|
||||
} else {
|
||||
eprintln!("$config.table_mode is not a string")
|
||||
}
|
||||
}
|
||||
"use_ls_colors" => {
|
||||
if let Ok(b) = value.as_bool() {
|
||||
config.use_ls_colors = b;
|
||||
} else {
|
||||
eprintln!("$config.use_ls_colors is not a bool")
|
||||
}
|
||||
}
|
||||
"color_config" => {
|
||||
if let Ok(map) = create_map(value, &config) {
|
||||
config.color_config = map;
|
||||
} else {
|
||||
eprintln!("$config.color_config is not a record")
|
||||
}
|
||||
}
|
||||
"use_grid_icons" => {
|
||||
if let Ok(b) = value.as_bool() {
|
||||
config.use_grid_icons = b;
|
||||
} else {
|
||||
eprintln!("$config.use_grid_icons is not a bool")
|
||||
}
|
||||
}
|
||||
"footer_mode" => {
|
||||
if let Ok(b) = value.as_string() {
|
||||
let val_str = b.to_lowercase();
|
||||
config.footer_mode = match val_str.as_ref() {
|
||||
"auto" => FooterMode::Auto,
|
||||
"never" => FooterMode::Never,
|
||||
"always" => FooterMode::Always,
|
||||
_ => match &val_str.parse::<u64>() {
|
||||
Ok(number) => FooterMode::RowCount(*number),
|
||||
_ => FooterMode::Never,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
eprintln!("$config.footer_mode is not a string")
|
||||
}
|
||||
}
|
||||
"animate_prompt" => {
|
||||
if let Ok(b) = value.as_bool() {
|
||||
config.animate_prompt = b;
|
||||
} else {
|
||||
eprintln!("$config.animate_prompt is not a bool")
|
||||
}
|
||||
}
|
||||
"float_precision" => {
|
||||
if let Ok(i) = value.as_integer() {
|
||||
config.float_precision = i;
|
||||
} else {
|
||||
eprintln!("$config.float_precision is not an integer")
|
||||
}
|
||||
}
|
||||
"use_ansi_coloring" => {
|
||||
if let Ok(b) = value.as_bool() {
|
||||
config.use_ansi_coloring = b;
|
||||
} else {
|
||||
eprintln!("$config.use_ansi_coloring is not a bool")
|
||||
}
|
||||
}
|
||||
"filesize_format" => {
|
||||
if let Ok(v) = value.as_string() {
|
||||
config.filesize_format = v.to_lowercase();
|
||||
} else {
|
||||
eprintln!("$config.filesize_format is not a string")
|
||||
}
|
||||
}
|
||||
"env_conversions" => {
|
||||
if let Ok((env_vars, conversions)) = value.as_record() {
|
||||
let mut env_conversions = HashMap::new();
|
||||
|
||||
config.env_conversions = env_conversions;
|
||||
for (env_var, record) in env_vars.iter().zip(conversions) {
|
||||
// println!("{}: {:?}", env_var, record);
|
||||
if let Ok(conversion) = EnvConversion::from_record(record) {
|
||||
env_conversions.insert(env_var.into(), conversion);
|
||||
} else {
|
||||
eprintln!("$config.env_conversions has incorrect conversion")
|
||||
}
|
||||
}
|
||||
|
||||
config.env_conversions = env_conversions;
|
||||
} else {
|
||||
eprintln!("$config.env_conversions is not a record")
|
||||
}
|
||||
}
|
||||
"edit_mode" => {
|
||||
if let Ok(v) = value.as_string() {
|
||||
config.edit_mode = v.to_lowercase();
|
||||
} else {
|
||||
eprintln!("$config.edit_mode is not a string")
|
||||
}
|
||||
}
|
||||
"max_history_size" => {
|
||||
if let Ok(i) = value.as_i64() {
|
||||
config.max_history_size = i;
|
||||
} else {
|
||||
eprintln!("$config.max_history_size is not an integer")
|
||||
}
|
||||
}
|
||||
"log_level" => {
|
||||
if let Ok(v) = value.as_string() {
|
||||
config.log_level = v.to_lowercase();
|
||||
} else {
|
||||
eprintln!("$config.log_level is not a string")
|
||||
}
|
||||
}
|
||||
"menu_config" => {
|
||||
if let Ok(map) = create_map(value, &config) {
|
||||
config.menu_config = map;
|
||||
} else {
|
||||
eprintln!("$config.menu_config is not a record")
|
||||
}
|
||||
}
|
||||
"keybindings" => {
|
||||
if let Ok(keybindings) = create_keybindings(value, &config) {
|
||||
config.keybindings = keybindings;
|
||||
} else {
|
||||
eprintln!("$config.keybindings is not a valid keybindings list")
|
||||
}
|
||||
}
|
||||
x => {
|
||||
eprintln!("$config.{} is an unknown config setting", x)
|
||||
}
|
||||
}
|
||||
"edit_mode" => {
|
||||
config.edit_mode = value.as_string()?;
|
||||
}
|
||||
"max_history_size" => {
|
||||
config.max_history_size = value.as_i64()?;
|
||||
}
|
||||
"log_level" => {
|
||||
config.log_level = value.as_string()?;
|
||||
}
|
||||
"menu_config" => {
|
||||
config.menu_config = create_map(value, &config)?;
|
||||
}
|
||||
"keybindings" => config.keybindings = create_keybindings(value, &config)?,
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
eprintln!("$config is not a record");
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
|
Loading…
Reference in New Issue
Block a user