source2_demo/event/
value.rs

1use crate::error::GameEventError;
2
3#[derive(Debug)]
4pub enum EventValue {
5    String(String),
6    Float(f32),
7    Int(i32),
8    Bool(bool),
9    Byte(u8),
10    U64(u64),
11}
12
13impl TryInto<String> for EventValue {
14    type Error = GameEventError;
15
16    fn try_into(self) -> Result<String, GameEventError> {
17        if let EventValue::String(x) = self {
18            Ok(x)
19        } else {
20            Err(GameEventError::ConversionError(
21                format!("{:?}", self),
22                "String".to_string(),
23            ))
24        }
25    }
26}
27
28impl TryInto<String> for &EventValue {
29    type Error = GameEventError;
30
31    fn try_into(self) -> Result<String, GameEventError> {
32        if let EventValue::String(x) = self {
33            Ok(x.to_owned())
34        } else {
35            Err(GameEventError::ConversionError(
36                format!("{:?}", self),
37                "String".to_string(),
38            ))
39        }
40    }
41}
42
43impl TryInto<bool> for EventValue {
44    type Error = GameEventError;
45
46    fn try_into(self) -> Result<bool, GameEventError> {
47        if let EventValue::Bool(x) = self {
48            Ok(x)
49        } else {
50            Err(GameEventError::ConversionError(
51                format!("{:?}", self),
52                "String".to_string(),
53            ))
54        }
55    }
56}
57
58impl TryInto<bool> for &EventValue {
59    type Error = GameEventError;
60
61    fn try_into(self) -> Result<bool, GameEventError> {
62        if let EventValue::Bool(x) = self {
63            Ok(*x)
64        } else {
65            Err(GameEventError::ConversionError(
66                format!("{:?}", self),
67                "String".to_string(),
68            ))
69        }
70    }
71}
72
73impl TryInto<f32> for EventValue {
74    type Error = GameEventError;
75
76    fn try_into(self) -> Result<f32, GameEventError> {
77        if let EventValue::Float(x) = self {
78            Ok(x)
79        } else {
80            Err(GameEventError::ConversionError(
81                format!("{:?}", self),
82                "f32".to_string(),
83            ))
84        }
85    }
86}
87
88impl TryInto<f32> for &EventValue {
89    type Error = GameEventError;
90
91    fn try_into(self) -> Result<f32, GameEventError> {
92        if let EventValue::Float(x) = self {
93            Ok(*x)
94        } else {
95            Err(GameEventError::ConversionError(
96                format!("{:?}", self),
97                "f32".to_string(),
98            ))
99        }
100    }
101}
102
103macro_rules! impl_try_into_for_integers {
104    ($target:ty) => {
105        impl TryInto<$target> for EventValue {
106            type Error = GameEventError;
107
108            fn try_into(self) -> Result<$target, GameEventError> {
109                match self {
110                    EventValue::Int(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
111                        GameEventError::ConversionError(
112                            format!("{:?}", x),
113                            stringify!($target).to_string(),
114                        )
115                    })?),
116                    EventValue::Byte(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
117                        GameEventError::ConversionError(
118                            format!("{:?}", x),
119                            stringify!($target).to_string(),
120                        )
121                    })?),
122                    EventValue::U64(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
123                        GameEventError::ConversionError(
124                            format!("{:?}", x),
125                            stringify!($target).to_string(),
126                        )
127                    })?),
128                    EventValue::Float(x) => Ok(x as $target),
129                    _ => Err(GameEventError::ConversionError(
130                        format!("{:?}", self),
131                        stringify!($target).to_string(),
132                    )),
133                }
134            }
135        }
136
137        impl TryInto<$target> for &EventValue {
138            type Error = GameEventError;
139
140            fn try_into(self) -> Result<$target, GameEventError> {
141                match self {
142                    EventValue::Int(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
143                        GameEventError::ConversionError(
144                            format!("{:?}", *x),
145                            stringify!($target).to_string(),
146                        )
147                    })?),
148                    EventValue::Byte(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
149                        GameEventError::ConversionError(
150                            format!("{:?}", x),
151                            stringify!($target).to_string(),
152                        )
153                    })?),
154                    EventValue::U64(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
155                        GameEventError::ConversionError(
156                            format!("{:?}", *x),
157                            stringify!($target).to_string(),
158                        )
159                    })?),
160                    EventValue::Float(x) => Ok(*x as $target),
161                    _ => Err(GameEventError::ConversionError(
162                        format!("{:?}", self),
163                        stringify!($target).to_string(),
164                    )),
165                }
166            }
167        }
168    };
169}
170
171impl_try_into_for_integers!(i8);
172impl_try_into_for_integers!(i16);
173impl_try_into_for_integers!(i32);
174impl_try_into_for_integers!(i64);
175impl_try_into_for_integers!(i128);
176impl_try_into_for_integers!(u8);
177impl_try_into_for_integers!(u16);
178impl_try_into_for_integers!(u32);
179impl_try_into_for_integers!(u64);
180impl_try_into_for_integers!(u128);
181impl_try_into_for_integers!(usize);
182impl_try_into_for_integers!(isize);