From f751ab7c2cd36ce85c6c82ac7e54bc724077a6b4 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 14 Sep 2022 22:00:26 +0200 Subject: [PATCH] Use functors instead of parameters --- src/Tools/cpp_out/src/main.rs | 4 ++-- src/Tools/tool_helper/src/lib.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index 4bb07a45..ad09cf63 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -14,7 +14,7 @@ struct CommandLine { fn configurate(cmd: &CommandLine) -> tool_helper::Result { let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?; - let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), "File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase(); + let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase(); let file_type = Error::ok_or_new({ match extension.as_ref() { "h" => Some(FileType::CHeader), @@ -23,7 +23,7 @@ fn configurate(cmd: &CommandLine) -> tool_helper::Result { "cpp" => Some(FileType::CPPSource), _ => None } - }, format!("{} unkown file extension", extension), None)?; + }, ||format!("{} unkown file extension", extension), None)?; Ok(Configuration{file_name, data_name: "Planschbecken".to_owned(), line_feed: cpp_out::LineFeed::Windows, file_type}) } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index f12bdf84..fcbee1c5 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -21,8 +21,8 @@ impl Error { } } - pub fn ok_or_new(option: Option, error_text: String, code: Option) -> Result { - Ok(option.ok_or(Error::new(error_text, code))?) + pub fn ok_or_new(option: Option, error_text: F, code: Option) -> Result where F: Fn () -> String{ + Ok(option.ok_or(Error::new(error_text(), code))?) } fn get_exit_code(exit_code: Option) -> i32 { @@ -48,9 +48,9 @@ pub fn open_input(input_file: Option) -> Result { } pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result { - Ok(Error::ok_or_new(input.to_str(), format!("Converting {} to UTF-8 failed", name), None)?.to_owned()) + 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 { - os_str_to_string(Error::ok_or_new(input.file_name(), format!("No {} file name found", name), None)?, name) + os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name), None)?, name) } \ No newline at end of file