Replace expect calls with results

This commit is contained in:
Jeff 2024-06-12 22:01:07 -04:00
parent 6b86c032ee
commit be06387316
2 changed files with 8 additions and 8 deletions

View File

@ -133,7 +133,7 @@ impl Bot {
}
}
let old_config = Config::read();
let old_config = Config::read()?;
let new_config = Config {
username: old_config.username,
password: old_config.password,

View File

@ -16,23 +16,23 @@ struct Config {
}
impl Config {
fn read() -> Self {
let config_path = var("CONFIG_PATH").expect("Provide CONFIG_PATH environment variable");
let config_file_content = read_to_string(config_path).expect("Failed to read config file");
fn read() -> Result<Self, String> {
let config_path = var("CONFIG_PATH").map_err(|error| error.to_string())?;
let config_file_content = read_to_string(config_path).map_err(|error| error.to_string())?;
toml::from_str::<Config>(&config_file_content).expect("Failed to deserialize config file.")
toml::from_str::<Config>(&config_file_content).map_err(|error| error.to_string())
}
fn write(&self) -> Result<(), String> {
let config_path = var("CONFIG_PATH").expect("Provide CONFIG_PATH environment variable");
let config_string = toml::to_string(self).expect("Failed to serialize Config");
let config_path = var("CONFIG_PATH").map_err(|error| error.to_string())?;
let config_string = toml::to_string(self).map_err(|error| error.to_string())?;
write(config_path, config_string).map_err(|error| error.to_string())
}
}
fn main() {
let config = Config::read();
let config = Config::read().unwrap();
let mut bot = Bot::new(&config.username, &config.password, config.admin_list)
.expect("Failed to create bot");