|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::TokenStream as TokenStream2; |
| 3 | +use quote::quote; |
| 4 | +use syn::{ |
| 5 | + parse::{Parse, ParseStream}, |
| 6 | + parse_macro_input, Attribute, Path, Token, |
| 7 | +}; |
| 8 | + |
| 9 | +struct CommandItem { |
| 10 | + /// The tokens inside `#[cfg(...)]`, e.g. `desktop` or `target_os = "windows"`. |
| 11 | + /// `None` means the command is always compiled in. |
| 12 | + cfg_tokens: Option<TokenStream2>, |
| 13 | + path: Path, |
| 14 | +} |
| 15 | + |
| 16 | +struct CommandList(Vec<CommandItem>); |
| 17 | + |
| 18 | +impl Parse for CommandList { |
| 19 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 20 | + let mut items = vec![]; |
| 21 | + while !input.is_empty() { |
| 22 | + let attrs = Attribute::parse_outer(input)?; |
| 23 | + let path: Path = input.parse()?; |
| 24 | + |
| 25 | + // Extract the first #[cfg(...)] attribute if present. |
| 26 | + // Any other attributes are ignored (they wouldn't make sense here anyway). |
| 27 | + let cfg_tokens = attrs.iter().find_map(|attr| { |
| 28 | + if !attr.path().is_ident("cfg") { |
| 29 | + return None; |
| 30 | + } |
| 31 | + attr.meta |
| 32 | + .require_list() |
| 33 | + .ok() |
| 34 | + .map(|list| list.tokens.clone()) |
| 35 | + }); |
| 36 | + |
| 37 | + items.push(CommandItem { cfg_tokens, path }); |
| 38 | + |
| 39 | + // Consume optional trailing comma |
| 40 | + if input.peek(Token![,]) { |
| 41 | + let _ = input.parse::<Token![,]>(); |
| 42 | + } |
| 43 | + } |
| 44 | + Ok(CommandList(items)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// A drop-in replacement for `tauri_specta::collect_commands!` that supports |
| 49 | +/// `#[cfg(...)]` attributes on individual commands. |
| 50 | +/// |
| 51 | +/// # Example |
| 52 | +/// ```rust |
| 53 | +/// collect_commands![ |
| 54 | +/// #[cfg(desktop)] |
| 55 | +/// desktop_tray::set_close_to_tray_enabled, |
| 56 | +/// windows::snap_overlay::show_snap_overlay, |
| 57 | +/// windows::snap_overlay::hide_snap_overlay, |
| 58 | +/// ] |
| 59 | +/// ``` |
| 60 | +/// |
| 61 | +/// # How it works |
| 62 | +/// |
| 63 | +/// For each unique cfg predicate P found in the list the macro generates two |
| 64 | +/// complete `tauri_specta::internal::command(generate_handler![...], |
| 65 | +/// collect_functions![...])` calls — one for `#[cfg(P)]` (including those |
| 66 | +/// commands) and one for `#[cfg(not(P))]` (excluding them). The compiler |
| 67 | +/// picks exactly one branch per target, so every command path only needs to |
| 68 | +/// exist on the targets where its cfg condition is true. |
| 69 | +/// |
| 70 | +/// For N distinct predicates, 2^N branches are emitted. In practice only |
| 71 | +/// `#[cfg(desktop)]` is used so this is always just two branches. |
| 72 | +#[proc_macro] |
| 73 | +pub fn collect_commands(input: TokenStream) -> TokenStream { |
| 74 | + let CommandList(items) = parse_macro_input!(input as CommandList); |
| 75 | + |
| 76 | + // Collect the unique cfg predicates present in this invocation. |
| 77 | + let mut predicates: Vec<TokenStream2> = vec![]; |
| 78 | + for item in &items { |
| 79 | + if let Some(cfg) = &item.cfg_tokens { |
| 80 | + let key = cfg.to_string(); |
| 81 | + if !predicates |
| 82 | + .iter() |
| 83 | + .any(|p: &TokenStream2| p.to_string() == key) |
| 84 | + { |
| 85 | + predicates.push(cfg.clone()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + let n = predicates.len(); |
| 91 | + let num_variants = 1usize << n; // 2^n — always at least 1 |
| 92 | + |
| 93 | + let mut branches: Vec<TokenStream2> = vec![]; |
| 94 | + |
| 95 | + for variant in 0..num_variants { |
| 96 | + // For variant `v`, bit `i` being set means predicate[i] is "active" |
| 97 | + // (true) for this branch. |
| 98 | + |
| 99 | + // Build `#[cfg(all(pred0_or_not, pred1_or_not, ...))]` |
| 100 | + let conditions: Vec<TokenStream2> = predicates |
| 101 | + .iter() |
| 102 | + .enumerate() |
| 103 | + .map(|(i, pred)| { |
| 104 | + if variant & (1 << i) != 0 { |
| 105 | + quote! { #pred } |
| 106 | + } else { |
| 107 | + quote! { not(#pred) } |
| 108 | + } |
| 109 | + }) |
| 110 | + .collect(); |
| 111 | + |
| 112 | + let cfg_guard: TokenStream2 = if conditions.is_empty() { |
| 113 | + // No predicates at all — unconditional (wrapping in all() is valid). |
| 114 | + quote! {} |
| 115 | + } else { |
| 116 | + quote! { #[cfg(all(#(#conditions),*))] } |
| 117 | + }; |
| 118 | + |
| 119 | + // Collect commands that are visible in this variant: |
| 120 | + // - always-on commands (no cfg attribute) are always included |
| 121 | + // - cfg-gated commands are included only when their predicate bit is set |
| 122 | + let variant_paths: Vec<&Path> = items |
| 123 | + .iter() |
| 124 | + .filter(|item| match &item.cfg_tokens { |
| 125 | + None => true, // always-on |
| 126 | + Some(cfg) => { |
| 127 | + let key = cfg.to_string(); |
| 128 | + let idx = predicates |
| 129 | + .iter() |
| 130 | + .position(|p| p.to_string() == key) |
| 131 | + .unwrap(); |
| 132 | + variant & (1 << idx) != 0 |
| 133 | + } |
| 134 | + }) |
| 135 | + .map(|item| &item.path) |
| 136 | + .collect(); |
| 137 | + |
| 138 | + branches.push(quote! { |
| 139 | + #cfg_guard |
| 140 | + let __commands = ::tauri_specta::internal::command( |
| 141 | + ::tauri::generate_handler![#(#variant_paths),*], |
| 142 | + ::specta::function::collect_functions![#(#variant_paths),*], |
| 143 | + ); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + quote! { |
| 148 | + { |
| 149 | + #(#branches)* |
| 150 | + __commands |
| 151 | + } |
| 152 | + } |
| 153 | + .into() |
| 154 | +} |
0 commit comments