Begin working on more graceful error handling

NOTE: This does not work currently

Signed-off-by: Marko Korhonen <marko.korhonen@reekynet.com>
This commit is contained in:
Marko Korhonen 2020-01-21 17:22:17 +02:00
parent 04e23c6144
commit 242c982565
No known key found for this signature in database
GPG key ID: 911B85FBC6003FE5

View file

@ -39,24 +39,44 @@ pub fn load_xdg() -> Config {
"{} does not exist, writing default configuration", "{} does not exist, writing default configuration",
path.display() path.display()
); );
match fs::write(&path, toml::to_string(&Config::default()).unwrap()) { match fs::write(&path, toml::to_string(&Config::default()).unwrap()) {
Ok(()) => println!("Default config saved to {}", path.display()), Ok(()) => println!("Default config saved to {}", path.display()),
Err(err) => eprintln!("Failed to write default config: {}", err), Err(err) => eprintln!("Failed to write default config: {}", err),
}; };
Config::default() Config::default()
} else { } else {
let toml = fs::read(&path).unwrap(); let toml: String;
let config: Config = toml::from_str(&toml).unwrap();
match fs::read(&path) {
Ok(string) => toml = string,
Err(err) => {
eprintln!("Failed to read config: {}", err);
eprintln!("Using default config");
return Config::default();
}
}
match toml::from_str<Config, _>(&toml) {
Ok(parsed_conf) = parsed_conf,
Err(err) => {
eprintln!("Failed to parse config: {}", err);
eprintln!("Using default config");
config config
} }
} }
}
}
pub fn load_user(path: PathBuf) -> Config { pub fn load_user(path: PathBuf) -> Config {
if !path.exists() { if !path.exists() {
panic!("{} does not exist", path.display()); panic!("{} does not exist", path.display());
} else { } else {
let toml = fs::read(&path).unwrap(); let toml = fs::read(&path).unwrap();
let config: Config = toml::from_str(&toml).unwrap(); match toml::from_str(&toml) {
Ok(parsed_conf) ->
}
config config
} }
} }