|
| 1 | +//! # Kinded |
| 2 | +//! |
| 3 | +//! Generate Rust enum kind types without boilerplate. |
| 4 | +//! |
| 5 | +//! ## Get Started |
| 6 | +//! |
| 7 | +//! ``` |
| 8 | +//! use kinded::Kinded; |
| 9 | +//! |
| 10 | +//! #[derive(Kinded)] |
| 11 | +//! enum Drink { |
| 12 | +//! Mate, |
| 13 | +//! Coffee(String), |
| 14 | +//! Tea { variety: String, caffeine: bool } |
| 15 | +//! } |
| 16 | +//! |
| 17 | +//! let drink = Drink::Coffee("Espresso".to_owned()); |
| 18 | +//! assert_eq!(drink.kind(), DrinkKind::Coffee); |
| 19 | +//! ``` |
| 20 | +//! |
| 21 | +//! Note, the definition of `DrinkKind` enum was generated automatically as well as `Drink::kind()` method. |
| 22 | +//! |
| 23 | +//! ## Kinded trait |
| 24 | +//! |
| 25 | +//! The library provides `Kinded` trait: |
| 26 | +//! |
| 27 | +//! ```rs |
| 28 | +//! pub trait Kinded { |
| 29 | +//! type Kind: PartialEq + Eq + Debug + Clone + Copy; |
| 30 | +//! |
| 31 | +//! fn kind(&self) -> Self::Kind; |
| 32 | +//! } |
| 33 | +//! ``` |
| 34 | +//! |
| 35 | +//! From the example above, the derived implementation of `Kinded` for `Drink` resembles the following: |
| 36 | +//! |
| 37 | +//! ```ignore |
| 38 | +//! impl Kinded for Drink { |
| 39 | +//! type Kind = DrinkKind; |
| 40 | +//! |
| 41 | +//! fn kind(&self) -> DrinkKind { |
| 42 | +//! match self { |
| 43 | +//! Drink::Mate => DrinkKind::Mate, |
| 44 | +//! Drink::Coffee(..) => DrinkKind::Coffee, |
| 45 | +//! Drink::Tea { .. } => DrinkKind::Tea, |
| 46 | +//! } |
| 47 | +//! } |
| 48 | +//! } |
| 49 | +//! ``` |
| 50 | +//! |
| 51 | +//! The `Kinded` trait allows to build abstract functions that can be used with different enum types. |
| 52 | +//! |
| 53 | +//! ## Attributes |
| 54 | +//! |
| 55 | +//! ### Custom kind type name |
| 56 | +//! |
| 57 | +//! By default the kind type name is generated by adding postfix `Kind` to the original enum name. |
| 58 | +//! This can be customized with `kind = ` attribute: |
| 59 | +//! |
| 60 | +//! ``` |
| 61 | +//! use kinded::Kinded; |
| 62 | +//! |
| 63 | +//! #[derive(Kinded)] |
| 64 | +//! #[kinded(kind = SimpleDrink)] |
| 65 | +//! enum Drink { |
| 66 | +//! Mate, |
| 67 | +//! Coffee(String), |
| 68 | +//! Tea { variety: String, caffeine: bool } |
| 69 | +//! } |
| 70 | +//! |
| 71 | +//! assert_eq!(Drink::Mate.kind(), SimpleDrink::Mate); |
| 72 | +//! ``` |
| 73 | +//! |
| 74 | +//! ### Derive traits |
| 75 | +//! |
| 76 | +//! By default the kind type implements the following traits: `Debug`, `Clone`, `Copy`, `PartialEq`, `Eq`, `From<T>`, `From<&T>`. |
| 77 | +//! |
| 78 | +//! Extra traits can be derived with `derive(..)` attribute: |
| 79 | +//! |
| 80 | +//! ``` |
| 81 | +//! use kinded::Kinded; |
| 82 | +//! use std::collections::HashSet; |
| 83 | +//! |
| 84 | +//! #[derive(Kinded)] |
| 85 | +//! #[kinded(derive(Hash))] |
| 86 | +//! enum Drink { |
| 87 | +//! Mate, |
| 88 | +//! Coffee(String), |
| 89 | +//! Tea { variety: String, caffeine: bool } |
| 90 | +//! } |
| 91 | +//! |
| 92 | +//! let mut drink_kinds = HashSet::new(); |
| 93 | +//! drink_kinds.insert(DrinkKind::Mate); |
| 94 | +//! ``` |
| 95 | +//! |
| 96 | +//! ## A note about enum-kinds |
| 97 | +//! |
| 98 | +//! There is a very similar crate [enum-kinds](https://github.com/Soft/enum-kinds) that does almost the same job. |
| 99 | +//! |
| 100 | +//! The main difference between `kinded` and `enum-kinds` crate is that `kinded` provides the `Kinded` trait, on top of which |
| 101 | +//! users can implement abstract functions and use them with different enum types. |
| 102 | +//! |
| 103 | +//! Another minor difference is that apart from `From<T>` and `From<&T>` conversions, `kidned` also implements `kind()` function on the enum type. |
| 104 | +//! |
| 105 | +//! ## A note about the war in Ukraine 🇺🇦 |
| 106 | +//! |
| 107 | +//! Today I live in Berlin, I have the luxury to live a physically safe life. |
| 108 | +//! But I am Ukrainian. The first 25 years of my life I spent in [Kharkiv](https://en.wikipedia.org/wiki/Kharkiv), |
| 109 | +//! the second-largest city in Ukraine, 60km away from the border with russia. Today about [a third of my home city is destroyed](https://www.youtube.com/watch?v=ihoufBFSZds) by russians. |
| 110 | +//! My parents, my relatives and my friends had to survive the artillery and air attack, living for over a month in basements. |
| 111 | +//! |
| 112 | +//! Some of them have managed to evacuate to EU. Some others are trying to live "normal lifes" in Kharkiv, doing there daily duties. |
| 113 | +//! And some are at the front line right now, risking their lives every second to protect the rest. |
| 114 | +//! |
| 115 | +//! I encourage you to donate to [Charity foundation of Serhiy Prytula](https://prytulafoundation.org/en). |
| 116 | +//! Just pick the project you like and donate. This is one of the best-known foundations, you can watch a [little documentary](https://www.youtube.com/watch?v=VlmWqoeub1Q) about it. |
| 117 | +//! Your contribution to the Ukrainian military force is a contribution to my calmness, so I can spend more time developing the project. |
| 118 | +//! |
| 119 | +//! Thank you. |
| 120 | +//! |
| 121 | +//! |
| 122 | +//! ## License |
| 123 | +//! |
| 124 | +//! MIT © [Serhii Potapov](https://www.greyblake.com) |
| 125 | +
|
| 126 | + |
1 | 127 | pub use kinded_macros::Kinded; |
2 | 128 | use std::fmt::Debug; |
3 | 129 |
|
|
0 commit comments