variantly/
lib.rs

1//!
2//! Debug helper methods for enum variants that are familiar from [`Option`] & [`Result`] such as [`Option::unwrap_or`] or [`Result::and_then`].
3//! # Example
4//! ```
5//! #[derive(variantly::Variantly)]
6//! enum Color {
7//!     RGB(u8, u8, u8),
8//!     HSV(u8, u8, u8),
9//!     Grey(u8),
10//!     FromOutOfSpace,
11//!     #[variantly(rename = "darkness")]
12//!     Black,
13//! }
14//!
15//! fn example() {
16//!     let color = Color::HSV(123, 45, 67);
17//!
18//!     // boolean helper method for determining variant:
19//!     assert!(color.is_hsv());
20//!     assert!(!color.is_rgb());
21//!
22//!     // Get inner values:
23//!     let (h, s, v) = color.unwrap_hsv();
24//!     assert_eq!((h, s, v), (123, 45, 67));
25//!
26//!     // Single values don't require tuple destructuring:
27//!     let color = Color::Grey(128);
28//!     let value = color.unwrap_grey();
29//!     assert_eq!(value, 128);
30//!
31//!     // Alter inner value, only if hsv:
32//!     let color = Color::HSV(111, 22, 33);
33//!     let color = color.and_then_hsv(|(h, s, _)| (h, s, 100));
34//!     assert_eq!(color.unwrap_hsv(), (111, 22, 100));
35//!
36//!     // Safely unwrap with a fallback:
37//!     let color = Color::RGB(255, 255, 0);
38//!     let (r, g, b) = color.unwrap_or_rgb((0, 0, 0));
39//!     assert_eq!((r, g, b), (255, 255, 0));
40//!     // Since color is of the HSV variant, the default is not used.
41//!
42//!     // Safely unwrap using the fallback
43//!     let color = Color::FromOutOfSpace;
44//!     let (r, g, b) = color.unwrap_or_rgb((0, 0, 0));
45//!     assert_eq!((r, g, b), (0, 0, 0));
46//!
47//!     // Convert into an Option
48//!     let color = Color::RGB(0, 255, 255);
49//!     let optional_rgb = color.rgb();
50//!     assert_eq!(Some((0, 255, 255)), optional_rgb);
51//!
52//!     // Convert into a Result
53//!     let color = Color::RGB(255, 0, 255);
54//!     let result_rgb = color.rgb_or("Error: This is not an RGB variant!");
55//!     assert_eq!(Ok((255, 0, 255)), result_rgb);
56//!
57//!     // Operations like this can also use their familiar `_else` versions:
58//!     let color = Color::FromOutOfSpace;
59//!     let result_rgb = color.rgb_or_else(|| Some("This is a computationally expensive error!"));
60//!     assert!(result_rgb.is_err());
61//!
62//!     // The `#[variantly(rename = "darkness")]` attribute renames derived methods:
63//!     let color = Color::Black;
64//!     assert!(color.is_darkness())
65//! }
66//! ```
67//! # Derived Methods
68//! In the naming of all methods described here, replace the `{variant_name}` with the snake_case formatted name of the given variant.
69//!
70//! ## Option & Result Conversion
71//! Use the below methods to convert the enum into either an option or result:
72//!
73//! ### `pub fn {variant_name}(self) -> Option(...)`
74//! If the enum is of the given variant, returns a [`Some`] containing the inner variant value. Otherwise, return [`None`].
75//!
76//! #### Example
77//! ```
78//! # #[derive(variantly::Variantly, Debug, PartialEq)]
79//! # enum Color {
80//! #     RGB(u8, u8, u8),
81//! #     HSV(u8, u8, u8),
82//! #     Grey(u8),
83//! #     FromOutOfSpace,
84//! #     #[variantly(rename = "darkness")]
85//! #     Black,
86//! # }
87//! let color = Color::HSV(1,2,3);
88//!
89//! let option = color.hsv();
90//! assert_eq!(Some((1, 2, 3)), option);
91//!
92//! let color = Color::FromOutOfSpace;
93//! assert_eq!(None, color.rgb());
94//! ```
95//!
96//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
97//!
98//! ### `pub fn {variant_name}_ref(&self) -> Option(&...)`
99//! If the enum is of the given variant, returns a `Some` containing a ref to the inner variant value. Otherwise, return None.
100//!
101//! #### Example
102//! ```
103//! # #[derive(variantly::Variantly, Debug, PartialEq)]
104//! # enum Color {
105//! #     RGB(u8, u8, u8),
106//! #     HSV(u8, u8, u8),
107//! #     Grey(u8),
108//! #     FromOutOfSpace,
109//! #     #[variantly(rename = "darkness")]
110//! #     Black,
111//! # }
112//!
113//! let color = Color::HSV(1,2,3);
114//!
115//! let option = color.hsv_ref();
116//! assert_eq!(Some((&1, &2, &3)), option);
117//!
118//! let color = Color::FromOutOfSpace;
119//! assert_eq!(None, color.rgb_ref());
120//! ```
121//!
122//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
123//!
124//! ### `pub fn {variant_name}_mut(&mut self) -> Option(&mut...)`
125//! If the enum is of the given variant, returns a `Some` containing a mutable ref to the inner variant value. Otherwise, return None.
126//!
127//! #### Example
128//! ```
129//! # #[derive(variantly::Variantly, Debug, PartialEq)]
130//! # enum Color {
131//! #     RGB(u8, u8, u8),
132//! #     HSV(u8, u8, u8),
133//! #     Grey(u8),
134//! #     FromOutOfSpace,
135//! #     #[variantly(rename = "darkness")]
136//! #     Black,
137//! # }
138//! let mut color = Color::HSV(1,2,3);
139//!
140//! let option = color.hsv_mut();
141//! assert_eq!(Some((&mut 1, &mut 2, &mut 3)), option);
142//!
143//! let mut color = Color::FromOutOfSpace;
144//! assert_eq!(None, color.rgb_mut());
145//! ```
146//!
147//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
148//!
149//! ### `pub fn {variant_name}_or<E>(self, err: E) -> Result<(...), E>`
150//! If the enum is of the given variant, returns a [`Result::Ok`] containing the inner value. Otherwise, return [`Result::Err`] containing `err`.
151//!
152//! #### Example
153//! ```
154//! # #[derive(variantly::Variantly, Debug, PartialEq)]
155//! # enum Color {
156//! #     RGB(u8, u8, u8),
157//! #     HSV(u8, u8, u8),
158//! #     Grey(u8),
159//! #     FromOutOfSpace,
160//! #     #[variantly(rename = "darkness")]
161//! #     Black,
162//! # }
163//! let color = Color::HSV(1,2,3);
164//!
165//! let result = color.hsv_or("Error: Not an HSV!");
166//! assert_eq!(Ok((1, 2, 3)), result);
167//!
168//! let color = Color::FromOutOfSpace;
169//! let result = color.hsv_or("Error: Not an HSV!");
170//! assert_eq!(Err("Error: Not an HSV!"), result);
171//! ```
172//!
173//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
174//!
175//! ### `pub fn {variant_name}_ref_or<E>(&self, err: E) -> Result<(&...), E>`
176//! If the enum is of the given variant, returns a `Result::Ok` containing a ref to the inner value. Otherwise, return `Result::Err` containing `err`.
177//!
178//! #### Example
179//! ```
180//! # #[derive(variantly::Variantly, Debug, PartialEq)]
181//! # enum Color {
182//! #     RGB(u8, u8, u8),
183//! #     HSV(u8, u8, u8),
184//! #     Grey(u8),
185//! #     FromOutOfSpace,
186//! #     #[variantly(rename = "darkness")]
187//! #     Black,
188//! # }
189//! let color = Color::HSV(1,2,3);
190//!
191//! let result = color.hsv_ref_or("Error: Not an HSV!");
192//! assert_eq!(Ok((&1, &2, &3)), result);
193//!
194//! let color = Color::FromOutOfSpace;
195//! let result = color.hsv_ref_or("Error: Not an HSV!");
196//! assert_eq!(Err("Error: Not an HSV!"), result);
197//! ```
198//!
199//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
200//!
201//! ### `pub fn {variant_name}_mut_or<E>(&mut self, err: E) -> Result<(&mut...), E>`
202//! If the enum is of the given variant, returns a `Result::Ok` containing a mutable ref to the inner value. Otherwise, return `Result::Err` containing `err`.
203//!
204//! #### Example
205//! ```
206//! # #[derive(variantly::Variantly, Debug, PartialEq)]
207//! # enum Color {
208//! #     RGB(u8, u8, u8),
209//! #     HSV(u8, u8, u8),
210//! #     Grey(u8),
211//! #     FromOutOfSpace,
212//! #     #[variantly(rename = "darkness")]
213//! #     Black,
214//! # }
215//! let mut color = Color::HSV(1,2,3);
216//!
217//! let result = color.hsv_mut_or("Error: Not an HSV!");
218//! assert_eq!(Ok((&mut 1, &mut 2, &mut 3)), result);
219//!
220//! let mut color = Color::FromOutOfSpace;
221//! let result = color.hsv_mut_or("Error: Not an HSV!");
222//! assert_eq!(Err("Error: Not an HSV!"), result);
223//! ```
224//!
225//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
226//!
227//! ### `pub fn {variant_name}_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(...), E>`
228//! If the enum is of the given variant, returns a [`Result::Ok`] containing the inner variant value. Otherwise, calls `f` to calculate a [`Result::Err`].
229//!
230//! #### Example
231//! ```
232//! # #[derive(variantly::Variantly, Debug, PartialEq)]
233//! # enum Color {
234//! #     RGB(u8, u8, u8),
235//! #     HSV(u8, u8, u8),
236//! #     Grey(u8),
237//! #     FromOutOfSpace,
238//! #     #[variantly(rename = "darkness")]
239//! #     Black,
240//! # }
241//! let color = Color::HSV(1,2,3);
242//!
243//! let result = color.hsv_or_else(|| "This is an expensive error to create.");
244//! assert_eq!(Ok((1, 2, 3)), result);
245//!
246//! let color = Color::FromOutOfSpace;
247//! let result = color.hsv_or_else(|| "This is an expensive error to create.");
248//! assert_eq!(Err("This is an expensive error to create."), result);
249//! ```
250//!
251//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
252//!
253//! ### `pub fn {variant_name}_ref_or_else<E, F: FnOnce() -> E>(&self, f: F) -> Result<(&...), E>`
254//! If the enum is of the given variant, returns a `Result::Ok` containing a ref to the inner variant value. Otherwise, calls `f` to calculate a `Result::Err`.
255//!
256//! #### Example
257//! ```
258//! # #[derive(variantly::Variantly, Debug, PartialEq)]
259//! # enum Color {
260//! #     RGB(u8, u8, u8),
261//! #     HSV(u8, u8, u8),
262//! #     Grey(u8),
263//! #     FromOutOfSpace,
264//! #     #[variantly(rename = "darkness")]
265//! #     Black,
266//! # }
267//! let color = Color::HSV(1,2,3);
268//!
269//! let result = color.hsv_ref_or_else(|| "This is an expensive error to create.");
270//! assert_eq!(Ok((&1, &2, &3)), result);
271//!
272//! let color = Color::FromOutOfSpace;
273//! let result = color.hsv_ref_or_else(|| "This is an expensive error to create.");
274//! assert_eq!(Err("This is an expensive error to create."), result);
275//! ```
276//!
277//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
278//!
279//! ### `pub fn {variant_name}_mut_or_else<E, F: FnOnce() -> E>(&mut self, f: F) -> Result<(&mut...), E>`
280//! If the enum is of the given variant, returns a `Result::Ok` containing a mut ref to the inner variant value. Otherwise, calls `f` to calculate a `Result::Err`.
281//!
282//! #### Example
283//! ```
284//! # #[derive(variantly::Variantly, Debug, PartialEq)]
285//! # enum Color {
286//! #     RGB(u8, u8, u8),
287//! #     HSV(u8, u8, u8),
288//! #     Grey(u8),
289//! #     FromOutOfSpace,
290//! #     #[variantly(rename = "darkness")]
291//! #     Black,
292//! # }
293//! let mut color = Color::HSV(1,2,3);
294//!
295//! let result = color.hsv_mut_or_else(|| "This is an expensive error to create.");
296//! assert_eq!(Ok((&mut 1, &mut 2, &mut 3)), result);
297//!
298//! let mut color = Color::FromOutOfSpace;
299//! let result = color.hsv_mut_or_else(|| "This is an expensive error to create.");
300//! assert_eq!(Err("This is an expensive error to create."), result);
301//! ```
302//!
303//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
304//!
305//! ## Accessing Inner Values
306//! Use the below methods to easily access the inner value of a given variant.
307//!
308//! ### `pub fn expect_{variant_name}(self, msg: &str) -> (...)`
309//! Returns the contained value.
310//!
311//! #### Panics
312//! Panics if the enum is not of the given variant with the custom message `msg`.
313//!
314//! #### Example
315//! ```
316//! # #[derive(variantly::Variantly, Debug, PartialEq)]
317//! # enum Color {
318//! #     RGB(u8, u8, u8),
319//! #     HSV(u8, u8, u8),
320//! #     Grey(u8),
321//! #     FromOutOfSpace,
322//! #     #[variantly(rename = "darkness")]
323//! #     Black,
324//! # }
325//! let color_a = Color::HSV(1,2,3);
326//! let color_b = Color::Grey(10);
327//!
328//! let (h, s, v) = color_a.expect_hsv("This should be an hsv");
329//! assert_eq!((h, s, v), (1, 2, 3));
330//!
331//! let grey = color_b.expect_grey("This should be grey");
332//! assert_eq!(grey, 10);
333//! ```
334//!
335//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
336//!
337//! ### `pub fn unwrap_{variant_name}(self) -> (...)`
338//! Returns the contained value.
339//!
340//! #### Panics
341//! Panics if the enum is not of the given variant.
342//!
343//! #### Example
344//! ```
345//! # #[derive(variantly::Variantly, Debug, PartialEq)]
346//! # enum Color {
347//! #     RGB(u8, u8, u8),
348//! #     HSV(u8, u8, u8),
349//! #     Grey(u8),
350//! #     FromOutOfSpace,
351//! #     #[variantly(rename = "darkness")]
352//! #     Black,
353//! # }
354//! let color_a = Color::HSV(1,2,3);
355//! let color_b = Color::Grey(10);
356//!
357//! let (h, s, v) = color_a.unwrap_hsv();
358//! assert_eq!((h, s, v), (1, 2, 3));
359//!
360//! let grey = color_b.unwrap_grey();
361//! assert_eq!(grey, 10);
362//! ```
363//!
364//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
365//!
366//! ### `pub fn unwrap_or_{variant_name}(self, fallback: (...)) -> (...)`
367//! Returns the contained value if the enum is of the given variant, otherwise returns the provided `fallback`.
368//!
369//! #### Example
370//! ```
371//! # #[derive(variantly::Variantly, Debug, PartialEq)]
372//! # enum Color {
373//! #     RGB(u8, u8, u8),
374//! #     HSV(u8, u8, u8),
375//! #     Grey(u8),
376//! #     FromOutOfSpace,
377//! #     #[variantly(rename = "darkness")]
378//! #     Black,
379//! # }
380//! let color_a = Color::HSV(1,2,3);
381//! let color_b = Color::Grey(10);
382//!
383//! let (h, s, v) = color_a.unwrap_or_hsv((4, 5, 6));
384//! assert_eq!((h, s, v), (1, 2, 3));
385//!
386//! let color = color_b.unwrap_or_rgb((4, 5, 6));
387//! assert_eq!(color, (4, 5, 6));
388//! ```
389//!
390//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
391//!
392//! ### `pub fn unwrap_or_else_{variant_name}<F: FnOnce() -> (...)>(self, f: F) -> (...)`
393//! Returns the contained value if the enum is of the given variant, otherwise computes a fallback from `f`.
394//!
395//! #### Example
396//! ```
397//! # #[derive(variantly::Variantly, Debug, PartialEq)]
398//! # enum Color {
399//! #     RGB(u8, u8, u8),
400//! #     HSV(u8, u8, u8),
401//! #     Grey(u8),
402//! #     FromOutOfSpace,
403//! #     #[variantly(rename = "darkness")]
404//! #     Black,
405//! # }
406//! let color_a = Color::HSV(1,2,3);
407//! let color_b = Color::Grey(10);
408//!
409//! let (h, s, v) = color_a.unwrap_or_else_hsv(|| (4,5,6));
410//! assert_eq!((h, s, v), (1, 2, 3));
411//!
412//! let (h, s, v) = color_b.unwrap_or_else_hsv(|| (4,5,6));
413//! assert_eq!((h, s, v), (4, 5, 6));
414//! ```
415//!
416//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
417//!
418//! ## Testing Variant Type
419//! Use the below methods to test whether a variant is of the given type.
420//!
421//! ### `pub fn is_{variant_name}(self) -> bool`
422//! Returns `true` if the enum is of the given variant.
423//!
424//! #### Example
425//! ```
426//! # #[derive(variantly::Variantly, Debug, PartialEq)]
427//! # enum Color {
428//! #     RGB(u8, u8, u8),
429//! #     HSV(u8, u8, u8),
430//! #     Grey(u8),
431//! #     FromOutOfSpace,
432//! #     #[variantly(rename = "darkness")]
433//! #     Black,
434//! # }
435//! let color = Color::FromOutOfSpace;
436//! assert!(color.is_from_out_of_space());
437//! ```
438//!
439//! *Note: Available for all variant types*
440//!
441//! ### `pub fn is_not_{variant_name}(self) -> bool`
442//! Returns `true` if the enum is *not* of the given variant.
443//!
444//! #### Example
445//! ```
446//! # #[derive(variantly::Variantly, Debug, PartialEq)]
447//! # enum Color {
448//! #     RGB(u8, u8, u8),
449//! #     HSV(u8, u8, u8),
450//! #     Grey(u8),
451//! #     FromOutOfSpace,
452//! #     #[variantly(rename = "darkness")]
453//! #     Black,
454//! # }
455//! let color = Color::HSV(1,2,3);
456//! assert!(color.is_not_rgb());
457//! ```
458//!
459//! *Note: Available for all variant types*
460//!
461//! ## Compare & Process Specific Variant
462//! Use the below to process and compare a specific enum variant.
463//!
464//! ### `pub fn and_{variant_name}(self, enum_b: GivenEnum) -> GivenEnum`
465//! Returns `enum_b` if both self and `enum_b` are of the given variant. Otherwise returns `self`.
466//!
467//! #### Example
468//! ```
469//! # #[derive(variantly::Variantly, Debug, PartialEq)]
470//! # enum Color {
471//! #     RGB(u8, u8, u8),
472//! #     HSV(u8, u8, u8),
473//! #     Grey(u8),
474//! #     FromOutOfSpace,
475//! #     #[variantly(rename = "darkness")]
476//! #     Black,
477//! # }
478//! let color_a = Color::HSV(1,2,3);
479//! let color_b = Color::HSV(4,5,6);
480//! let and = color_a.and_hsv(color_b);
481//! assert_eq!(
482//!     and,
483//!     Color::HSV(4,5,6),
484//! );
485//! ```
486//!
487//! *Available for all variant types*
488//!
489//! ### `pub fn and_then_{variant_name}<F: FnOnce((...)) -> (...)>(self, f: F) -> Self`
490//! Returns the enum as is if it is not of the given variant, otherwise calls `f` with the wrapped value and returns the result.
491//!
492//! #### Example
493//! ```
494//! # #[derive(variantly::Variantly, Debug, PartialEq)]
495//! # enum Color {
496//! #     RGB(u8, u8, u8),
497//! #     HSV(u8, u8, u8),
498//! #     Grey(u8),
499//! #     FromOutOfSpace,
500//! #     #[variantly(rename = "darkness")]
501//! #     Black,
502//! # }
503//! let color_a = Color::HSV(1,2,3);
504//!
505//! let and = color_a.and_then_hsv(|(h, s, _)| (h, s, 4));
506//! assert_eq!(
507//!     and,
508//!     Color::HSV(1, 2, 4),
509//! );
510//! ```
511//!
512//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
513//!
514//! ### `pub fn or_{variant_name}(self, enum_b: GivenEnum) -> GivenEnum`
515//! Returns `self` if it is of the given variant, otherwise returns `enum_b`.
516//!
517//! #### Example
518//! ```
519//! # #[derive(variantly::Variantly, Debug, PartialEq)]
520//! # enum Color {
521//! #     RGB(u8, u8, u8),
522//! #     HSV(u8, u8, u8),
523//! #     Grey(u8),
524//! #     FromOutOfSpace,
525//! #     #[variantly(rename = "darkness")]
526//! #     Black,
527//! # }
528//! let color_a = Color::HSV(1,2,3);
529//! let color_b = Color::RGB(4,5,6);
530//! let or = color_a.or_rgb(color_b);
531//! assert_eq!(
532//!     or,
533//!     Color::RGB(4,5,6),
534//! );
535//! ```
536//!
537//! *Available for all variant types*
538//!
539//! ### `pub fn or_else_{variant_name}<F: FnOnce() -> (...)>(self, f: F) -> Self {`
540//! Returns `self` if it is of the given variant, otherwise calls `f` and returns the result.
541//!
542//! #### Example
543//! ```
544//! # #[derive(variantly::Variantly, Debug, PartialEq)]
545//! # enum Color {
546//! #     RGB(u8, u8, u8),
547//! #     HSV(u8, u8, u8),
548//! #     Grey(u8),
549//! #     FromOutOfSpace,
550//! #     #[variantly(rename = "darkness")]
551//! #     Black,
552//! # }
553//! let color = Color::HSV(1,2,3);
554//! let color = color.or_else_rgb(|| (4,5,6));
555//! assert_eq!(
556//!     color,
557//!     Color::RGB(4,5,6),
558//! );
559//! ```
560//!
561//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
562//!
563//! # Renaming Methods
564//! The `variantly` attribute may be placed on a variant in order to customize the resulting method names. The value set against `rename` inside the attribute will be used in place of the snake_cased variant name when constructing derived method names.
565//! ```
566//! #[derive(variantly::Variantly)]
567//! enum SomeEnum {
568//!     #[variantly(rename = "variant_a")]
569//!     SomeVariantWithALongName(String),
570//!     VariantB,
571//! }
572//!
573//! let variant = SomeEnum::SomeVariantWithALongName(String::from("Hello"));
574//! assert!(variant.is_variant_a());
575//! ```
576//! Methods associated with `SomeVariantWithALongName` will now be accessible only with the `variant_a`
577//! suffix, such as `.unwrap_or_else_variant_a()`. This can help control overly verbose fn names.
578//! Note that the input to `rename` is used as is and is not coerced into snake_case.
579//!
580//! The above is also relevant when two variant names would expand to create conflicting method names:
581//! ```
582//! #[derive(variantly::Variantly)]
583//! enum SomeEnum {
584//!     #[variantly(rename = "capital")]
585//!     ABC,
586//!     #[variantly(rename = "lower")]
587//!     abc,
588//! }
589//! ```
590//! Without the `rename` attribute in the above, both variants would create conflicting methods such as `.is_abc()` due to the coercion to snake_case.
591//! This is avoided by using the rename input to create meaningful and unique fn names.
592//!
593//! #### License
594//!
595//! <sup>
596//! Licensed under <a href="LICENSE">MIT license</a>.
597//! </sup>
598//!
599//! <br>
600//!
601//! <sub>
602//! Unless you explicitly state otherwise, any contribution intentionally submitted
603//! for inclusion in this crate shall be licensed as above, without any additional terms or conditions.
604//! </sub>
605
606#[macro_use]
607extern crate darling;
608extern crate proc_macro;
609
610#[macro_use]
611mod idents;
612
613mod derive;
614mod error;
615mod input;
616
617use derive::derive_variantly_fns;
618use proc_macro::TokenStream;
619use syn::{parse_macro_input, ItemEnum};
620
621/// The `Variantly` derive macro. See [the module level documentation](self) for more information.
622#[proc_macro_derive(Variantly, attributes(variantly))]
623pub fn variantly(input: TokenStream) -> TokenStream {
624    let item_enum = parse_macro_input!(input as ItemEnum);
625    derive_variantly_fns(item_enum).unwrap_or_else(|err| err.into_compile_error())
626}