main.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(
  5. all(not(debug_assertions), target_os = "windows"),
  6. windows_subsystem = "windows"
  7. )]
  8. // we move some basic commands to a separate module just to show it works
  9. mod commands;
  10. use commands::{cmd, invoke, message, resolver};
  11. use serde::Deserialize;
  12. use tauri::{command, State, Window};
  13. #[derive(Debug)]
  14. pub struct MyState {
  15. #[allow(dead_code)]
  16. value: u64,
  17. #[allow(dead_code)]
  18. label: String,
  19. }
  20. #[derive(Debug, serde::Serialize)]
  21. enum MyError {
  22. FooError,
  23. }
  24. // ------------------------ Commands using Window ------------------------
  25. #[command]
  26. fn window_label(window: Window) {
  27. println!("window label: {}", window.label());
  28. }
  29. // Async commands
  30. #[command]
  31. async fn async_simple_command(argument: String) {
  32. println!("{}", argument);
  33. }
  34. #[command]
  35. async fn async_stateful_command(
  36. argument: Option<String>,
  37. state: State<'_, MyState>,
  38. ) -> Result<(), ()> {
  39. println!("{:?} {:?}", argument, state.inner());
  40. Ok(())
  41. }
  42. // Raw future commands
  43. #[command(async)]
  44. fn future_simple_command(argument: String) -> impl std::future::Future<Output = ()> {
  45. println!("{}", argument);
  46. std::future::ready(())
  47. }
  48. #[command(async)]
  49. fn future_simple_command_with_return(
  50. argument: String,
  51. ) -> impl std::future::Future<Output = String> {
  52. println!("{}", argument);
  53. std::future::ready(argument)
  54. }
  55. #[command(async)]
  56. fn future_simple_command_with_result(
  57. argument: String,
  58. ) -> impl std::future::Future<Output = Result<String, ()>> {
  59. println!("{}", argument);
  60. std::future::ready(Ok(argument))
  61. }
  62. #[command(async)]
  63. fn force_async(argument: String) -> String {
  64. argument
  65. }
  66. #[command(async)]
  67. fn force_async_with_result(argument: &str) -> Result<&str, MyError> {
  68. (!argument.is_empty())
  69. .then(|| argument)
  70. .ok_or(MyError::FooError)
  71. }
  72. // ------------------------ Commands returning Result ------------------------
  73. #[command]
  74. fn simple_command_with_result(argument: String) -> Result<String, MyError> {
  75. println!("{}", argument);
  76. (!argument.is_empty())
  77. .then(|| argument)
  78. .ok_or(MyError::FooError)
  79. }
  80. #[command]
  81. fn stateful_command_with_result(
  82. argument: Option<String>,
  83. state: State<'_, MyState>,
  84. ) -> Result<String, MyError> {
  85. println!("{:?} {:?}", argument, state.inner());
  86. dbg!(argument.ok_or(MyError::FooError))
  87. }
  88. // Async commands
  89. #[command]
  90. async fn async_simple_command_with_result(argument: String) -> Result<String, MyError> {
  91. println!("{}", argument);
  92. Ok(argument)
  93. }
  94. #[command]
  95. async fn async_stateful_command_with_result(
  96. argument: Option<String>,
  97. state: State<'_, MyState>,
  98. ) -> Result<String, MyError> {
  99. println!("{:?} {:?}", argument, state.inner());
  100. Ok(argument.unwrap_or_else(|| "".to_string()))
  101. }
  102. // Non-Ident command function arguments
  103. #[command]
  104. fn command_arguments_wild(_: Window) {
  105. println!("we saw the wildcard!")
  106. }
  107. #[derive(Deserialize)]
  108. struct Person<'a> {
  109. name: &'a str,
  110. age: u8,
  111. }
  112. #[command]
  113. fn command_arguments_struct(Person { name, age }: Person<'_>) {
  114. println!("received person struct with name: {} | age: {}", name, age)
  115. }
  116. #[derive(Deserialize)]
  117. struct InlinePerson<'a>(&'a str, u8);
  118. #[command]
  119. fn command_arguments_tuple_struct(InlinePerson(name, age): InlinePerson<'_>) {
  120. println!("received person tuple with name: {} | age: {}", name, age)
  121. }
  122. #[command]
  123. fn borrow_cmd(argument: &str) -> &str {
  124. argument
  125. }
  126. #[command]
  127. fn borrow_cmd_async(argument: &str) -> &str {
  128. argument
  129. }
  130. fn main() {
  131. let context = tauri::generate_context!("../../examples/commands/tauri.conf.json");
  132. tauri::Builder::default()
  133. .menu(tauri::Menu::os_default(&context.package_info().name))
  134. .manage(MyState {
  135. value: 0,
  136. label: "Tauri!".into(),
  137. })
  138. .invoke_handler(tauri::generate_handler![
  139. borrow_cmd,
  140. borrow_cmd_async,
  141. window_label,
  142. force_async,
  143. force_async_with_result,
  144. commands::simple_command,
  145. commands::stateful_command,
  146. cmd,
  147. invoke,
  148. message,
  149. resolver,
  150. async_simple_command,
  151. future_simple_command,
  152. async_stateful_command,
  153. command_arguments_wild,
  154. command_arguments_struct,
  155. simple_command_with_result,
  156. stateful_command_with_result,
  157. command_arguments_tuple_struct,
  158. async_simple_command_with_result,
  159. future_simple_command_with_return,
  160. future_simple_command_with_result,
  161. async_stateful_command_with_result,
  162. ])
  163. .run(context)
  164. .expect("error while running tauri application");
  165. }