iterum_macros/lib.rs
1use proc_macro::TokenStream;
2use syn::parse_macro_input;
3
4mod internals;
5
6/// A macro to generate versioned structs.
7///
8/// ## Examples
9///
10/// The following code will result in three versions of the `User` struct:
11///
12/// - `UserV0_0_0`
13/// - `UserV1_0_0`
14/// - `UserV2_0_0`
15///
16/// ```rust
17/// use iterum_macros::versioned;
18/// use serde::{Deserialize, Serialize};
19///
20/// #[derive(Deserialize, Serialize)]
21/// struct Email(String);
22///
23/// #[versioned(
24/// // use semantic versioning
25/// semver,
26/// // add #[serde(rename = "...")] on the enum variants
27/// // and #[serde(borrow)] where appropriate
28/// serde,
29/// // add attributes to the generated enum only
30/// attrs(serde(tag = "version"))
31/// )]
32/// // add attributes to all generated structs
33/// #[derive(Deserialize, Serialize)]
34/// struct User<'a, A> {
35/// // present on all versions
36/// username: String,
37/// // present on all versions until 1.0.0 (exclusive)
38/// #[versioned(until = "1.0.0")]
39/// email: A,
40/// // present on all versions since 1.0.0 (inclusive)
41/// #[versioned(since = "1.0.0")]
42/// email: Email,
43/// created_at: String,
44/// // present on all versions since 2.0.0 (inclusive)
45/// #[versioned(since = "2.0.0")]
46/// a: &'a str,
47/// }
48/// ```
49#[proc_macro_attribute]
50pub fn versioned(attr: TokenStream, input: TokenStream) -> TokenStream {
51 let input = parse_macro_input!(input as syn::DeriveInput);
52 internals::versioned::expand(attr.into(), input)
53 .unwrap_or_else(syn::Error::into_compile_error)
54 .into()
55}