Use functors instead of parameters

This commit is contained in:
Jaby 2022-09-14 22:00:26 +02:00 committed by Jaby
parent f024cf4f1b
commit 525346212d
2 changed files with 6 additions and 6 deletions

View File

@ -14,7 +14,7 @@ struct CommandLine {
fn configurate(cmd: &CommandLine) -> tool_helper::Result<Configuration> { fn configurate(cmd: &CommandLine) -> tool_helper::Result<Configuration> {
let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?; 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({ let file_type = Error::ok_or_new({
match extension.as_ref() { match extension.as_ref() {
"h" => Some(FileType::CHeader), "h" => Some(FileType::CHeader),
@ -23,7 +23,7 @@ fn configurate(cmd: &CommandLine) -> tool_helper::Result<Configuration> {
"cpp" => Some(FileType::CPPSource), "cpp" => Some(FileType::CPPSource),
_ => None _ => 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}) Ok(Configuration{file_name, data_name: "Planschbecken".to_owned(), line_feed: cpp_out::LineFeed::Windows, file_type})
} }

View File

@ -21,8 +21,8 @@ impl Error {
} }
} }
pub fn ok_or_new<T>(option: Option<T>, error_text: String, code: Option<i32>) -> Result<T> { pub fn ok_or_new<T, F>(option: Option<T>, error_text: F, code: Option<i32>) -> Result<T> where F: Fn () -> String{
Ok(option.ok_or(Error::new(error_text, code))?) Ok(option.ok_or(Error::new(error_text(), code))?)
} }
fn get_exit_code(exit_code: Option<i32>) -> i32 { fn get_exit_code(exit_code: Option<i32>) -> i32 {
@ -48,9 +48,9 @@ pub fn open_input(input_file: Option<PathBuf>) -> Result<Input> {
} }
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String> { 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()) 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> { 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) os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name), None)?, name)
} }