Skip to content

Commit

Permalink
Improve error handling when config does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicparga committed Sep 2, 2023
1 parent d43d13e commit 5094e8b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions billo/lib/running/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ mod datastructures;
use crate::running::datastructures::Config;
use std::fmt::Write as _;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

fn ensure_input(config_filepathbuf: &PathBuf) -> err::Result<Config> {
let mut err_msg: String = String::new();

if !config_filepathbuf.is_file() {
if !config_filepathbuf.exists() {
writeln!(
err_msg,
"Config file does not exist: {}",
config_filepathbuf.to_string_lossy()
)
.expect("Should already handle error that config filepath being dirpath");
} else if !config_filepathbuf.is_file() {
writeln!(
err_msg,
"Expected config as file, but dir is provided: {}",
config_filepathbuf.to_string_lossy()
)
.expect("Should already handle error that config filepath being dirpath");
}
// some debugging, could be removed
let config_dirpathbuf: &Path = config_filepathbuf.parent().expect("Expected a parent");
fs::write(config_dirpathbuf.join("greetings.txt"), "Hello world!")
.expect("Unable to write file");

if err_msg.is_empty() {
let json_str: String = fs::read_to_string(config_filepathbuf)
Expand Down

0 comments on commit 5094e8b

Please sign in to comment.