Skip to main content

mkutils_macros/
lib.rs

1mod context;
2mod default;
3mod from_chain;
4mod set_variant;
5mod toggle;
6mod type_assoc;
7mod utils;
8
9use crate::{default::Default, from_chain::FromChain, set_variant::SetVariant, toggle::Toggle, type_assoc::TypeAssoc};
10use proc_macro::TokenStream;
11
12// TODO: add documentation
13#[proc_macro_attribute]
14pub fn context(args_token_stream: TokenStream, input_token_stream: TokenStream) -> TokenStream {
15    crate::context::context(args_token_stream, input_token_stream)
16}
17
18/// Implement `::std::convert::From` through a chain of intermediate types.
19///
20///
21/// # Example
22///
23/// ```rust
24/// #[derive(FromChain)]
25/// #[from(Foo, Bar, Baz)]
26/// struct MyStruct;
27/// ```
28///
29/// adds
30///
31/// ```rust
32/// impl From<Foo> for MyStruct {
33///     fn from(foo: Foo) -> Self {
34///         Self::from(Baz::from(Bar::from(foo)))
35///     }
36/// }
37/// ```
38#[proc_macro_derive(FromChain, attributes(from))]
39pub fn from_chain(input_token_stream: TokenStream) -> TokenStream {
40    FromChain::derive(input_token_stream)
41}
42
43/// Implements traits that only have associated types.
44///
45///
46/// # Example
47///
48/// ```rust
49/// #[derive(TypeAssoc)]
50/// #[type_assoc(trait = Foo, Item = Vec<u8>)]
51/// struct MyStruct;
52/// ```
53///
54/// adds
55///
56/// ```rust
57/// impl Foo for MyStruct {
58///     type Item = Vec<u8>;
59/// }
60/// ```
61#[proc_macro_derive(TypeAssoc, attributes(type_assoc))]
62pub fn type_assoc(input_token_stream: TokenStream) -> TokenStream {
63    TypeAssoc::derive(input_token_stream)
64}
65
66/// Implements `Default` for a struct, using `Default::default()` for each field
67/// unless a `#[default(...)]` attribute provides a custom expression.
68///
69///
70/// # Example
71///
72/// ```rust
73/// #[derive(Default)]
74/// struct MyStruct {
75///     name: String,
76///     #[default(42)]
77///     count: i32,
78///     #[default(vec![1, 2, 3])]
79///     items: Vec<i32>,
80/// }
81/// ```
82///
83/// adds
84///
85/// ```rust
86/// impl Default for MyStruct {
87///     fn default() -> Self {
88///         Self {
89///             name: ::core::default::Default::default(),
90///             count: 42,
91///             items: vec![1, 2, 3],
92///         }
93///     }
94/// }
95/// ```
96#[proc_macro_derive(Default, attributes(default))]
97pub fn default(input_token_stream: TokenStream) -> TokenStream {
98    Default::derive(input_token_stream)
99}
100
101/// Adds `set_*()` methods for each unit variant on the given enum.
102///
103/// # Example
104///
105/// ```rust
106/// #[derive(SetVariant)]
107/// enum MyEnum {
108///   Foo,
109///   Bar,
110///   Baz(String),
111/// }
112/// ```
113///
114/// adds
115///
116/// ```rust
117/// impl MyEnum {
118///   pub fn set_foo(&mut self) -> &mut Self {
119///     *self = Self::Foo;
120///
121///     self
122///   }
123///
124///   pub fn set_bar(&mut self) -> &mut Self {
125///     *self = Self::Bar;
126///
127///     self
128///   }
129/// }
130/// ```
131#[proc_macro_derive(SetVariant)]
132pub fn set_variant(input_token_stream: TokenStream) -> TokenStream {
133    SetVariant::derive(input_token_stream)
134}
135
136/// Adds a `toggled()` method that maps each enum variant to the next unit variant.
137///
138/// # Example
139///
140/// ```rust
141/// #[derive(Toggle)]
142/// enum MyEnum {
143///   Foo,
144///   Bar,
145///   Baz(String),
146/// }
147/// ```
148///
149/// adds
150///
151/// ```rust
152/// impl MyEnum {
153///   pub fn toggle(&self) -> Self {
154///     match self {
155///         Self::Foo => Self::Bar,
156///         Self::Bar => Self::Foo,
157///         Self::Baz(_string) => Self::Foo,
158///     }
159///   }
160///
161///   pub fn toggle(&mut self) -> &mut Self {
162///     *self = self.toggled();
163///
164///     self
165///   }
166/// }
167/// ```
168#[proc_macro_derive(Toggle)]
169pub fn toggle(input_token_stream: TokenStream) -> TokenStream {
170    Toggle::derive(input_token_stream)
171}