Преглед изворни кода

refactor: use own invoke return struct instead of serde_json::Value (#1259)

Lucas Fernandes Nogueira пре 4 година
родитељ
комит
33eb49c4c0

+ 1 - 1
cli/tauri.js/templates/src-tauri/src/main.rs

@@ -22,7 +22,7 @@ fn main() {
               println!("{}", argument);
             }
           }
-          Ok(serde_json::Value::Null)
+          Ok(().into())
         }
       }
     })

+ 2 - 2
tauri/examples/api/src-tauri/src/main.rs

@@ -38,11 +38,11 @@ fn main() {
         Ok(command) => match command {
           LogOperation { event, payload } => {
             println!("{} {:?}", event, payload);
-            Ok(serde_json::Value::Null)
+            Ok(().into())
           }
           PerformRequest { endpoint, body } => {
             println!("{} {:?}", endpoint, body);
-            Ok(serde_json::Value::String("message response".to_string()))
+            Ok("message response".into())
           }
         },
       }

+ 2 - 2
tauri/examples/communication/src-tauri/src/main.rs

@@ -39,11 +39,11 @@ fn main() {
         Ok(command) => match command {
           LogOperation { event, payload } => {
             println!("{} {:?}", event, payload);
-            Ok(serde_json::Value::Null)
+            Ok(().into())
           }
           PerformRequest { endpoint, body } => {
             println!("{} {:?}", endpoint, body);
-            Ok(serde_json::Value::String("message response".to_string()))
+            Ok("message response".into())
           }
         },
       }

+ 19 - 4
tauri/src/app.rs

@@ -1,4 +1,5 @@
 use futures::future::BoxFuture;
+use serde::Serialize;
 use serde_json::Value as JsonValue;
 use tauri_api::{config::Config, private::AsTauriContext};
 
@@ -18,8 +19,9 @@ pub use webview::{
 };
 pub use webview_manager::{WebviewDispatcher, WebviewManager};
 
-type InvokeHandler<A> =
-  dyn Fn(WebviewManager<A>, String) -> BoxFuture<'static, crate::Result<JsonValue>> + Send + Sync;
+type InvokeHandler<A> = dyn Fn(WebviewManager<A>, String) -> BoxFuture<'static, crate::Result<InvokeResponse>>
+  + Send
+  + Sync;
 type Setup<A> = dyn Fn(WebviewManager<A>) -> BoxFuture<'static, ()> + Send + Sync;
 
 /// `App` runtime information.
@@ -45,6 +47,19 @@ pub(crate) struct Webview<A: ApplicationExt> {
   pub(crate) url: WindowUrl,
 }
 
