|
| 1 | +# Kinded |
| 2 | + |
| 3 | +Generate Rust enum kind types without boilerplate. |
| 4 | + |
| 5 | +## Get Started |
| 6 | + |
| 7 | +```rs |
| 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 | +```rs |
| 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 | +```rs |
| 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 | + |
| 72 | +### Derive traits |
| 73 | + |
| 74 | +By default the kind type implements the following traits: `Debug`, `Clone`, `Copy`, `PartialEq`, `Eq`, `From<T>`, `From<&T>`. |
| 75 | + |
| 76 | +Extra traits can be derived with `derive(..)` attribute: |
| 77 | + |
| 78 | +```rs |
| 79 | +use kinded::Kinded; |
| 80 | +use std::collections::HashSet; |
| 81 | + |
| 82 | +#[derive(Kinded)] |
| 83 | +#[kinded(derive(Hash))] |
| 84 | +enum Drink { |
| 85 | + Mate, |
| 86 | + Coffee(String), |
| 87 | + Tea { variety: String, caffeine: bool } |
| 88 | +} |
| 89 | + |
| 90 | +let mut drink_kinds = HashSet::new(); |
| 91 | +drink_kinds.insert(DrinkKind::Mate); |
| 92 | +``` |
| 93 | + |
| 94 | +## A note about enum-kinds |
| 95 | + |
| 96 | +There is a very similar crate [enum-kinds](https://github.com/Soft/enum-kinds) that does almost the same job. |
| 97 | + |
| 98 | +The main difference between `kinded` and `enum-kinds` crate is that `kinded` provides the `Kinded` trait, on top of which |
| 99 | +users can implement abstract functions and use them with different enum types. |
| 100 | + |
| 101 | +Another minor difference is that apart from `From<T>` and `From<&T>` conversions, `kidned` also implements `kind()` function on the enum type. |
| 102 | + |
| 103 | +## A note about the war in Ukraine 🇺🇦 |
| 104 | + |
| 105 | +Today I live in Berlin, I have the luxury to live a physically safe life. |
| 106 | +But I am Ukrainian. The first 25 years of my life I spent in [Kharkiv](https://en.wikipedia.org/wiki/Kharkiv), |
| 107 | +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. |
| 108 | +My parents, my relatives and my friends had to survive the artillery and air attack, living for over a month in basements. |
| 109 | + |
| 110 | +Some of them have managed to evacuate to EU. Some others are trying to live "normal lifes" in Kharkiv, doing there daily duties. |
| 111 | +And some are at the front line right now, risking their lives every second to protect the rest. |
| 112 | + |
| 113 | +I encourage you to donate to [Charity foundation of Serhiy Prytula](https://prytulafoundation.org/en). |
| 114 | +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. |
| 115 | +Your contribution to the Ukrainian military force is a contribution to my calmness, so I can spend more time developing the project. |
| 116 | + |
| 117 | +Thank you. |
| 118 | + |
| 119 | + |
| 120 | +## License |
| 121 | + |
| 122 | +MIT © [Serhii Potapov](https://www.greyblake.com) |
0 commit comments