|
| 1 | +use crate::{Reflect, Struct, Tuple}; |
| 2 | + |
| 3 | +pub trait Enum: Reflect { |
| 4 | + fn variant(&self) -> EnumVariant<'_>; |
| 5 | + fn variant_mut(&mut self) -> EnumVariantMut<'_>; |
| 6 | + fn variant_info(&self) -> VariantInfo<'_>; |
| 7 | + fn iter_variants_info(&self) -> VariantInfoIter<'_>; |
| 8 | + fn get_index_name(&self, index: usize) -> Option<&str>; |
| 9 | + fn get_index_from_name(&self, name: &str) -> Option<usize>; |
| 10 | +} |
| 11 | +pub struct VariantInfo<'a> { |
| 12 | + pub index: usize, |
| 13 | + pub name: &'a str, |
| 14 | +} |
| 15 | +pub struct VariantInfoIter<'a> { |
| 16 | + pub(crate) value: &'a dyn Enum, |
| 17 | + pub(crate) index: usize, |
| 18 | + pub(crate) len: usize, |
| 19 | +} |
| 20 | +impl<'a> Iterator for VariantInfoIter<'a> { |
| 21 | + type Item = VariantInfo<'a>; |
| 22 | + |
| 23 | + fn next(&mut self) -> Option<Self::Item> { |
| 24 | + if self.index == self.len { |
| 25 | + return None; |
| 26 | + } |
| 27 | + let item = VariantInfo { |
| 28 | + index: self.index, |
| 29 | + name: self.value.get_index_name(self.index).unwrap(), |
| 30 | + }; |
| 31 | + self.index += 1; |
| 32 | + Some(item) |
| 33 | + } |
| 34 | + |
| 35 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 36 | + let size = self.len - self.index; |
| 37 | + (size, Some(size)) |
| 38 | + } |
| 39 | +} |
| 40 | +impl<'a> ExactSizeIterator for VariantInfoIter<'a> {} |
| 41 | + |
| 42 | +pub enum EnumVariant<'a> { |
| 43 | + Unit, |
| 44 | + NewType(&'a dyn Reflect), |
| 45 | + Tuple(&'a dyn Tuple), |
| 46 | + Struct(&'a dyn Struct), |
| 47 | +} |
| 48 | +pub enum EnumVariantMut<'a> { |
| 49 | + Unit, |
| 50 | + NewType(&'a mut dyn Reflect), |
| 51 | + Tuple(&'a mut dyn Tuple), |
| 52 | + Struct(&'a mut dyn Struct), |
| 53 | +} |
0 commit comments