Skip to content

Commit

Permalink
refactor: Refactor to use expect instead of unwrap (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm authored Nov 2, 2024
1 parent 6d847ee commit 3c87b23
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
10 changes: 6 additions & 4 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Display for TxtRecord {
if self.val.is_none() {
write!(f, "{}", self.key)
} else {
write!(f, "{}={}", self.key, self.val.clone().unwrap())
write!(f, "{}={}", self.key, self.val.clone().expect("To exist"))
}
}
}
Expand All @@ -40,7 +40,9 @@ impl ResolvedService {

pub fn timestamp_millis() -> u64 {
let now = SystemTime::now();
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
let since_epoch = now
.duration_since(SystemTime::UNIX_EPOCH)
.expect("To have a duration since unix epoch");

since_epoch.as_secs() * 1000 + u64::from(since_epoch.subsec_millis())
}
Expand Down Expand Up @@ -189,7 +191,7 @@ pub fn check_service_type_fully_qualified(service_type: &str) -> Result<(), Mdns
}

// Remove the trailing dot for validation purposes
let service_type = service_type.strip_suffix('.').unwrap();
let service_type = service_type.strip_suffix('.').expect("To end with .");

// Split into parts based on dots
let parts: Vec<&str> = service_type.split('.').collect();
Expand All @@ -201,7 +203,7 @@ pub fn check_service_type_fully_qualified(service_type: &str) -> Result<(), Mdns
return Err(MdnsError::IncorrectFormat);
}

let domain = parts.last().unwrap(); // Domain is always the last component
let domain = parts.last().expect("To have a domain"); // Domain is always the last component
let protocol = parts[parts.len() - 2]; // Protocol is the second-to-last component

// Validate protocol name (must be either _tcp or _udp)
Expand Down
21 changes: 12 additions & 9 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn from_service_info(info: &ServiceInfo) -> ResolvedService {
val: bytes_option_to_string_option_with_escaping(r.val()),
})
.collect();
sorted_txt.sort_by(|a, b| a.key.partial_cmp(&b.key).unwrap());
sorted_txt.sort_by(|a, b| a.key.partial_cmp(&b.key).expect("To be partial comparable"));
ResolvedService {
instance_name: info.get_fullname().into(),
hostname: info.get_hostname().into(),
Expand Down Expand Up @@ -347,8 +347,7 @@ fn copy_to_clipboard(window: Window, contents: String) {
let app = window.app_handle();
app.clipboard()
.write_text(contents)
.map_err(|err| log::error!("Failed to write to clipboard: {}", err))
.unwrap();
.expect("To write to clipboard");
}

#[cfg(desktop)]
Expand Down Expand Up @@ -395,7 +394,7 @@ mod app_updates {
current_version: update.current_version.clone(),
});

*pending_update.0.lock().unwrap() = update;
*pending_update.0.lock().expect("To lock") = update;

Ok(update_metadata)
}
Expand All @@ -405,7 +404,7 @@ mod app_updates {
app: AppHandle,
pending_update: State<'_, PendingUpdate>,
) -> Result<()> {
let Some(update) = pending_update.0.lock().unwrap().take() else {
let Some(update) = pending_update.0.lock().expect("To lock").take() else {
return Err(Error::NoPendingUpdate);
};

Expand Down Expand Up @@ -450,13 +449,17 @@ pub fn run() {
)
.plugin(tauri_plugin_updater::Builder::new().build())
.setup(|app| {
let splashscreen_window = app.get_webview_window("splashscreen").unwrap();
let main_window = app.get_webview_window("main").unwrap();
let splashscreen_window = app
.get_webview_window("splashscreen")
.expect("Splashscreen window to exist");
let main_window = app
.get_webview_window("main")
.expect("Main window to exist");
tauri::async_runtime::spawn(async move {
#[cfg(not(debug_assertions))]
tokio::time::sleep(SPLASH_SCREEN_DURATION).await;
splashscreen_window.close().unwrap();
main_window.show().unwrap();
splashscreen_window.close().expect("To close");
main_window.show().expect("To show");
#[cfg(debug_assertions)]
main_window.open_devtools();
});
Expand Down
9 changes: 5 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ fn ResolvedServiceGridItem(resolved_service: ResolvedService) -> impl IntoView {
log::debug!("ResolvedServiceGridItem");
let mut hostname = resolved_service.hostname;
hostname.pop(); // remove the trailing dot
let updated_at =
DateTime::from_timestamp_millis(resolved_service.updated_at_ms as i64).unwrap();
let updated_at = DateTime::from_timestamp_millis(resolved_service.updated_at_ms as i64)
.expect("To get convert");
let as_local_datetime: DateTime<Local> = updated_at.with_timezone(&Local);
let addrs = resolved_service
.addresses
Expand Down Expand Up @@ -416,8 +416,9 @@ fn Browse() -> impl IntoView {
log::debug!("Browse");
let (resolved, set_resolved) = create_signal(ResolvedServices::new());
create_resource(move || set_resolved, listen_on_resolve_events);

let is_desktop = use_context::<IsDesktopSignal>().unwrap().0;
let is_desktop = use_context::<IsDesktopSignal>()
.expect("is_desktop context to exist")
.0;
let browsing = create_rw_signal(false);
let service_type = create_rw_signal(String::new());
let not_browsing = Signal::derive(move || !browsing.get());
Expand Down

0 comments on commit 3c87b23

Please sign in to comment.