endpoints.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use crate::{api::config::Config, hooks::InvokeMessage, runtime::Params};
  2. use serde::{Deserialize, Serialize};
  3. use serde_json::Value as JsonValue;
  4. mod cli;
  5. mod dialog;
  6. mod event;
  7. #[allow(unused_imports)]
  8. mod file_system;
  9. mod global_shortcut;
  10. mod http;
  11. mod internal;
  12. mod notification;
  13. mod shell;
  14. mod window;
  15. /// The response for a JS `invoke` call.
  16. pub struct InvokeResponse {
  17. json: crate::Result<JsonValue>,
  18. }
  19. impl<T: Serialize> From<T> for InvokeResponse {
  20. fn from(value: T) -> Self {
  21. Self {
  22. json: serde_json::to_value(value).map_err(Into::into),
  23. }
  24. }
  25. }
  26. #[derive(Deserialize)]
  27. #[serde(tag = "module", content = "message")]
  28. enum Module {
  29. Fs(file_system::Cmd),
  30. Window(window::Cmd),
  31. Shell(shell::Cmd),
  32. Event(event::Cmd),
  33. Internal(internal::Cmd),
  34. Dialog(dialog::Cmd),
  35. Cli(cli::Cmd),
  36. Notification(notification::Cmd),
  37. Http(http::Cmd),
  38. GlobalShortcut(global_shortcut::Cmd),
  39. }
  40. impl Module {
  41. fn run<M: Params>(self, message: InvokeMessage<M>, config: &Config) {
  42. let window = message.window();
  43. match self {
  44. Self::Fs(cmd) => message
  45. .respond_async(async move { cmd.run().and_then(|r| r.json).map_err(|e| e.to_string()) }),
  46. Self::Window(cmd) => message.respond_async(async move {
  47. cmd
  48. .run(window)
  49. .await
  50. .and_then(|r| r.json)
  51. .map_err(|e| e.to_string())
  52. }),
  53. Self::Shell(cmd) => message.respond_async(async move {
  54. cmd
  55. .run(window)
  56. .and_then(|r| r.json)
  57. .map_err(|e| e.to_string())
  58. }),
  59. Self::Event(cmd) => message.respond_async(async move {
  60. cmd
  61. .run(window)
  62. .and_then(|r| r.json)
  63. .map_err(|e| e.to_string())
  64. }),
  65. Self::Internal(cmd) => message.respond_async(async move {
  66. cmd
  67. .run(window)
  68. .and_then(|r| r.json)
  69. .map_err(|e| e.to_string())
  70. }),
  71. Self::Dialog(cmd) => message
  72. .respond_async(async move { cmd.run().and_then(|r| r.json).map_err(|e| e.to_string()) }),
  73. Self::Cli(cmd) => {
  74. if let Some(cli_config) = config.tauri.cli.clone() {
  75. message.respond_async(async move {
  76. cmd
  77. .run(&cli_config)
  78. .and_then(|r| r.json)
  79. .map_err(|e| e.to_string())
  80. })
  81. }
  82. }
  83. Self::Notification(cmd) => {
  84. let identifier = config.tauri.bundle.identifier.clone();
  85. message.respond_async(async move {
  86. cmd
  87. .run(identifier)
  88. .and_then(|r| r.json)
  89. .map_err(|e| e.to_string())
  90. })
  91. }
  92. Self::Http(cmd) => message.respond_async(async move {
  93. cmd
  94. .run()
  95. .await
  96. .and_then(|r| r.json)
  97. .map_err(|e| e.to_string())
  98. }),
  99. Self::GlobalShortcut(cmd) => message.respond_async(async move {
  100. cmd
  101. .run(window)
  102. .and_then(|r| r.json)
  103. .map_err(|e| e.to_string())
  104. }),
  105. }
  106. }
  107. }
  108. pub(crate) fn handle<M: Params>(module: String, message: InvokeMessage<M>, config: &Config) {
  109. let mut payload = message.payload();
  110. if let JsonValue::Object(ref mut obj) = payload {
  111. obj.insert("module".to_string(), JsonValue::String(module));
  112. }
  113. match serde_json::from_value::<Module>(payload) {
  114. Ok(module) => module.run(message, config),
  115. Err(e) => message.reject(e.to_string()),
  116. }
  117. }