From ba330047cab84437801a24adb34d0ec218793dae Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 11 Jan 2025 13:53:30 -0800 Subject: [PATCH] Cargo format --- src-tauri/src/analytics.rs | 5 ++- src-tauri/src/notifications.rs | 5 +-- src-tauri/src/tauri_plugin_mac_window.rs | 36 +++++----------- src-tauri/src/template_callback.rs | 6 +-- src-tauri/yaak-grpc/src/codec.rs | 6 +-- src-tauri/yaak-grpc/src/lib.rs | 8 ++-- src-tauri/yaak-grpc/src/manager.rs | 37 ++++++---------- src-tauri/yaak-grpc/src/proto.rs | 13 +++--- src-tauri/yaak-license/src/license.rs | 4 +- src-tauri/yaak-models/src/lib.rs | 4 +- src-tauri/yaak-plugins/src/error.rs | 20 ++++----- src-tauri/yaak-plugins/src/lib.rs | 6 +-- src-tauri/yaak-plugins/src/nodejs.rs | 18 +++----- src-tauri/yaak-plugins/src/plugin_handle.rs | 7 +-- src-tauri/yaak-templates/src/lib.rs | 4 +- src-tauri/yaak-templates/src/parser.rs | 19 +++------ src-tauri/yaak-templates/src/renderer.rs | 47 ++++----------------- 17 files changed, 82 insertions(+), 163 deletions(-) diff --git a/src-tauri/src/analytics.rs b/src-tauri/src/analytics.rs index 6090a917..25e3c463 100644 --- a/src-tauri/src/analytics.rs +++ b/src-tauri/src/analytics.rs @@ -5,7 +5,10 @@ use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use tauri::{Manager, Runtime, WebviewWindow}; use ts_rs::TS; -use yaak_models::queries::{generate_id, get_key_value_int, get_key_value_string, get_or_create_settings, set_key_value_int, set_key_value_string, UpdateSource}; +use yaak_models::queries::{ + generate_id, get_key_value_int, get_key_value_string, get_or_create_settings, + set_key_value_int, set_key_value_string, UpdateSource, +}; use crate::is_dev; diff --git a/src-tauri/src/notifications.rs b/src-tauri/src/notifications.rs index abaed170..9543eae3 100644 --- a/src-tauri/src/notifications.rs +++ b/src-tauri/src/notifications.rs @@ -74,10 +74,7 @@ impl YaakNotifier { return Ok(()); } - let notification = resp - .json::() - .await - .map_err(|e| e.to_string())?; + let notification = resp.json::().await.map_err(|e| e.to_string())?; let age = notification.timestamp.signed_duration_since(Utc::now()); let seen = get_kv(window).await?; diff --git a/src-tauri/src/tauri_plugin_mac_window.rs b/src-tauri/src/tauri_plugin_mac_window.rs index 6a9bb969..8cb70b50 100644 --- a/src-tauri/src/tauri_plugin_mac_window.rs +++ b/src-tauri/src/tauri_plugin_mac_window.rs @@ -1,3 +1,4 @@ +use crate::MAIN_WINDOW_PREFIX; use hex_color::HexColor; use log::warn; use objc::{msg_send, sel, sel_impl}; @@ -6,7 +7,6 @@ use tauri::{ plugin::{Builder, TauriPlugin}, Emitter, Listener, Manager, Runtime, Window, WindowEvent, }; -use crate::MAIN_WINDOW_PREFIX; const WINDOW_CONTROL_PAD_X: f64 = 13.0; const WINDOW_CONTROL_PAD_Y: f64 = 18.0; @@ -202,10 +202,9 @@ pub fn setup_traffic_light_positioner(window: &Window) { } unsafe { - let ns_win = window - .ns_window() - .expect("NS Window should exist to mount traffic light delegate.") - as id; + let ns_win = + window.ns_window().expect("NS Window should exist to mount traffic light delegate.") + as id; let current_delegate: id = ns_win.delegate(); @@ -322,10 +321,7 @@ pub fn setup_traffic_light_positioner(window: &Window) { ) { unsafe { with_window_state(&*this, |state: &mut WindowState| { - state - .window - .emit("did-enter-fullscreen", ()) - .expect("Failed to emit event"); + state.window.emit("did-enter-fullscreen", ()).expect("Failed to emit event"); }); let super_del: id = *this.get_ivar("super_delegate"); @@ -339,10 +335,7 @@ pub fn setup_traffic_light_positioner(window: &Window) { ) { unsafe { with_window_state(&*this, |state: &mut WindowState| { - state - .window - .emit("will-enter-fullscreen", ()) - .expect("Failed to emit event"); + state.window.emit("will-enter-fullscreen", ()).expect("Failed to emit event"); }); let super_del: id = *this.get_ivar("super_delegate"); @@ -356,10 +349,7 @@ pub fn setup_traffic_light_positioner(window: &Window) { ) { unsafe { with_window_state(&*this, |state: &mut WindowState| { - state - .window - .emit("did-exit-fullscreen", ()) - .expect("Failed to emit event"); + state.window.emit("did-exit-fullscreen", ()).expect("Failed to emit event"); let id = state.window.ns_window().expect("Failed to emit event") as id; position_traffic_lights( @@ -381,10 +371,7 @@ pub fn setup_traffic_light_positioner(window: &Window) { ) { unsafe { with_window_state(&*this, |state: &mut WindowState| { - state - .window - .emit("will-exit-fullscreen", ()) - .expect("Failed to emit event"); + state.window.emit("will-exit-fullscreen", ()).expect("Failed to emit event"); }); let super_del: id = *this.get_ivar("super_delegate"); @@ -432,11 +419,8 @@ pub fn setup_traffic_light_positioner(window: &Window) { window: window.clone(), }; let app_box = Box::into_raw(Box::new(app_state)) as *mut c_void; - let random_str: String = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(20) - .map(char::from) - .collect(); + let random_str: String = + rand::thread_rng().sample_iter(&Alphanumeric).take(20).map(char::from).collect(); // We need to ensure we have a unique delegate name, otherwise we will panic while trying to create a duplicate // delegate with the same name. diff --git a/src-tauri/src/template_callback.rs b/src-tauri/src/template_callback.rs index 01dffacf..d05037a0 100644 --- a/src-tauri/src/template_callback.rs +++ b/src-tauri/src/template_callback.rs @@ -31,11 +31,7 @@ impl TemplateCallback for PluginTemplateCallback { let window_context = self.window_context.to_owned(); // The beta named the function `Response` but was changed in stable. // Keep this here for a while because there's no easy way to migrate - let fn_name = if fn_name == "Response" { - "response" - } else { - fn_name - }; + let fn_name = if fn_name == "Response" { "response" } else { fn_name }; let function = self .plugin_manager diff --git a/src-tauri/yaak-grpc/src/codec.rs b/src-tauri/yaak-grpc/src/codec.rs index f01ce87d..33bb3832 100644 --- a/src-tauri/yaak-grpc/src/codec.rs +++ b/src-tauri/yaak-grpc/src/codec.rs @@ -33,8 +33,7 @@ impl Encoder for DynamicCodec { type Error = Status; fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error> { - item.encode(dst) - .expect("buffer is too small to decode this message"); + item.encode(dst).expect("buffer is too small to decode this message"); Ok(()) } } @@ -45,8 +44,7 @@ impl Decoder for DynamicCodec { fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result, Self::Error> { let mut msg = DynamicMessage::new(self.0.output()); - msg.merge(src) - .map_err(|err| Status::internal(err.to_string()))?; + msg.merge(src).map_err(|err| Status::internal(err.to_string()))?; Ok(Some(msg)) } } diff --git a/src-tauri/yaak-grpc/src/lib.rs b/src-tauri/yaak-grpc/src/lib.rs index 828c1627..f05f7382 100644 --- a/src-tauri/yaak-grpc/src/lib.rs +++ b/src-tauri/yaak-grpc/src/lib.rs @@ -30,15 +30,13 @@ pub struct MethodDefinition { pub server_streaming: bool, } -static SERIALIZE_OPTIONS: &'static SerializeOptions = &SerializeOptions::new() - .skip_default_fields(false) - .stringify_64_bit_integers(false); +static SERIALIZE_OPTIONS: &'static SerializeOptions = + &SerializeOptions::new().skip_default_fields(false).stringify_64_bit_integers(false); pub fn serialize_message(msg: &DynamicMessage) -> Result { let mut buf = Vec::new(); let mut se = serde_json::Serializer::pretty(&mut buf); - msg.serialize_with_options(&mut se, SERIALIZE_OPTIONS) - .map_err(|e| e.to_string())?; + msg.serialize_with_options(&mut se, SERIALIZE_OPTIONS).map_err(|e| e.to_string())?; let s = String::from_utf8(buf).expect("serde_json to emit valid utf8"); Ok(s) } diff --git a/src-tauri/yaak-grpc/src/manager.rs b/src-tauri/yaak-grpc/src/manager.rs index ad8e3e08..348e18e8 100644 --- a/src-tauri/yaak-grpc/src/manager.rs +++ b/src-tauri/yaak-grpc/src/manager.rs @@ -54,19 +54,14 @@ impl From for StreamError { impl GrpcConnection { pub fn service(&self, service: &str) -> Result { - let service = self - .pool - .get_service_by_name(service) - .ok_or("Failed to find service")?; + let service = self.pool.get_service_by_name(service).ok_or("Failed to find service")?; Ok(service) } pub fn method(&self, service: &str, method: &str) -> Result { let service = self.service(service)?; - let method = service - .methods() - .find(|m| m.name() == method) - .ok_or("Failed to find method")?; + let method = + service.methods().find(|m| m.name() == method).ok_or("Failed to find method")?; Ok(method) } @@ -132,13 +127,10 @@ impl GrpcConnection { let path = method_desc_to_path(method); let codec = DynamicCodec::new(method.clone()); client.ready().await.unwrap(); - client - .client_streaming(req, path, codec) - .await - .map_err(|e| StreamError { - message: e.message().to_string(), - status: Some(e), - }) + client.client_streaming(req, path, codec).await.map_err(|e| StreamError { + message: e.message().to_string(), + status: Some(e), + }) } pub async fn server_streaming( @@ -197,8 +189,7 @@ impl GrpcHandle { fill_pool_from_files(&self.app_handle, proto_files).await }?; - self.pools - .insert(make_pool_key(id, uri, proto_files), pool.clone()); + self.pools.insert(make_pool_key(id, uri, proto_files), pool.clone()); Ok(()) } @@ -211,9 +202,7 @@ impl GrpcHandle { // Ensure reflection is up-to-date self.reflect(id, uri, proto_files).await?; - let pool = self - .get_pool(id, uri, proto_files) - .ok_or("Failed to get pool".to_string())?; + let pool = self.get_pool(id, uri, proto_files).ok_or("Failed to get pool".to_string())?; Ok(self.services_from_pool(&pool)) } @@ -234,7 +223,7 @@ impl GrpcHandle { &pool, input_message, )) - .unwrap(), + .unwrap(), }) } def @@ -249,9 +238,7 @@ impl GrpcHandle { proto_files: &Vec, ) -> Result { self.reflect(id, uri, proto_files).await?; - let pool = self - .get_pool(id, uri, proto_files) - .ok_or("Failed to get pool")?; + let pool = self.get_pool(id, uri, proto_files).ok_or("Failed to get pool")?; let uri = uri_from_str(uri)?; let conn = get_transport(); @@ -301,4 +288,4 @@ fn make_pool_key(id: &str, uri: &str, proto_files: &Vec) -> String { ); format!("{:x}", md5::compute(pool_key)) -} \ No newline at end of file +} diff --git a/src-tauri/yaak-grpc/src/proto.rs b/src-tauri/yaak-grpc/src/proto.rs index 8217c7e2..a1b95366 100644 --- a/src-tauri/yaak-grpc/src/proto.rs +++ b/src-tauri/yaak-grpc/src/proto.rs @@ -104,7 +104,7 @@ pub async fn fill_pool_from_reflection(uri: &Uri) -> Result(app_handle: &AppHandle) -> Result Ok(LicenseCheckStatus::Trialing { end: trial_end }), - (false, false) => Ok(LicenseCheckStatus::PersonalUse { trial_ended: trial_end }), + (false, false) => Ok(LicenseCheckStatus::PersonalUse { + trial_ended: trial_end, + }), (true, _) => { info!("Checking license activation"); // A license has been activated, so let's check the license server diff --git a/src-tauri/yaak-models/src/lib.rs b/src-tauri/yaak-models/src/lib.rs index 23512ac1..2c6f5d79 100644 --- a/src-tauri/yaak-models/src/lib.rs +++ b/src-tauri/yaak-models/src/lib.rs @@ -1,5 +1,5 @@ +pub mod error; pub mod models; pub mod queries; -pub mod error; -pub mod plugin; \ No newline at end of file +pub mod plugin; diff --git a/src-tauri/yaak-plugins/src/error.rs b/src-tauri/yaak-plugins/src/error.rs index 710189a9..2940502f 100644 --- a/src-tauri/yaak-plugins/src/error.rs +++ b/src-tauri/yaak-plugins/src/error.rs @@ -1,37 +1,37 @@ +use crate::server::plugin_runtime::EventStreamEvent; use thiserror::Error; use tokio::io; use tokio::sync::mpsc::error::SendError; -use crate::server::plugin_runtime::EventStreamEvent; #[derive(Error, Debug)] pub enum Error { #[error("IO error: {0}")] IoErr(#[from] io::Error), - + #[error("Tauri error: {0}")] TauriErr(#[from] tauri::Error), - + #[error("Tauri shell error: {0}")] TauriShellErr(#[from] tauri_plugin_shell::Error), - + #[error("Grpc transport error: {0}")] GrpcTransportErr(#[from] tonic::transport::Error), - + #[error("Grpc send error: {0}")] GrpcSendErr(#[from] SendError>), - + #[error("JSON error: {0}")] JsonErr(#[from] serde_json::Error), - + #[error("Plugin not found: {0}")] PluginNotFoundErr(String), - + #[error("Plugin error: {0}")] PluginErr(String), - + #[error("Client not initialized error")] ClientNotInitializedErr, - + #[error("Unknown event received")] UnknownEventErr, } diff --git a/src-tauri/yaak-plugins/src/lib.rs b/src-tauri/yaak-plugins/src/lib.rs index d9a26853..6842c2b7 100644 --- a/src-tauri/yaak-plugins/src/lib.rs +++ b/src-tauri/yaak-plugins/src/lib.rs @@ -1,15 +1,15 @@ -use std::process::exit; +use crate::manager::PluginManager; use log::info; +use std::process::exit; use tauri::plugin::{Builder, TauriPlugin}; use tauri::{Manager, RunEvent, Runtime, State}; -use crate::manager::PluginManager; pub mod error; pub mod events; pub mod manager; mod nodejs; -mod server; pub mod plugin_handle; +mod server; mod util; pub fn init() -> TauriPlugin { diff --git a/src-tauri/yaak-plugins/src/nodejs.rs b/src-tauri/yaak-plugins/src/nodejs.rs index 8d8a9cc2..a854e6cc 100644 --- a/src-tauri/yaak-plugins/src/nodejs.rs +++ b/src-tauri/yaak-plugins/src/nodejs.rs @@ -1,8 +1,8 @@ -use std::net::SocketAddr; use crate::error::Result; use log::info; use serde; use serde::Deserialize; +use std::net::SocketAddr; use tauri::path::BaseDirectory; use tauri::{AppHandle, Manager, Runtime}; use tauri_plugin_shell::process::CommandEvent; @@ -20,15 +20,12 @@ pub async fn start_nodejs_plugin_runtime( addr: SocketAddr, kill_rx: &Receiver, ) -> Result<()> { - let plugin_runtime_main = app - .path() - .resolve("vendored/plugin-runtime", BaseDirectory::Resource)? - .join("index.cjs"); + let plugin_runtime_main = + app.path().resolve("vendored/plugin-runtime", BaseDirectory::Resource)?.join("index.cjs"); // HACK: Remove UNC prefix for Windows paths to pass to sidecar - let plugin_runtime_main = dunce::simplified(plugin_runtime_main.as_path()) - .to_string_lossy() - .to_string(); + let plugin_runtime_main = + dunce::simplified(plugin_runtime_main.as_path()).to_string_lossy().to_string(); info!("Starting plugin runtime main={}", plugin_runtime_main); @@ -59,10 +56,7 @@ pub async fn start_nodejs_plugin_runtime( // Check on child tokio::spawn(async move { - kill_rx - .wait_for(|b| *b == true) - .await - .expect("Kill channel errored"); + kill_rx.wait_for(|b| *b == true).await.expect("Kill channel errored"); info!("Killing plugin runtime"); child.kill().expect("Failed to kill plugin runtime"); info!("Killed plugin runtime"); diff --git a/src-tauri/yaak-plugins/src/plugin_handle.rs b/src-tauri/yaak-plugins/src/plugin_handle.rs index c9eb637e..5821e681 100644 --- a/src-tauri/yaak-plugins/src/plugin_handle.rs +++ b/src-tauri/yaak-plugins/src/plugin_handle.rs @@ -57,11 +57,8 @@ impl PluginHandle { pub async fn terminate(&self, window_context: WindowContext) -> Result<()> { info!("Terminating plugin {}", self.dir); - let event = self.build_event_to_send( - window_context, - &InternalEventPayload::TerminateRequest, - None, - ); + let event = + self.build_event_to_send(window_context, &InternalEventPayload::TerminateRequest, None); self.send(&event).await } diff --git a/src-tauri/yaak-templates/src/lib.rs b/src-tauri/yaak-templates/src/lib.rs index 55c9da3d..80a3b023 100644 --- a/src-tauri/yaak-templates/src/lib.rs +++ b/src-tauri/yaak-templates/src/lib.rs @@ -1,6 +1,6 @@ +pub mod format; pub mod parser; pub mod renderer; -pub mod format; pub use parser::*; -pub use renderer::*; \ No newline at end of file +pub use renderer::*; diff --git a/src-tauri/yaak-templates/src/parser.rs b/src-tauri/yaak-templates/src/parser.rs index 9fdc3829..de3abc26 100644 --- a/src-tauri/yaak-templates/src/parser.rs +++ b/src-tauri/yaak-templates/src/parser.rs @@ -3,25 +3,20 @@ use std::fmt::Display; use ts_rs::TS; #[derive(Clone, PartialEq, Debug, Serialize, Deserialize, TS)] -#[ts(export, export_to="parser.ts")] +#[ts(export, export_to = "parser.ts")] pub struct Tokens { pub tokens: Vec, } impl Display for Tokens { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = self - .tokens - .iter() - .map(|t| t.to_string()) - .collect::>() - .join(""); + let str = self.tokens.iter().map(|t| t.to_string()).collect::>().join(""); write!(f, "{}", str) } } #[derive(Clone, PartialEq, Debug, Serialize, Deserialize, TS)] -#[ts(export, export_to="parser.ts")] +#[ts(export, export_to = "parser.ts")] pub struct FnArg { pub name: String, pub value: Val, @@ -36,7 +31,7 @@ impl Display for FnArg { #[derive(Clone, PartialEq, Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "snake_case", tag = "type")] -#[ts(export, export_to="parser.ts")] +#[ts(export, export_to = "parser.ts")] pub enum Val { Str { text: String }, Var { name: String }, @@ -71,7 +66,7 @@ impl Display for Val { #[derive(Clone, PartialEq, Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "snake_case", tag = "type")] -#[ts(export, export_to="parser.ts")] +#[ts(export, export_to = "parser.ts")] pub enum Token { Raw { text: String }, Tag { val: Val }, @@ -396,9 +391,7 @@ impl Parser { return false; } - let cmp = self.chars[self.pos..self.pos + value.len()] - .iter() - .collect::(); + let cmp = self.chars[self.pos..self.pos + value.len()].iter().collect::(); if cmp == value { // We have a match, so advance the current index diff --git a/src-tauri/yaak-templates/src/renderer.rs b/src-tauri/yaak-templates/src/renderer.rs index 0ff24045..cb7ffaa2 100644 --- a/src-tauri/yaak-templates/src/renderer.rs +++ b/src-tauri/yaak-templates/src/renderer.rs @@ -80,10 +80,7 @@ async fn render_tag( match cb.run(name.as_str(), resolved_args.clone()).await { Ok(s) => s, Err(e) => { - warn!( - "Failed to run template callback {}({:?}): {}", - name, resolved_args, e - ); + warn!("Failed to run template callback {}({:?}): {}", name, resolved_args, e); "".to_string() } } @@ -116,10 +113,7 @@ mod tests { let template = ""; let vars = HashMap::new(); let result = ""; - assert_eq!( - parse_and_render(template, &vars, &empty_cb).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &empty_cb).await, result.to_string()); } #[tokio::test] @@ -128,10 +122,7 @@ mod tests { let template = "Hello World!"; let vars = HashMap::new(); let result = "Hello World!"; - assert_eq!( - parse_and_render(template, &vars, &empty_cb).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &empty_cb).await, result.to_string()); } #[tokio::test] @@ -140,10 +131,7 @@ mod tests { let template = "${[ foo ]}"; let vars = HashMap::from([("foo".to_string(), "bar".to_string())]); let result = "bar"; - assert_eq!( - parse_and_render(template, &vars, &empty_cb).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &empty_cb).await, result.to_string()); } #[tokio::test] @@ -156,10 +144,7 @@ mod tests { vars.insert("baz".to_string(), "baz".to_string()); let result = "foo: bar: baz"; - assert_eq!( - parse_and_render(template, &vars, &empty_cb).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &empty_cb).await, result.to_string()); } #[tokio::test] @@ -168,10 +153,7 @@ mod tests { let template = "hello ${[ word ]} world!"; let vars = HashMap::from([("word".to_string(), "cruel".to_string())]); let result = "hello cruel world!"; - assert_eq!( - parse_and_render(template, &vars, &empty_cb).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &empty_cb).await, result.to_string()); } #[tokio::test] @@ -187,12 +169,7 @@ mod tests { fn_name: &str, args: HashMap, ) -> Result { - Ok(format!( - "{fn_name}: {}, {:?} {:?}", - args.len(), - args.get("a"), - args.get("b") - )) + Ok(format!("{fn_name}: {}, {:?} {:?}", args.len(), args.get("a"), args.get("b"))) } } assert_eq!(parse_and_render(template, &vars, &CB {}).await, result); @@ -218,10 +195,7 @@ mod tests { } } - assert_eq!( - parse_and_render(template, &vars, &CB {}).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &CB {}).await, result.to_string()); } #[tokio::test] @@ -241,9 +215,6 @@ mod tests { } } - assert_eq!( - parse_and_render(template, &vars, &CB {}).await, - result.to_string() - ); + assert_eq!(parse_and_render(template, &vars, &CB {}).await, result.to_string()); } }