enum_conversion_traits/
lib.rs1use std::{error::Error, fmt};
2
3#[derive(Debug)]
6pub struct EnumConversionError {
7 pub name: String,
8 pub requested_type: String,
9}
10
11impl EnumConversionError {
12 pub fn new(name: &str, requested_type: &str) -> EnumConversionError {
14 EnumConversionError {
15 name: name.to_string(),
16 requested_type: requested_type.to_string(),
17 }
18 }
19}
20impl Error for EnumConversionError {}
21
22impl fmt::Display for EnumConversionError {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 write!(
25 f,
26 "EnumConversionError :: Active field of enum <{}> is not of type <{}>",
27 self.name, self.requested_type,
28 )
29 }
30}
31
32pub trait GetVariant<T, Marker> {
38 fn get_variant(self) -> Result<T, EnumConversionError>;
39 fn get_variant_ref(&self) -> Result<&T, EnumConversionError>;
40 fn get_variant_mut(&mut self) -> Result<&mut T, EnumConversionError>;
41}
42
43pub trait TryTo<T> {
50 type Error;
51 fn try_to(self) -> Result<T, Self::Error>;
52}