From edad9e2d68346f46c9dee5e4c74853885e0735e2 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 13 May 2024 07:28:45 -0700 Subject: [PATCH] Refactor UpdateMode --- src-tauri/src/lib.rs | 126 +++++++++++++++++++-------------------- src-tauri/src/updates.rs | 39 ++++++------ 2 files changed, 85 insertions(+), 80 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d6c4e5d4..45d8908e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -57,7 +57,7 @@ use crate::models::{ }; use crate::plugin::{ImportResult, run_plugin_export_curl, run_plugin_import}; use crate::render::render_request; -use crate::updates::{update_mode_from_str, UpdateMode, YaakUpdater}; +use crate::updates::{UpdateMode, YaakUpdater}; use crate::window_menu::app_menu; mod analytics; @@ -218,8 +218,8 @@ async fn cmd_grpc_go( ..Default::default() }, ) - .await - .map_err(|e| e.to_string())? + .await + .map_err(|e| e.to_string())? }; let conn_id = conn.id.clone(); @@ -316,8 +316,8 @@ async fn cmd_grpc_go( ..base_msg.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); }); return; } @@ -332,8 +332,8 @@ async fn cmd_grpc_go( ..base_msg.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); }); } Ok(IncomingMsg::Commit) => { @@ -372,8 +372,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); async move { let (maybe_stream, maybe_msg) = match ( @@ -419,8 +419,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); } match maybe_msg { @@ -434,13 +434,13 @@ async fn cmd_grpc_go( } else { "Received response with metadata" } - .to_string(), + .to_string(), event_type: GrpcEventType::Info, ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); upsert_grpc_event( &w, &GrpcEvent { @@ -449,8 +449,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); upsert_grpc_event( &w, &GrpcEvent { @@ -460,8 +460,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); } Some(Err(e)) => { upsert_grpc_event( @@ -484,8 +484,8 @@ async fn cmd_grpc_go( }, }), ) - .await - .unwrap(); + .await + .unwrap(); } None => { // Server streaming doesn't return initial message @@ -503,13 +503,13 @@ async fn cmd_grpc_go( } else { "Received response with metadata" } - .to_string(), + .to_string(), event_type: GrpcEventType::Info, ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); stream.into_inner() } Some(Err(e)) => { @@ -533,8 +533,8 @@ async fn cmd_grpc_go( }, }), ) - .await - .unwrap(); + .await + .unwrap(); return; } None => return, @@ -552,8 +552,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); } Ok(None) => { let trailers = stream @@ -571,8 +571,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); break; } Err(status) => { @@ -586,8 +586,8 @@ async fn cmd_grpc_go( ..base_event.clone() }, ) - .await - .unwrap(); + .await + .unwrap(); } } } @@ -688,7 +688,7 @@ async fn cmd_send_ephemeral_request( None, &mut cancel_rx, ) - .await + .await } #[tauri::command] @@ -755,7 +755,7 @@ async fn cmd_import_data( AnalyticsAction::Import, Some(json!({ "plugin": plugin_name })), ) - .await; + .await; result = Some(r); break; } @@ -786,7 +786,7 @@ async fn cmd_import_data( let maybe_gen_id_opt = |id: Option, model: ModelType, ids: &mut HashMap| - -> Option { + -> Option { match id { Some(id) => Some(maybe_gen_id(id.as_str(), model, ids)), None => None, @@ -932,7 +932,7 @@ async fn cmd_export_data( AnalyticsAction::Export, None, ) - .await; + .await; Ok(()) } @@ -983,8 +983,8 @@ async fn cmd_send_http_request( None, None, ) - .await - .expect("Failed to create response"); + .await + .expect("Failed to create response"); let download_path = if let Some(p) = download_dir { Some(std::path::Path::new(p).to_path_buf()) @@ -1009,7 +1009,7 @@ async fn cmd_send_http_request( download_path, &mut cancel_rx, ) - .await + .await } async fn response_err( @@ -1117,8 +1117,8 @@ async fn cmd_create_cookie_jar( ..Default::default() }, ) - .await - .map_err(|e| e.to_string()) + .await + .map_err(|e| e.to_string()) } #[tauri::command] @@ -1137,8 +1137,8 @@ async fn cmd_create_environment( ..Default::default() }, ) - .await - .map_err(|e| e.to_string()) + .await + .map_err(|e| e.to_string()) } #[tauri::command] @@ -1159,8 +1159,8 @@ async fn cmd_create_grpc_request( ..Default::default() }, ) - .await - .map_err(|e| e.to_string()) + .await + .map_err(|e| e.to_string()) } #[tauri::command] @@ -1194,8 +1194,8 @@ async fn cmd_create_http_request( ..Default::default() }, ) - .await - .map_err(|e| e.to_string()) + .await + .map_err(|e| e.to_string()) } #[tauri::command] @@ -1287,8 +1287,8 @@ async fn cmd_create_folder( ..Default::default() }, ) - .await - .map_err(|e| e.to_string()) + .await + .map_err(|e| e.to_string()) } #[tauri::command] @@ -1418,8 +1418,8 @@ async fn cmd_list_cookie_jars( ..Default::default() }, ) - .await - .expect("Failed to create CookieJar"); + .await + .expect("Failed to create CookieJar"); Ok(vec![cookie_jar]) } else { Ok(cookie_jars) @@ -1488,8 +1488,8 @@ async fn cmd_list_workspaces(w: WebviewWindow) -> Result, String> ..Default::default() }, ) - .await - .expect("Failed to create Workspace"); + .await + .expect("Failed to create Workspace"); Ok(vec![workspace]) } else { Ok(workspaces) @@ -1731,16 +1731,16 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> WebviewWindow { window_id, WebviewUrl::App(url.unwrap_or_default().into()), ) - .resizable(true) - .fullscreen(false) - .disable_drag_drop_handler() // Required for frontend Dnd on windows - .inner_size(1100.0, 600.0) - .position( - // Randomly offset so windows don't stack exactly - 100.0 + random::() * 30.0, - 100.0 + random::() * 30.0, - ) - .title(handle.package_info().name.to_string()); + .resizable(true) + .fullscreen(false) + .disable_drag_drop_handler() // Required for frontend Dnd on windows + .inner_size(1100.0, 600.0) + .position( + // Randomly offset so windows don't stack exactly + 100.0 + random::() * 30.0, + 100.0 + random::() * 30.0, + ) + .title(handle.package_info().name.to_string()); // Add macOS-only things #[cfg(target_os = "macos")] @@ -1820,7 +1820,7 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> WebviewWindow { async fn get_update_mode(h: &AppHandle) -> UpdateMode { let settings = get_or_create_settings(h).await; - update_mode_from_str(settings.update_channel.as_str()) + UpdateMode::new(settings.update_channel.as_str()) } fn safe_uri(endpoint: &str) -> Result { diff --git a/src-tauri/src/updates.rs b/src-tauri/src/updates.rs index baadb802..d23169d5 100644 --- a/src-tauri/src/updates.rs +++ b/src-tauri/src/updates.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use std::time::SystemTime; use log::info; @@ -20,6 +21,25 @@ pub enum UpdateMode { Beta, } +impl Display for UpdateMode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let s = match self { + UpdateMode::Stable => "stable", + UpdateMode::Beta => "beta", + }; + write!(f, "{}", s) + } +} + +impl UpdateMode { + pub fn new(mode: &str) -> UpdateMode { + match mode { + "beta" => UpdateMode::Beta, + _ => UpdateMode::Stable, + } + } +} + impl YaakUpdater { pub fn new() -> Self { Self { @@ -33,11 +53,10 @@ impl YaakUpdater { ) -> Result { self.last_update_check = SystemTime::now(); - let update_mode = get_update_mode_str(mode); let enabled = !is_dev(); info!( "Checking for updates mode={} enabled={}", - update_mode, enabled + mode, enabled ); if !enabled { @@ -46,7 +65,7 @@ impl YaakUpdater { match app_handle .updater_builder() - .header("X-Update-Mode", update_mode)? + .header("X-Update-Mode", mode.to_string())? .build()? .check() .await @@ -102,17 +121,3 @@ impl YaakUpdater { self.force_check(app_handle, mode).await } } - -pub fn update_mode_from_str(mode: &str) -> UpdateMode { - match mode { - "beta" => UpdateMode::Beta, - _ => UpdateMode::Stable, - } -} - -fn get_update_mode_str(mode: UpdateMode) -> &'static str { - match mode { - UpdateMode::Stable => "stable", - UpdateMode::Beta => "beta", - } -}