lib.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use crate::context::ContextItems;
  5. use proc_macro::TokenStream;
  6. use syn::{parse_macro_input, DeriveInput, ItemFn};
  7. mod command;
  8. mod command_module;
  9. mod runtime;
  10. #[macro_use]
  11. mod context;
  12. /// Mark a function as a command handler. It creates a wrapper function with the necessary glue code.
  13. ///
  14. /// # Stability
  15. /// The output of this macro is managed internally by Tauri,
  16. /// and should not be accessed directly on normal applications.
  17. /// It may have breaking changes in the future.
  18. #[proc_macro_attribute]
  19. pub fn command(attributes: TokenStream, item: TokenStream) -> TokenStream {
  20. command::wrapper(attributes, item)
  21. }
  22. /// Accepts a list of commands functions. Creates a handler that allows commands to be called from JS with invoke().
  23. ///
  24. /// # Examples
  25. /// ```rust,ignore
  26. /// use tauri_macros::{command, generate_handler};
  27. /// #[command]
  28. /// fn command_one() {
  29. /// println!("command one called");
  30. /// }
  31. /// #[command]
  32. /// fn command_two() {
  33. /// println!("command two called");
  34. /// }
  35. /// fn main() {
  36. /// let _handler = generate_handler![command_one, command_two];
  37. /// }
  38. /// ```
  39. /// # Stability
  40. /// The output of this macro is managed internally by Tauri,
  41. /// and should not be accessed directly on normal applications.
  42. /// It may have breaking changes in the future.
  43. #[proc_macro]
  44. pub fn generate_handler(item: TokenStream) -> TokenStream {
  45. parse_macro_input!(item as command::Handler).into()
  46. }
  47. /// Reads a Tauri config file and generates a `::tauri::Context` based on the content.
  48. ///
  49. /// # Stability
  50. /// The output of this macro is managed internally by Tauri,
  51. /// and should not be accessed directly on normal applications.
  52. /// It may have breaking changes in the future.
  53. #[proc_macro]
  54. pub fn generate_context(items: TokenStream) -> TokenStream {
  55. // this macro is exported from the context module
  56. let path = parse_macro_input!(items as ContextItems);
  57. context::generate_context(path).into()
  58. }
  59. /// Adds the default type for the last parameter (assumed to be runtime) for a specific feature.
  60. ///
  61. /// e.g. To default the runtime generic to type `crate::Wry` when the `wry` feature is enabled, the
  62. /// syntax would look like `#[default_runtime(crate::Wry, wry)`. This is **always** set for the last
  63. /// generic, so make sure the last generic is the runtime when using this macro.
  64. #[doc(hidden)]
  65. #[proc_macro_attribute]
  66. pub fn default_runtime(attributes: TokenStream, input: TokenStream) -> TokenStream {
  67. let attributes = parse_macro_input!(attributes as runtime::Attributes);
  68. let input = parse_macro_input!(input as DeriveInput);
  69. runtime::default_runtime(attributes, input).into()
  70. }
  71. /// Prepares the command module enum.
  72. #[doc(hidden)]
  73. #[proc_macro_derive(CommandModule, attributes(cmd))]
  74. pub fn derive_command_module(input: TokenStream) -> TokenStream {
  75. let input = parse_macro_input!(input as DeriveInput);
  76. command_module::generate_run_fn(input)
  77. }
  78. /// Adds a `run` method to an enum (one of the tauri endpoint modules).
  79. /// The `run` method takes a `tauri::endpoints::InvokeContext`
  80. /// and returns a `tauri::Result<tauri::endpoints::InvokeResponse>`.
  81. /// It matches on each enum variant and call a method with name equal to the variant name, lowercased and snake_cased,
  82. /// passing the context and the variant's fields as arguments.
  83. /// That function must also return the same `Result<InvokeResponse>`.
  84. #[doc(hidden)]
  85. #[proc_macro_attribute]
  86. pub fn command_enum(_: TokenStream, input: TokenStream) -> TokenStream {
  87. let input = parse_macro_input!(input as DeriveInput);
  88. command_module::generate_command_enum(input)
  89. }
  90. #[doc(hidden)]
  91. #[proc_macro_attribute]
  92. pub fn module_command_handler(attributes: TokenStream, input: TokenStream) -> TokenStream {
  93. let attributes = parse_macro_input!(attributes as command_module::HandlerAttributes);
  94. let input = parse_macro_input!(input as ItemFn);
  95. command_module::command_handler(attributes, input).into()
  96. }
  97. #[doc(hidden)]
  98. #[proc_macro_attribute]
  99. pub fn module_command_test(attributes: TokenStream, input: TokenStream) -> TokenStream {
  100. let attributes = parse_macro_input!(attributes as command_module::HandlerTestAttributes);
  101. let input = parse_macro_input!(input as ItemFn);
  102. command_module::command_test(attributes, input).into()
  103. }