Skip to main content

mkutils_macros/
lib.rs

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