Skip to content

Commit 89e05b9

Browse files
committed
Remove unused macro invocation and add docs for the only remaining macro
1 parent b72bb86 commit 89e05b9

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

src/lib.rs

+42-20
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,48 @@ impl Parse for EnumConversionsArgs {
160160
}
161161
}
162162

163+
/// Derive macro that generates conversions between an enum and its variants and other types.
164+
///
165+
/// The macro can be used as follows:
166+
///
167+
/// ```rust
168+
/// use nested_enum_utils::enum_conversions;
169+
///
170+
/// #[enum_conversions()]
171+
/// enum MyEnum {
172+
/// Variant1(u32),
173+
/// Variant2(String),
174+
/// }
175+
/// ```
176+
///
177+
/// This will create From instances from each variant type to the enum and TryFrom instances from the enum to each variant type.
178+
///
179+
/// The macro also accepts a list of target types to generate conversions to:
180+
///
181+
/// ```rust
182+
/// use nested_enum_utils::enum_conversions;
183+
///
184+
/// #[enum_conversions(Outer)]
185+
/// enum Inner {
186+
/// Variant1(u32),
187+
/// Variant2(String),
188+
/// }
189+
///
190+
/// #[enum_conversions()]
191+
/// enum Outer {
192+
/// Inner1(Inner),
193+
/// // other variants
194+
/// }
195+
/// ```
196+
///
197+
/// This will, in addition, generate From instances from each variant type to the outer enum and TryFrom instances from the outer enum to each variant type.
198+
/// The conversion to the outer enum relies on conversions between the inner enum and the outer enum, which is provided by the
199+
/// enum_conversions attribute on the Outer enum.
200+
///
201+
/// Limitations:
202+
///
203+
/// - enums must have unnamed single fields
204+
/// - field types must be distinct
163205
#[proc_macro_attribute]
164206
pub fn enum_conversions(attr: TokenStream, item: TokenStream) -> TokenStream {
165207
let args = parse_macro_input!(attr as EnumConversionsArgs);
@@ -192,23 +234,3 @@ pub fn enum_conversions(attr: TokenStream, item: TokenStream) -> TokenStream {
192234
};
193235
TokenStream::from(expanded)
194236
}
195-
196-
#[proc_macro_derive(EnumConversions)]
197-
pub fn derive_enum_conversions(input: TokenStream) -> TokenStream {
198-
let input = parse_macro_input!(input as DeriveInput);
199-
let enum_name = &input.ident;
200-
201-
let variants = match extract_enum_variants(&input) {
202-
Ok(v) => v,
203-
Err(e) => return e.to_compile_error().into(),
204-
};
205-
206-
let self_conversions = generate_enum_self_conversions(enum_name, &variants);
207-
208-
let expanded = quote! {
209-
#input
210-
#self_conversions
211-
};
212-
213-
TokenStream::from(expanded)
214-
}

0 commit comments

Comments
 (0)