Read settings in a bit
This commit is contained in:
@@ -10,19 +10,19 @@ pub struct Error {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(exit_code: i32, text: String) -> Error {
|
||||
Error{exit_code, text}
|
||||
pub fn new(text: String, exit_code: Option<i32>) -> Error {
|
||||
Error{exit_code: Self::get_exit_code(exit_code), text}
|
||||
}
|
||||
|
||||
pub fn try_with_code<T, S>(result: std::result::Result<T, S>, exit_code: Option<i32>) -> Result<T> where S: std::fmt::Display {
|
||||
pub fn try_or_new<T, S>(result: std::result::Result<T, S>, exit_code: Option<i32>) -> Result<T> where S: std::fmt::Display {
|
||||
match result {
|
||||
Ok(value) => Ok(value),
|
||||
Err(error) => Err(Error::new(Self::get_exit_code(exit_code), error.to_string())),
|
||||
Err(error) => Err(Error::new(error.to_string(), exit_code)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_with<T, S>(result: std::result::Result<T, S>) -> Result<T> where S: std::fmt::Display {
|
||||
Self::try_with_code(result, None)
|
||||
pub fn ok_or_new<T>(option: Option<T>, error_text: String, code: Option<i32>) -> Result<T> {
|
||||
Ok(option.ok_or(Error::new(error_text, code))?)
|
||||
}
|
||||
|
||||
fn get_exit_code(exit_code: Option<i32>) -> i32 {
|
||||
@@ -35,14 +35,22 @@ impl Error {
|
||||
|
||||
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output> {
|
||||
match output_file {
|
||||
Some(output_path) => Ok(Box::new(Error::try_with(std::fs::File::create(output_path))?)),
|
||||
Some(output_path) => Ok(Box::new(Error::try_or_new(std::fs::File::create(output_path), None)?)),
|
||||
None => Ok(Box::new(std::io::stdout())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input> {
|
||||
match input_file {
|
||||
Some(input_path) => Ok(Box::new(Error::try_with(std::fs::File::open(input_path))?)),
|
||||
Some(input_path) => Ok(Box::new(Error::try_or_new(std::fs::File::open(input_path), None)?)),
|
||||
None => Ok(Box::new(std::io::stdin())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String> {
|
||||
Ok(Error::ok_or_new(input.to_str(), format!("Converting {} to UTF-8 failed", name), None)?.to_owned())
|
||||
}
|
||||
|
||||
pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result<String> {
|
||||
os_str_to_string(Error::ok_or_new(input.file_name(), format!("No {} file name found", name), None)?, name)
|
||||
}
|
Reference in New Issue
Block a user