Skip to content

Commit

Permalink
[rt_util] Allow unwrap_or_quit() to take both String and &str
Browse files Browse the repository at this point in the history
  • Loading branch information
m4heshd committed Apr 12, 2024
1 parent 15dbbe1 commit 4e300fd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/src/app_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn get_app_root_dir() -> PathBuf {
env::current_exe()
.unwrap_or_quit(err_msg)
.parent()
.unwrap_or_quit(format!("{err_msg}. Invalid executable path").as_str())
.unwrap_or_quit(format!("{err_msg}. Invalid executable path"))
.to_path_buf()
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/net_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn init_server() {
// TCP listener
let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], *port)))
.await
.unwrap_or_quit(format!("Failed to start the server on port \"{port}\"").as_str());
.unwrap_or_quit(format!("Failed to start the server on port \"{port}\""));

log_success!(
"UFC Ripper (v{}) GUI is live at http://localhost:{port} {}\n",
Expand Down
10 changes: 5 additions & 5 deletions backend/src/rt_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ impl Drop for ExitHandler {
/// Implements custom unwrap functionality which quits the application when fails
pub trait QuitUnwrap<T> {
/// Unwraps the value or quits the application with a custom message
fn unwrap_or_quit(self, msg: &str) -> T;
fn unwrap_or_quit(self, msg: impl AsRef<str>) -> T;
}

// Implements `QuitUnwrap` for `Result`
impl<T, E> QuitUnwrap<T> for Result<T, E>
where
E: Display,
{
fn unwrap_or_quit(self, msg: &str) -> T {
fn unwrap_or_quit(self, msg: impl AsRef<str>) -> T {
match self {
Ok(val) => val,
Err(err) => quit(Some(format!("Error: {err}\n{msg}").as_str())),
Err(err) => quit(Some(&format!("Error: {err}\n{}", msg.as_ref()))),
}
}
}

// Implements `QuitUnwrap` for `Option`
impl<T> QuitUnwrap<T> for Option<T> {
fn unwrap_or_quit(self, msg: &str) -> T {
fn unwrap_or_quit(self, msg: impl AsRef<str>) -> T {
match self {
Some(val) => val,
None => quit(Some(msg)),
None => quit(Some(msg.as_ref())),
}
}
}
Expand Down

0 comments on commit 4e300fd

Please sign in to comment.