junobuild_macros/lib.rs
1#![doc = include_str!("../README.md")]
2
3extern crate proc_macro;
4
5#[doc(hidden)]
6mod error;
7#[doc(hidden)]
8mod parser;
9
10use parser::{hook_macro, Hook};
11use proc_macro::TokenStream;
12
13/// The `on_set_doc` function is a procedural macro attribute for hooking into the `OnSetDoc` event.
14/// It allows you to define custom logic to be executed when a document is set.
15///
16/// Example:
17///
18/// ```rust
19/// #[on_set_doc]
20/// async fn on_set_doc(context: OnSetDocContext) -> Result<(), String> {
21/// // Your hook logic here
22/// }
23/// ```
24///
25/// When no attributes are provided, the hook is triggered for any document set within any collection.
26/// You can scope the events to a particular list of collections.
27///
28/// Example:
29/// ```rust
30/// #[on_set_doc(collections = ["demo"])]
31/// async fn on_set_doc(context: OnSetDocContext) -> Result<(), String> {
32/// // Your hook logic here
33/// }
34/// ```
35///
36/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
37///
38#[proc_macro_attribute]
39pub fn on_set_doc(attr: TokenStream, item: TokenStream) -> TokenStream {
40 hook_macro(Hook::OnSetDoc, attr, item)
41}
42
43/// The `on_set_many_docs` function is a procedural macro attribute for hooking into the `OnSetManyDocs` event.
44/// It allows you to define custom logic to be executed when multiple documents are set.
45///
46/// Example:
47///
48/// ```rust
49/// #[on_set_many_docs]
50/// async fn on_set_many_docs(context: OnSetManyDocsContext) -> Result<(), String> {
51/// // Your hook logic here
52/// }
53/// ```
54///
55/// When no attributes are provided, the hook is triggered for any document set within any collection.
56/// You can scope the events to a particular list of collections.
57///
58/// Example:
59/// ```rust
60/// #[on_set_many_docs(collections = ["demo"])]
61/// async fn on_set_many_docs(context: OnSetManyDocsContext) -> Result<(), String> {
62/// // Your hook logic here
63/// }
64/// ```
65///
66/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
67///
68#[proc_macro_attribute]
69pub fn on_set_many_docs(attr: TokenStream, item: TokenStream) -> TokenStream {
70 hook_macro(Hook::OnSetManyDocs, attr, item)
71}
72
73/// The `on_delete_doc` function is a procedural macro attribute for hooking into the `OnDeleteDoc` event.
74/// It allows you to define custom logic to be executed when a document is deleted.
75///
76/// Example:
77///
78/// ```rust
79/// #[on_delete_doc]
80/// async fn on_delete_doc(context: OnDeleteDocContext) -> Result<(), String> {
81/// // Your hook logic here
82/// }
83/// ```
84///
85/// When no attributes are provided, the hook is triggered for any document set within any collection.
86/// You can scope the events to a particular list of collections.
87///
88/// Example:
89/// ```rust
90/// #[on_delete_doc(collections = ["demo"])]
91/// async fn on_delete_doc(context: OnDeleteDocContext) -> Result<(), String> {
92/// // Your hook logic here
93/// }
94/// ```
95///
96/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
97///
98#[proc_macro_attribute]
99pub fn on_delete_doc(attr: TokenStream, item: TokenStream) -> TokenStream {
100 hook_macro(Hook::OnDeleteDoc, attr, item)
101}
102
103/// The `on_delete_many_docs` function is a procedural macro attribute for hooking into the `OnDeleteManyDocs` event.
104/// It allows you to define custom logic to be executed when multiple documents are deleted.
105///
106/// Example:
107///
108/// ```rust
109/// #[on_delete_many_docs]
110/// async fn on_delete_many_docs(context: OnDeleteManyDocsContext) -> Result<(), String> {
111/// // Your hook logic here
112/// }
113/// ```
114///
115/// When no attributes are provided, the hook is triggered for any document set within any collection.
116/// You can scope the events to a particular list of collections.
117///
118/// Example:
119/// ```rust
120/// #[on_delete_many_docs(collections = ["demo"])]
121/// async fn on_delete_many_docs(context: OnDeleteManyDocsContext) -> Result<(), String> {
122/// // Your hook logic here
123/// }
124/// ```
125///
126/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
127///
128#[proc_macro_attribute]
129pub fn on_delete_many_docs(attr: TokenStream, item: TokenStream) -> TokenStream {
130 hook_macro(Hook::OnDeleteManyDocs, attr, item)
131}
132
133/// The `on_delete_filtered_docs` function is a procedural macro attribute for hooking into the `OnDeleteFilteredDocs` event.
134/// It allows you to define custom logic to be executed when documents are deleted based on specific filter criteria.
135///
136/// Example:
137///
138/// ```rust
139/// #[on_delete_filtered_docs]
140/// async fn on_delete_filtered_docs(context: OnDeleteFilteredDocsContext) -> Result<(), String> {
141/// // Your hook logic here
142/// }
143/// ```
144///
145/// You can scope the events to a particular list of collections, making the hook more selective.
146///
147/// Example:
148/// ```rust
149/// #[on_delete_filtered_docs(collections = ["demo"])]
150/// async fn on_delete_filtered_docs(context: OnDeleteFilteredDocsContext) -> Result<(), String> {
151/// // Your hook logic here
152/// }
153/// ```
154///
155/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
156///
157/// # Parameters
158/// - `collections`: An optional list of collections to limit the scope of the hook.
159/// - `context`: An instance of `OnDeleteFilteredDocsContext` containing information about the deletion event.
160///
161/// # Returns
162/// - `Ok(())`: Indicates successful execution of the hook logic.
163/// - `Err(String)`: An error message if the hook logic encounters issues.
164///
165#[proc_macro_attribute]
166pub fn on_delete_filtered_docs(attr: TokenStream, item: TokenStream) -> TokenStream {
167 hook_macro(Hook::OnDeleteFilteredDocs, attr, item)
168}
169
170/// The `on_upload_asset` function is a procedural macro attribute for hooking into the `OnUploadAsset` event.
171/// It allows you to define custom logic to be executed when an asset is uploaded.
172///
173/// Example:
174///
175/// ```rust
176/// #[on_upload_asset]
177/// async fn on_upload_asset(context: OnUploadAssetContext) -> Result<(), String> {
178/// // Your hook logic here
179/// }
180/// ```
181///
182/// When no attributes are provided, the hook is triggered for any asset upload within any collection.
183/// You can scope the events to a particular list of collections.
184///
185/// Example:
186/// ```rust
187/// #[on_upload_asset(collections = ["demo"])]
188/// async fn on_upload_asset(context: OnUploadAssetContext) -> Result<(), String> {
189/// // Your hook logic here
190/// }
191/// ```
192///
193/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
194///
195#[proc_macro_attribute]
196pub fn on_upload_asset(attr: TokenStream, item: TokenStream) -> TokenStream {
197 hook_macro(Hook::OnUploadAsset, attr, item)
198}
199
200/// The `on_delete_asset` function is a procedural macro attribute for hooking into the `OnDeleteAsset` event.
201/// It allows you to define custom logic to be executed when an asset is deleted.
202///
203/// Example:
204///
205/// ```rust
206/// #[on_delete_asset]
207/// async fn on_delete_asset(context: OnDeleteAssetContext) -> Result<(), String> {
208/// // Your hook logic here
209/// }
210/// ```
211///
212/// When no attributes are provided, the hook is triggered for any asset deletion within any collection.
213/// You can scope the events to a particular list of collections.
214///
215/// Example:
216/// ```rust
217/// #[on_delete_asset(collections = ["demo"])]
218/// async fn on_delete_asset(context: OnDeleteAssetContext) -> Result<(), String> {
219/// // Your hook logic here
220/// }
221/// ```
222///
223/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
224///
225#[proc_macro_attribute]
226pub fn on_delete_asset(attr: TokenStream, item: TokenStream) -> TokenStream {
227 hook_macro(Hook::OnDeleteAsset, attr, item)
228}
229
230/// The `on_delete_many_assets` function is a procedural macro attribute for hooking into the `OnDeleteManyAssets` event.
231/// It allows you to define custom logic to be executed when multiple assets are deleted.
232///
233/// Example:
234///
235/// ```rust
236/// #[on_delete_many_assets]
237/// async fn on_delete_many_assets(context: OnDeleteManyAssetsContext) -> Result<(), String> {
238/// // Your hook logic here
239/// }
240/// ```
241///
242/// When no attributes are provided, the hook is triggered for any asset deletion within any collection.
243/// You can scope the events to a particular list of collections.
244///
245/// Example:
246/// ```rust
247/// #[on_delete_many_assets(collections = ["demo"])]
248/// async fn on_delete_many_assets(context: OnDeleteManyAssetsContext) -> Result<(), String> {
249/// // Your hook logic here
250/// }
251/// ```
252///
253/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
254///
255#[proc_macro_attribute]
256pub fn on_delete_many_assets(attr: TokenStream, item: TokenStream) -> TokenStream {
257 hook_macro(Hook::OnDeleteManyAssets, attr, item)
258}
259
260/// The `on_delete_filtered_assets` function is a procedural macro attribute for hooking into the `OnDeleteFilteredAssets` event.
261/// It allows you to define custom logic to be executed when assets are deleted based on specific filter criteria.
262///
263/// Example:
264///
265/// ```rust
266/// #[on_delete_filtered_assets]
267/// async fn on_delete_filtered_assets(context: OnDeleteFilteredAssetsContext) -> Result<(), String> {
268/// // Your hook logic here
269/// }
270/// ```
271///
272/// You can scope the events to a particular list of collections, making the hook more selective.
273///
274/// Example:
275/// ```rust
276/// #[on_delete_filtered_assets(collections = ["assets_collection"])]
277/// async fn on_delete_filtered_assets(context: OnDeleteFilteredAssetsContext) -> Result<(), String> {
278/// // Your hook logic here
279/// }
280/// ```
281///
282/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the hook will never be called.
283///
284/// # Parameters
285/// - `collections`: An optional list of collections to limit the scope of the hook.
286/// - `context`: An instance of `OnDeleteFilteredAssetsContext` containing information about the deletion event.
287///
288/// # Returns
289/// - `Ok(())`: Indicates successful execution of the hook logic.
290/// - `Err(String)`: An error message if the hook logic encounters issues.
291///
292#[proc_macro_attribute]
293pub fn on_delete_filtered_assets(attr: TokenStream, item: TokenStream) -> TokenStream {
294 hook_macro(Hook::OnDeleteFilteredAssets, attr, item)
295}
296
297/// The `assert_set_doc` function is a procedural macro attribute for asserting conditions before setting a document.
298/// It enables you to define custom validation logic to be executed prior to a document creation or update.
299///
300/// Example:
301///
302/// ```rust
303/// #[assert_set_doc]
304/// fn assert_set_doc(context: AssertSetDocContext) -> Result<(), String> {
305/// // Your assertion logic here
306/// }
307/// ```
308///
309/// When no attributes are provided, the assertion logic is applied to any document set within any collection.
310/// You can scope the assertion to a particular list of collections.
311///
312/// Example:
313/// ```rust
314/// #[assert_set_doc(collections = ["demo"])]
315/// fn assert_set_doc(context: AssertSetDocContext) -> Result<(), String> {
316/// // Your assertion logic here
317/// }
318/// ```
319///
320/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the assertion will always be evaluated.
321///
322#[proc_macro_attribute]
323pub fn assert_set_doc(attr: TokenStream, item: TokenStream) -> TokenStream {
324 hook_macro(Hook::AssertSetDoc, attr, item)
325}
326
327/// The `assert_delete_doc` function is a procedural macro attribute for asserting conditions before deleting a document.
328/// It enables you to define custom validation logic to be executed prior to a document deletion.
329///
330/// Example:
331///
332/// ```rust
333/// #[assert_delete_doc]
334/// fn assert_delete_doc(context: AssertDeleteDocContext) -> Result<(), String> {
335/// // Your assertion logic here
336/// }
337/// ```
338///
339/// When no attributes are provided, the assertion logic is applied to any document delete within any collection.
340/// You can scope the assertion to a particular list of collections.
341///
342/// Example:
343/// ```rust
344/// #[assert_delete_doc(collections = ["demo"])]
345/// fn assert_delete_doc(context: AssertDeleteDocContext) -> Result<(), String> {
346/// // Your assertion logic here, specific to the "demo" collection
347/// }
348/// ```
349///
350/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the assertion will always be evaluated.
351///
352#[proc_macro_attribute]
353pub fn assert_delete_doc(attr: TokenStream, item: TokenStream) -> TokenStream {
354 hook_macro(Hook::AssertDeleteDoc, attr, item)
355}
356
357/// The `assert_upload_asset` function is a procedural macro attribute for asserting conditions before committing the upload of an asset.
358/// It enables you to define custom validation logic to be executed prior to an asset being committed.
359///
360/// Example:
361///
362/// ```rust
363/// #[assert_upload_asset]
364/// fn assert_upload_asset(context: AssertUploadAssetContext) -> Result<(), String> {
365/// // Your assertion logic here
366/// }
367/// ```
368///
369/// When no attributes are provided, the assertion logic is applied to any asset upload within any collection.
370/// You can scope the assertion to a particular list of collections.
371///
372/// Example:
373/// ```rust
374/// #[assert_upload_asset(collections = ["assets"])]
375/// fn juno_assert_upload_asset(context: AssertUploadAssetContext) -> Result<(), String> {
376/// // Your assertion logic here, specific to the "assets" collection
377/// }
378/// ```
379///
380/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the assertion will always be evaluated.
381///
382#[proc_macro_attribute]
383pub fn assert_upload_asset(attr: TokenStream, item: TokenStream) -> TokenStream {
384 hook_macro(Hook::AssertUploadAsset, attr, item)
385}
386
387/// The `assert_delete_asset` function is a procedural macro attribute for asserting conditions before deleting an asset.
388/// It enables you to define custom validation logic to be executed prior to an asset being deleted.
389///
390/// Example:
391///
392/// ```rust
393/// #[assert_delete_asset]
394/// fn assert_delete_asset(context: AssertDeleteAssetContext) -> Result<(), String> {
395/// // Your assertion logic here
396/// }
397/// ```
398///
399/// When no attributes are provided, the assertion logic is applied to any asset deletion within any collection.
400/// You can scope the assertion to a particular list of collections.
401///
402/// Example:
403/// ```rust
404/// #[assert_delete_asset(collections = ["assets"])]
405/// fn juno_assert_delete_asset(context: AssertDeleteAssetContext) -> Result<(), String> {
406/// // Your assertion logic here, specific to the "assets" collection
407/// }
408/// ```
409///
410/// The attributes accept a list of comma-separated collections. If the attribute array is left empty, the assertion will always be evaluated.
411///
412#[proc_macro_attribute]
413pub fn assert_delete_asset(attr: TokenStream, item: TokenStream) -> TokenStream {
414 hook_macro(Hook::AssertDeleteAsset, attr, item)
415}
416
417/// The `on_post_upgrade` function is a procedural macro attribute for hooking into the `OnPostUpgrade` event.
418/// It allows you to define custom logic to be executed after a satellite upgrade.
419///
420/// Example:
421///
422/// ```rust
423/// #[on_post_upgrade]
424/// fn on_post_upgrade() {
425/// // Your post-upgrade logic here
426/// }
427/// ```
428///
429#[proc_macro_attribute]
430pub fn on_post_upgrade(attr: TokenStream, item: TokenStream) -> TokenStream {
431 hook_macro(Hook::OnPostUpgrade, attr, item)
432}
433
434/// The `on_init` function is a procedural macro attribute for hooking into the `OnInit` event.
435/// It allows you to define custom logic to be executed after a satellite is initialized.
436///
437/// Example:
438///
439/// ```rust
440/// #[on_init]
441/// fn on_init() {
442/// // Your post-init logic here
443/// }
444/// ```
445///
446#[proc_macro_attribute]
447pub fn on_init(attr: TokenStream, item: TokenStream) -> TokenStream {
448 hook_macro(Hook::OnInit, attr, item)
449}
450
451/// The `on_post_upgrade_sync` function is a procedural macro attribute for hooking into the `OnPostUpgrade` event.
452/// Unlike `on_post_upgrade`, this variant **executes synchronously** and is intended for advanced use cases
453/// where immediate execution is required.
454///
455/// This macro should only be used in scenarios where deferred execution (via async hooks) is **not an option**.
456/// Regular users should use `#[on_post_upgrade]` instead.
457///
458/// # Warning ⚠️
459/// - This function **executes immediately** during a satellite upgrade.
460/// - It **bypasses** the usual async execution model, which may lead to **unexpected behavior** if used improperly.
461/// - Any error in this function will cause the upgrade to fail!!!
462/// - If the upgrade fails, the satellite might become unresponsive and lose its data.
463/// - **Developers should prefer `on_post_upgrade` unless they explicitly need synchronous behavior.**
464///
465/// Furthermore, note that the random number generator or other capabilities might not have been initialized at this point.
466///
467/// # Example (Restricted Usage)
468///
469/// ```rust
470/// #[on_post_upgrade_sync]
471/// fn on_post_upgrade_sync() {
472/// // Perform necessary actions immediately on upgrade
473/// }
474/// ```
475///
476/// **Note:** This function is hidden from public documentation to discourage general usage.
477#[doc(hidden)]
478#[proc_macro_attribute]
479pub fn on_post_upgrade_sync(attr: TokenStream, item: TokenStream) -> TokenStream {
480 hook_macro(Hook::OnPostUpgradeSync, attr, item)
481}
482
483/// The `on_init_sync` function is a procedural macro attribute for hooking into the `OnInit` event.
484/// It serves the same purpose as `on_init`, but **executes synchronously**, ensuring that initialization
485/// logic is run **immediately** instead of being deferred.
486///
487/// This function is intended for **special cases only** where an immediate initialization
488/// step is necessary before any asynchronous tasks are triggered.
489///
490/// # Warning ⚠️
491/// - This function **runs immediately** at initialization time.
492/// - It **bypasses deferred execution**, meaning long-running operations **may slow down startup**.
493/// - Regular users **should use `on_init` instead**, unless there's a strict requirement for synchronous behavior.
494///
495/// Furthermore, note that the random number generator or other capabilities might not have been initialized at this point.
496///
497/// # Example (Restricted Usage)
498///
499/// ```rust
500/// #[on_init_sync]
501/// fn on_init_sync() {
502/// // This runs synchronously before any async operations are triggered
503/// }
504/// ```
505///
506/// **Note:** This function is hidden from public documentation to prevent unintended usage.
507#[doc(hidden)]
508#[proc_macro_attribute]
509pub fn on_init_sync(attr: TokenStream, item: TokenStream) -> TokenStream {
510 hook_macro(Hook::OnInitSync, attr, item)
511}
512
513/// The `on_init_random_seed` function is a procedural macro attribute for hooking into the
514/// `OnInitRandomSeed` event. It allows you to define custom logic to be executed after
515/// the satellite has initialized the random seed.
516///
517/// Example:
518///
519/// ```rust
520/// #[on_init_random_seed]
521/// fn on_init_random_seed() {
522/// // Your post-initialization logic after random seed setup
523/// }
524/// ```
525///
526#[proc_macro_attribute]
527pub fn on_init_random_seed(attr: TokenStream, item: TokenStream) -> TokenStream {
528 hook_macro(Hook::OnInitRandomSeed, attr, item)
529}