1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
pub use crate*;
/// Implements conversion from inner types into enum variants.
///
/// Add `#[derive(VariantFrom)]` on an enum with single-field variants. This will implement `IntoVariant<YourEnum>` for all the types contained in the variants, and, by consequence, you'll also get `VariantFrom` on `YourEnum`.
///
/// Example:
/// ```
/// use into_variant::{IntoVariant, VariantFrom};
///
/// #[derive(VariantFrom)]
/// enum Fill {
/// Color(Color),
/// Pattern(Pattern),
/// }
///
/// struct Color {}
/// struct Pattern {}
///
/// // now, this works:
/// let variant = Fill::variant_from(Color {});
/// assert!(matches!(variant, Fill::Color(Color{})));
///
/// // or, alternatively:
/// let variant = Color {}.into_variant();
/// assert!(matches!(variant, Fill::Color(Color{})));
/// ```
///
/// If you don't want to implement `IntoVariant` for a particular variant's contents, you can skip it with the `into_variant(skip)` attribute:
///
/// ```
/// use into_variant::{IntoVariant, VariantFrom};
///
/// #[derive(VariantFrom)]
/// enum Fill {
/// Color(Color),
/// #[into_variant(skip)]
/// Pattern(Pattern),
/// }
///
/// struct Color {}
/// struct Pattern {}
///
/// // now, this works:
/// let variant = Fill::variant_from(Color {});
/// // but this doesn't:
/// // let variant = Fill::variant_from(Pattern {});
/// ```
///
/// Variants with nothing in them (empty tuple or unit variants) are automatically skipped:
/// ```
/// use into_variant::VariantFrom;
///
/// #[derive(VariantFrom)]
/// enum Fill {
/// // nothing will be generated for these 2
/// Color(),
/// Pattern,
/// }
/// ```
///
/// `derive(VariantFrom)` only works on variants with a single, simple field (struct or enum), like `Color(Color)`. It does not work for more complex values, such as tuples or arrays. They must be explicitly skipped with `into_variant(skip)` or you'll get a compile-time error.
pub use VariantFrom;