+/// The response for a JS `invoke` call.
+pub struct InvokeResponse {
+  json: crate::Result<JsonValue>,
+}
+
+impl<T: Serialize> From<T> for InvokeResponse {
+  fn from(value: T) -> Self {
+    Self {
+      json: serde_json::to_value(value).map_err(Into::into),
+    }
+  }
+}
+
 /// The application runner.
 pub struct App<A: ApplicationExt> {
   /// The JS message handler.
@@ -90,7 +105,7 @@ impl<A: ApplicationExt + 'static> App<A> {
     &self,
     dispatcher: &WebviewManager<A>,
     arg: &JsonValue,
-  ) -> crate::Result<Option<JsonValue>> {
+  ) -> crate::Result<Option<InvokeResponse>> {
     if let Some(ref invoke_handler) = self.invoke_handler {
       let fut = invoke_handler(dispatcher.clone(), arg.to_string());
       fut.await.map(Some)
@@ -196,7 +211,7 @@ impl<A: ApplicationExt + 'static, C: AsTauriContext> AppBuilder<A, C> {
 
   /// Defines the JS message handler callback.
   pub fn invoke_handler<
-    T: futures::Future<Output = crate::Result<JsonValue>> + Send + Sync + 'static,
+    T: futures::Future<Output = crate::Result<InvokeResponse>> + Send + Sync + 'static,
     F: Fn(WebviewManager<A>, String) -> T + Send + Sync + 'static,
   >(
     mut self,

+ 10 - 7
tauri/src/app/utils.rs

@@ -10,6 +10,7 @@ use crate::{
     config::WindowUrl,
     rpc::{format_callback, format_callback_result},
   },
+  app::InvokeResponse,
   ApplicationExt, WebviewBuilderExt,
 };
 
@@ -20,7 +21,7 @@ use super::{
 #[cfg(embedded_server)]
 use crate::api::tcp::{get_available_port, port_is_available};
 
-use serde::{Deserialize, Serialize};
+use serde::Deserialize;
 use serde_json::Value as JsonValue;
 
 #[derive(Debug, Deserialize)]
@@ -330,8 +331,7 @@ pub(super) fn build_webview<A: ApplicationExt + 'static>(
 /// If the Result `is_err()`, the callback will be the `error_callback` function name and the argument will be the Err value.
 async fn execute_promise<
   A: ApplicationExt + 'static,
-  R: Serialize,
-  F: futures::Future<Output = crate::Result<R>> + Send + 'static,
+  F: futures::Future<Output = crate::Result<InvokeResponse>> + Send + 'static,
 >(
   webview_manager: &crate::WebviewManager<A>,
   task: F,
@@ -339,7 +339,10 @@ async fn execute_promise<
   error_callback: String,
 ) {
   let callback_string = match format_callback_result(
-    task.await.map_err(|err| err.to_string()),
+    task
+      .await
+      .and_then(|response| response.json)
+      .map_err(|err| err.to_string()),
     success_callback,
     error_callback.clone(),
   ) {
@@ -355,11 +358,11 @@ async fn on_message<A: ApplicationExt + 'static>(
   application: Arc<App<A>>,
   webview_manager: WebviewManager<A>,
   message: Message,
-) -> crate::Result<JsonValue> {
+) -> crate::Result<InvokeResponse> {
   if message.inner == serde_json::json!({ "cmd":"__initialized" }) {
     application.run_setup(&webview_manager).await;
     crate::plugin::ready(A::plugin_store(), &webview_manager).await;
-    Ok(JsonValue::Null)
+    Ok(().into())
   } else {
     let response = if let Some(module) = &message.tauri_module {
       crate::endpoints::handle(
@@ -386,7 +389,7 @@ async fn on_message<A: ApplicationExt + 'static>(
       if let Err(crate::Error::UnknownApi(_)) = response {
         response = crate::plugin::extend_api(A::plugin_store(), &webview_manager, &message.inner)
           .await
-          .map(|value| value.unwrap_or_default());
+          .map(|value| value.into());
       }
       response
     };

+ 8 - 10
tauri/src/endpoints.rs

@@ -13,9 +13,12 @@ mod notification;
 mod shell;
 mod window;
 
-use crate::{app::Context, ApplicationExt};
+use crate::{
+  app::{Context, InvokeResponse},
+  ApplicationExt,
+};
 
-use serde::{Deserialize, Serialize};
+use serde::Deserialize;
 use serde_json::Value as JsonValue;
 
 #[derive(Deserialize)]
@@ -38,7 +41,7 @@ impl Module {
     self,
     webview_manager: &crate::WebviewManager<A>,
     context: &Context,
-  ) -> crate::Result<JsonValue> {
+  ) -> crate::Result<InvokeResponse> {
     match self {
       Self::Fs(cmd) => cmd.run().await,
       Self::Window(cmd) => cmd.run(webview_manager).await,
@@ -59,15 +62,10 @@ pub(crate) async fn handle<A: ApplicationExt + 'static>(
   module: String,
   mut arg: JsonValue,
   context: &Context,
-) -> crate::Result<JsonValue> {
+) -> crate::Result<InvokeResponse> {
   if let JsonValue::Object(ref mut obj) = arg {
     obj.insert("module".to_string(), JsonValue::String(module));
   }
   let module: Module = serde_json::from_value(arg)?;
-  let response = module.run(webview_manager, context).await?;
-  Ok(serde_json::to_value(response)?)
-}
-
-pub(crate) fn to_value(t: impl Serialize) -> crate::Result<JsonValue> {
-  serde_json::to_value(t).map_err(Into::into)
+  module.run(webview_manager, context).await
 }

+ 3 - 3
tauri/src/endpoints/cli.rs

@@ -1,5 +1,5 @@
+use crate::app::InvokeResponse;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 /// The API descriptor.
 #[derive(Deserialize)]
@@ -11,14 +11,14 @@ pub enum Cmd {
 
 impl Cmd {
   #[allow(unused_variables)]
-  pub async fn run(self, context: &crate::app::Context) -> crate::Result<JsonValue> {
+  pub async fn run(self, context: &crate::app::Context) -> crate::Result<InvokeResponse> {
     match self {
       #[allow(unused_variables)]
       Self::CliMatches => {
         #[cfg(cli)]
         return tauri_api::cli::get_matches(&context.config)
           .map_err(Into::into)
-          .and_then(super::to_value);
+          .map(Into::into);
         #[cfg(not(cli))]
           Err(crate::Error::ApiNotEnabled(
             "CLI definition not set under tauri.conf.json > tauri > cli (https://tauri.studio/docs/api/config#tauri.cli)".to_string(),

+ 15 - 16
tauri/src/endpoints/dialog.rs

@@ -1,8 +1,8 @@
-use crate::api::dialog::{
-  ask as ask_dialog, message as message_dialog, AskResponse, FileDialogBuilder,
+use crate::{
+  api::dialog::{ask as ask_dialog, message as message_dialog, AskResponse, FileDialogBuilder},
+  app::InvokeResponse,
 };
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 use std::path::PathBuf;
 
@@ -63,7 +63,7 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self) -> crate::Result<JsonValue> {
+  pub async fn run(self) -> crate::Result<InvokeResponse> {
     match self {
       Self::OpenDialog { options } => {
         #[cfg(open_dialog)]
@@ -85,7 +85,7 @@ impl Cmd {
           .to_string_lossy()
           .to_string();
         message_dialog(app_name, message);
-        Ok(JsonValue::Null)
+        Ok(().into())
       }
       Self::AskDialog { title, message } => {
         let exe = std::env::current_exe()?;
@@ -99,7 +99,7 @@ impl Cmd {
           }),
           message,
         )?;
-        Ok(JsonValue::Bool(answer))
+        Ok(answer)
       }
     }
   }
@@ -107,7 +107,7 @@ impl Cmd {
 
 /// Shows an open dialog.
 #[cfg(open_dialog)]
-pub fn open(options: OpenDialogOptions) -> crate::Result<JsonValue> {
+pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
   let mut dialog_builder = FileDialogBuilder::new();
   if let Some(default_path) = options.default_path {
     dialog_builder = dialog_builder.set_directory(default_path);
@@ -117,18 +117,18 @@ pub fn open(options: OpenDialogOptions) -> crate::Result<JsonValue> {
     dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
   }
   let response = if options.directory {
-    serde_json::to_value(dialog_builder.pick_folder())?
+    dialog_builder.pick_folder().into()
   } else if options.multiple {
-    serde_json::to_value(dialog_builder.pick_files())?
+    dialog_builder.pick_files().into()
   } else {
-    serde_json::to_value(dialog_builder.pick_file())?
+    dialog_builder.pick_file().into()
   };
   Ok(response)
 }
 
 /// Shows a save dialog.
 #[cfg(save_dialog)]
-pub fn save(options: SaveDialogOptions) -> crate::Result<JsonValue> {
+pub fn save(options: SaveDialogOptions) -> crate::Result<InvokeResponse> {
   let mut dialog_builder = FileDialogBuilder::new();
   if let Some(default_path) = options.default_path {
     dialog_builder = dialog_builder.set_directory(default_path);
@@ -137,14 +137,13 @@ pub fn save(options: SaveDialogOptions) -> crate::Result<JsonValue> {
     let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
     dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
   }
-  let response = dialog_builder.save_file();
-  Ok(serde_json::to_value(response)?)
+  Ok(dialog_builder.save_file().into())
 }
 
 /// Shows a dialog with a yes/no question.
-pub fn ask(title: String, message: String) -> crate::Result<bool> {
+pub fn ask(title: String, message: String) -> crate::Result<InvokeResponse> {
   match ask_dialog(title, message) {
-    AskResponse::Yes => Ok(true),
-    _ => Ok(false),
+    AskResponse::Yes => Ok(true.into()),
+    _ => Ok(false.into()),
   }
 }

+ 3 - 4
tauri/src/endpoints/event.rs

@@ -1,5 +1,5 @@
+use crate::app::InvokeResponse;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 /// The API descriptor.
 #[derive(Deserialize)]
@@ -26,7 +26,7 @@ impl Cmd {
   pub async fn run<A: crate::ApplicationExt + 'static>(
     self,
     webview_manager: &crate::WebviewManager<A>,
-  ) -> crate::Result<JsonValue> {
+  ) -> crate::Result<InvokeResponse> {
     match self {
       Self::Listen {
         event,
@@ -35,7 +35,6 @@ impl Cmd {
       } => {
         let js_string = listen_fn(event, handler, once)?;
         webview_manager.current_webview().await?.eval(&js_string)?;
-        Ok(JsonValue::Null)
       }
       Self::Emit {
         event,
@@ -54,9 +53,9 @@ impl Cmd {
           // dispatch the event to JS listeners
           webview_manager.emit(event, payload).await?;
         }
-        Ok(JsonValue::Null)
       }
     }
+    Ok(().into())
   }
 }
 

+ 13 - 22
tauri/src/endpoints/file_system.rs

@@ -1,7 +1,6 @@
-use crate::{api::path::BaseDirectory, ApplicationDispatcherExt};
+use crate::{api::path::BaseDirectory, app::InvokeResponse, ApplicationDispatcherExt};
 
 use serde::{Deserialize, Serialize};
-use serde_json::Value as JsonValue;
 use tauri_api::{dir, file, path::resolve_path};
 
 use std::{fs, fs::File, io::Write, path::PathBuf};
@@ -92,21 +91,17 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self) -> crate::Result<JsonValue> {
+  pub async fn run(self) -> crate::Result<InvokeResponse> {
     match self {
       Self::ReadTextFile { path, options } => {
         #[cfg(read_text_file)]
-        return read_text_file(path, options)
-          .await
-          .and_then(super::to_value);
+        return read_text_file(path, options).await.map(Into::into);
         #[cfg(not(read_text_file))]
         Err(crate::Error::ApiNotAllowlisted("readTextFile".to_string()))
       }
       Self::ReadBinaryFile { path, options } => {
         #[cfg(read_binary_file)]
-        return read_binary_file(path, options)
-          .await
-          .and_then(super::to_value);
+        return read_binary_file(path, options).await.map(Into::into);
         #[cfg(not(read_binary_file))]
         Err(crate::Error::ApiNotAllowlisted(
           "readBinaryFile".to_string(),
@@ -118,9 +113,7 @@ impl Cmd {
         options,
       } => {
         #[cfg(write_file)]
-        return write_file(path, contents, options)
-          .await
-          .and_then(super::to_value);
+        return write_file(path, contents, options).await.map(Into::into);
         #[cfg(not(write_file))]
         Err(crate::Error::ApiNotAllowlisted("writeFile".to_string()))
       }
@@ -132,7 +125,7 @@ impl Cmd {
         #[cfg(write_binary_file)]
         return write_binary_file(path, contents, options)
           .await
-          .and_then(super::to_value);
+          .map(Into::into);
         #[cfg(not(write_binary_file))]
         Err(crate::Error::ApiNotAllowlisted(
           "writeBinaryFile".to_string(),
@@ -140,7 +133,7 @@ impl Cmd {
       }
       Self::ReadDir { path, options } => {
         #[cfg(read_dir)]
-        return read_dir(path, options).await.and_then(super::to_value);
+        return read_dir(path, options).await.map(Into::into);
         #[cfg(not(read_dir))]
         Err(crate::Error::ApiNotAllowlisted("readDir".to_string()))
       }
@@ -152,25 +145,25 @@ impl Cmd {
         #[cfg(copy_file)]
         return copy_file(source, destination, options)
           .await
-          .and_then(super::to_value);
+          .map(Into::into);
         #[cfg(not(copy_file))]
         Err(crate::Error::ApiNotAllowlisted("copyFile".to_string()))
       }
       Self::CreateDir { path, options } => {
         #[cfg(create_dir)]
-        return create_dir(path, options).await.and_then(super::to_value);
+        return create_dir(path, options).await.map(Into::into);
         #[cfg(not(create_dir))]
         Err(crate::Error::ApiNotAllowlisted("createDir".to_string()))
       }
       Self::RemoveDir { path, options } => {
         #[cfg(remove_dir)]
-        return remove_dir(path, options).await.and_then(super::to_value);
+        return remove_dir(path, options).await.map(Into::into);
         #[cfg(not(remove_dir))]
         Err(crate::Error::ApiNotAllowlisted("removeDir".to_string()))
       }
       Self::RemoveFile { path, options } => {
         #[cfg(remove_file)]
-        return remove_file(path, options).await.and_then(super::to_value);
+        return remove_file(path, options).await.map(Into::into);
         #[cfg(not(remove_file))]
         Err(crate::Error::ApiNotAllowlisted("removeFile".to_string()))
       }
@@ -182,15 +175,13 @@ impl Cmd {
         #[cfg(rename_file)]
         return rename_file(old_path, new_path, options)
           .await
-          .and_then(super::to_value);
+          .map(Into::into);
         #[cfg(not(rename_file))]
         Err(crate::Error::ApiNotAllowlisted("renameFile".to_string()))
       }
       Self::ResolvePath { path, directory } => {
         #[cfg(path_api)]
-        return resolve_path_handler(path, directory)
-          .await
-          .and_then(super::to_value);
+        return resolve_path_handler(path, directory).await.map(Into::into);
         #[cfg(not(path_api))]
         Err(crate::Error::ApiNotAllowlisted("pathApi".to_string()))
       }

+ 3 - 5
tauri/src/endpoints/global_shortcut.rs

@@ -1,7 +1,6 @@
-use crate::{api::shortcuts::ShortcutManager, async_runtime::Mutex};
+use crate::{api::shortcuts::ShortcutManager, app::InvokeResponse, async_runtime::Mutex};
 use once_cell::sync::Lazy;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 use std::sync::Arc;
 
@@ -26,7 +25,7 @@ impl Cmd {
   pub async fn run<A: crate::ApplicationExt + 'static>(
     self,
     webview_manager: &crate::WebviewManager<A>,
-  ) -> crate::Result<JsonValue> {
+  ) -> crate::Result<InvokeResponse> {
     #[cfg(not(global_shortcut))]
     return Err(crate::Error::ApiNotAllowlisted(
       "globalShortcut".to_string(),
@@ -41,13 +40,12 @@ impl Cmd {
             crate::api::rpc::format_callback(handler.to_string(), serde_json::Value::Null);
           let _ = dispatcher.eval(callback_string.as_str());
         })?;
-        Ok(JsonValue::Null)
       }
       Self::Unregister { shortcut } => {
         let mut manager = manager_handle().lock().await;
         manager.unregister_shortcut(shortcut)?;
-        Ok(JsonValue::Null)
       }
     }
+    Ok(().into())
   }
 }

+ 5 - 8
tauri/src/endpoints/http.rs

@@ -1,8 +1,7 @@
-use crate::async_runtime::Mutex;
+use crate::{app::InvokeResponse, async_runtime::Mutex};
 
 use once_cell::sync::Lazy;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 use tauri_api::http::{Client, ClientBuilder, HttpRequestBuilder, ResponseData};
 
 use std::{collections::HashMap, sync::Arc};
@@ -31,25 +30,23 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self) -> crate::Result<JsonValue> {
+  pub async fn run(self) -> crate::Result<InvokeResponse> {
     match self {
       Self::CreateClient { options } => {
         let client = options.unwrap_or_default().build()?;
         let mut store = clients().lock().await;
         let id = rand::random::<ClientId>();
         store.insert(id, client);
-        Ok(JsonValue::Number(id.into()))
+        Ok(id.into())
       }
       Self::DropClient { client } => {
         let mut store = clients().lock().await;
         store.remove(&client);
-        Ok(JsonValue::Null)
+        Ok(().into())
       }
       Self::HttpRequest { client, options } => {
         #[cfg(http_request)]
-        return make_request(client, *options)
-          .await
-          .and_then(super::to_value);
+        return make_request(client, *options).await.map(Into::into);
         #[cfg(not(http_request))]
         Err(crate::Error::ApiNotAllowlisted("httpRequest".to_string()))
       }

+ 4 - 4
tauri/src/endpoints/internal.rs

@@ -1,5 +1,5 @@
+use crate::app::InvokeResponse;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 /// The API descriptor.
 #[derive(Deserialize)]
@@ -9,7 +9,7 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self) -> crate::Result<JsonValue> {
+  pub async fn run(self) -> crate::Result<InvokeResponse> {
     match self {
       Self::ValidateSalt { salt } => validate_salt(salt),
     }
@@ -17,6 +17,6 @@ impl Cmd {
 }
 
 /// Validates a salt.
-pub fn validate_salt(salt: String) -> crate::Result<JsonValue> {
-  Ok(JsonValue::Bool(crate::salt::is_valid(salt)))
+pub fn validate_salt(salt: String) -> crate::Result<InvokeResponse> {
+  Ok(crate::salt::is_valid(salt).into())
 }

+ 10 - 12
tauri/src/endpoints/notification.rs

@@ -1,5 +1,5 @@
+use crate::app::InvokeResponse;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 use tauri_api::{config::Config, notification::Notification};
 
 /// The options for the notification API.
@@ -26,25 +26,23 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self, context: &crate::app::Context) -> crate::Result<JsonValue> {
+  pub async fn run(self, context: &crate::app::Context) -> crate::Result<InvokeResponse> {
     match self {
       Self::Notification { options } => {
         #[cfg(notification)]
-        return send(options, &context.config)
-          .await
-          .and_then(super::to_value);
+        return send(options, &context.config).await.map(Into::into);
         #[cfg(not(notification))]
         Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
       }
       Self::IsNotificationPermissionGranted => {
         #[cfg(notification)]
-        return is_permission_granted().await.and_then(super::to_value);
+        return is_permission_granted().await.map(Into::into);
         #[cfg(not(notification))]
         Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
       }
       Self::RequestNotificationPermission => {
         #[cfg(notification)]
-        return request_permission().map(JsonValue::String);
+        return request_permission().map(Into::into);
         #[cfg(not(notification))]
         Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
       }
@@ -52,7 +50,7 @@ impl Cmd {
   }
 }
 
-pub async fn send(options: NotificationOptions, config: &Config) -> crate::Result<JsonValue> {
+pub async fn send(options: NotificationOptions, config: &Config) -> crate::Result<InvokeResponse> {
   let identifier = config.tauri.bundle.identifier.clone();
   let mut notification = Notification::new(identifier).title(options.title);
   if let Some(body) = options.body {
@@ -62,15 +60,15 @@ pub async fn send(options: NotificationOptions, config: &Config) -> crate::Resul
     notification = notification.icon(icon);
   }
   notification.show()?;
-  Ok(JsonValue::Null)
+  Ok(().into())
 }
 
-pub async fn is_permission_granted() -> crate::Result<JsonValue> {
+pub async fn is_permission_granted() -> crate::Result<InvokeResponse> {
   let settings = crate::settings::read_settings()?;
   if let Some(allow_notification) = settings.allow_notification {
-    Ok(JsonValue::String(allow_notification.to_string()))
+    Ok(allow_notification.into())
   } else {
-    Ok(JsonValue::Null)
+    Ok(().into())
   }
 }
 

+ 4 - 4
tauri/src/endpoints/shell.rs

@@ -1,5 +1,5 @@
+use crate::app::InvokeResponse;
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 /// The API descriptor.
 #[derive(Deserialize)]
@@ -12,7 +12,7 @@ pub enum Cmd {
 }
 
 impl Cmd {
-  pub async fn run(self) -> crate::Result<JsonValue> {
+  pub async fn run(self) -> crate::Result<InvokeResponse> {
     match self {
       Self::Execute {
         command: _,
@@ -21,7 +21,7 @@ impl Cmd {
         #[cfg(execute)]
         {
           //TODO
-          Ok(JsonValue::Null)
+          Ok(().into())
         }
         #[cfg(not(execute))]
         Err(crate::Error::ApiNotAllowlisted("execute".to_string()))
@@ -30,7 +30,7 @@ impl Cmd {
         #[cfg(open)]
         {
           open_browser(uri);
-          Ok(JsonValue::Null)
+          Ok(().into())
         }
         #[cfg(not(open))]
         Err(crate::Error::ApiNotAllowlisted("open".to_string()))

+ 3 - 4
tauri/src/endpoints/window.rs

@@ -1,6 +1,5 @@
-use crate::app::{ApplicationExt, Icon};
+use crate::app::{ApplicationExt, Icon, InvokeResponse};
 use serde::Deserialize;
-use serde_json::Value as JsonValue;
 
 #[derive(Deserialize)]
 #[serde(untagged)]
@@ -95,7 +94,7 @@ impl Cmd {
   pub async fn run<A: ApplicationExt + 'static>(
     self,
     webview_manager: &crate::WebviewManager<A>,
-  ) -> crate::Result<JsonValue> {
+  ) -> crate::Result<InvokeResponse> {
     if cfg!(not(window)) {
       Err(crate::Error::ApiNotAllowlisted("setTitle".to_string()))
     } else {
@@ -151,7 +150,7 @@ impl Cmd {
         Self::SetFullscreen { fullscreen } => current_webview.set_fullscreen(fullscreen)?,
         Self::SetIcon { icon } => current_webview.set_icon(icon.into())?,
       }
-      Ok(JsonValue::Null)
+      Ok(().into())
     }
   }
 }