Skip to content

Commit dd62619

Browse files
committed
Document the versioned macro
Signed-off-by: Christian Poveda <[email protected]>
1 parent 72039f3 commit dd62619

File tree

1 file changed

+54
-0
lines changed
  • cyclonedx-bom-macros/src

1 file changed

+54
-0
lines changed

cyclonedx-bom-macros/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,60 @@ fn helper(
301301
Ok(tokens)
302302
}
303303

304+
/// A `cfg`-like attribute macro to generate versioned modules.
305+
///
306+
/// This macro allows to duplicate a module by providing version numbers to the
307+
/// macro itself. For example:
308+
/// ```rust
309+
/// use cyclonedx_bom_macros::versioned;
310+
///
311+
/// #[versioned("1.0", "2.0")]
312+
/// mod base {
313+
/// pub(super) struct Foo;
314+
/// }
315+
/// ```
316+
/// Will generate two modules: `v1_0` and `v2_0`, where each one of them
317+
/// contains the definition of `Foo`:
318+
/// ```rust
319+
/// mod v1_0 {
320+
/// pub(super) struct Foo;
321+
/// }
322+
///
323+
/// mod v2_0 {
324+
/// pub(super) struct Foo;
325+
/// }
326+
/// ```
327+
/// Additionally the macro can be used to gate definitions and expressions
328+
/// behind a specific version, very much like the `cfg` attribute. Based on the
329+
/// previous example:
330+
/// ```rust
331+
/// use cyclonedx_bom_macros::versioned;
332+
///
333+
/// #[versioned("1.0", "2.0")]
334+
/// mod base {
335+
/// pub(super) struct Foo;
336+
///
337+
/// #[versioned("2.0")]
338+
/// pub(super) struct Bar;
339+
/// }
340+
/// ```
341+
/// The following code will be generated:
342+
/// ```rust
343+
/// mod v1_0 {
344+
/// pub(super) struct Foo;
345+
/// }
346+
///
347+
/// mod v2_0 {
348+
/// pub(super) struct Foo;
349+
/// pub(super) struct Bar;
350+
/// }
351+
/// ```
352+
/// Note that `Bar` only exists inside the `v2_0` module. Note that the
353+
/// `versioned` attribute annotating the module defines the versions that will be
354+
/// used to generate the modules and the attribute annotating the `Bar` definition
355+
/// states that this definition will only appear on the `2.0` module.
356+
///
357+
/// Check the test folder for more usage examples.
304358
#[proc_macro_attribute]
305359
pub fn versioned(input: TokenStream, annotated_item: TokenStream) -> TokenStream {
306360
match helper(input, annotated_item) {

0 commit comments

Comments
 (0)