Skip to content

Commit

Permalink
fixed crash on invalid config file
Browse files Browse the repository at this point in the history
  • Loading branch information
MnlPhlp committed May 14, 2024
1 parent da33bb0 commit 8579992
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wspick"
version = "1.2.3"
version = "1.3.0"
authors = ["MnlPhlp <online@philipp-manuel.de>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
35 changes: 31 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ fn main() -> Result<()> {
save_config(&Projects::new(), &config_file)?;
}
// load config
// let mut config: Projects = toml_edit::de::from_str(&fs::read_to_string(&config_file)?)?;
let mut config: Projects = toml::from_str(&fs::read_to_string(&config_file)?)?;
let mut config = load_config(&config_file)?;
// add later added config items
update_config(&mut config, &config_file)?;
// check cmd args#
Expand Down Expand Up @@ -119,6 +118,34 @@ fn main() -> Result<()> {
Ok(())
}

fn load_config(config_file: &PathBuf) -> Result<Projects> {
let mut config: Result<Projects, _> = toml::from_str(&fs::read_to_string(&config_file)?);
while let Err(ref err) = config {
// display error and ask for action
match inquire::Select::new(
format!("config file is invalid: {err}\n\nwhat do you want to do?").as_str(),
vec!["edit", "generate new", "exit"],
)
.prompt()?
{
"edit" => {
let mut edited = Projects::new();
if edit_project(&mut edited, config_file).is_ok() {
config = Ok(edited)
};
}
"generate new" => {
// generate new empty configuration
save_config(&Projects::new(), config_file)?;
config = Ok(Projects::new())
}
"exit" => std::process::exit(1),
_ => (),
}
}
Ok(config?)
}

fn add_dir(config: &mut Projects, config_file: &PathBuf) -> Result<()> {
let path = inquire::Text::new("directory path:")
.with_validator(FileValidator)
Expand Down Expand Up @@ -311,7 +338,7 @@ fn sort_config(config: &mut Projects) {
let mut keys = config.paths.keys().cloned().collect::<Vec<String>>();
keys.sort();
for k in keys {
let val = config.paths.remove(&k).unwrap();
let val = config.paths.swap_remove(&k).unwrap();
new_paths.insert(k, val);
}
config.paths = new_paths;
Expand All @@ -323,7 +350,7 @@ fn edit_project(config: &mut Projects, config_file: &PathBuf) -> Result<()> {
.arg(config_file)
.spawn()?
.wait()?;
let new_config: Projects = toml::from_str(&fs::read_to_string(config_file)?)?;
let new_config = load_config(config_file)?;
config.paths = new_config.paths;
config.editor = new_config.editor;
config.open_cmd = new_config.open_cmd;
Expand Down

0 comments on commit 8579992

Please sign in to comment.