Show error on bad config, but keep going (#778)

This commit is contained in:
JT 2022-01-19 11:42:12 -05:00 committed by GitHub
parent d2d22815fb
commit be8c905ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,29 +104,51 @@ 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();
if let Ok(v) = v {
for (key, value) in v.0.iter().zip(v.1) {
match key.as_str() {
"filesize_metric" => {
config.filesize_metric = value.as_bool()?;
if let Ok(b) = value.as_bool() {
config.filesize_metric = b;
} else {
eprintln!("$config.filesize_metric is not a bool")
}
}
"table_mode" => {
config.table_mode = value.as_string()?;
if let Ok(v) = value.as_string() {
config.table_mode = v;
} else {
eprintln!("$config.table_mode is not a string")
}
}
"use_ls_colors" => {
config.use_ls_colors = value.as_bool()?;
if let Ok(b) = value.as_bool() {
config.use_ls_colors = b;
} else {
eprintln!("$config.use_ls_colors is not a bool")
}
}
"color_config" => {
config.color_config = create_map(value, &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" => {
config.use_grid_icons = value.as_bool()?;
if let Ok(b) = value.as_bool() {
config.use_grid_icons = b;
} else {
eprintln!("$config.use_grid_icons is not a bool")
}
}
"footer_mode" => {
let val_str = value.as_string()?.to_lowercase();
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,
@ -136,45 +158,98 @@ impl Value {
_ => FooterMode::Never,
},
};
} else {
eprintln!("$config.footer_mode is not a string")
}
}
"animate_prompt" => {
config.animate_prompt = value.as_bool()?;
if let Ok(b) = value.as_bool() {
config.animate_prompt = b;
} else {
eprintln!("$config.animate_prompt is not a bool")
}
}
"float_precision" => {
config.float_precision = value.as_integer()?;
if let Ok(i) = value.as_integer() {
config.float_precision = i;
} else {
eprintln!("$config.float_precision is not an integer")
}
}
"use_ansi_coloring" => {
config.use_ansi_coloring = value.as_bool()?;
if let Ok(b) = value.as_bool() {
config.use_ansi_coloring = b;
} else {
eprintln!("$config.use_ansi_coloring is not a bool")
}
}
"filesize_format" => {
config.filesize_format = value.as_string()?.to_lowercase();
if let Ok(v) = value.as_string() {
config.filesize_format = v.to_lowercase();
} else {
eprintln!("$config.filesize_format is not a string")
}
}
"env_conversions" => {
let (env_vars, conversions) = value.as_record()?;
if let Ok((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(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" => {
config.edit_mode = value.as_string()?;
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" => {
config.max_history_size = value.as_i64()?;
if let Ok(i) = value.as_i64() {
config.max_history_size = i;
} else {
eprintln!("$config.max_history_size is not an integer")
}
}
"log_level" => {
config.log_level = value.as_string()?;
if let Ok(v) = value.as_string() {
config.log_level = v.to_lowercase();
} else {
eprintln!("$config.log_level is not a string")
}
}
"menu_config" => {
config.menu_config = create_map(value, &config)?;
if let Ok(map) = create_map(value, &config) {
config.menu_config = map;
} else {
eprintln!("$config.menu_config is not a record")
}
"keybindings" => config.keybindings = create_keybindings(value, &config)?,
_ => {}
}
"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)
}
}
}
} else {
eprintln!("$config is not a record");
}
Ok(config)