Skip to main content

source2_demo/event/
value.rs

1use crate::error::GameEventError;
2
3/// Value type for game event fields.
4///
5/// Game events contain named fields with values. This enum represents
6/// all possible value types that can appear in game events.
7///
8/// # Examples
9///
10/// ```no_run
11/// use source2_demo::prelude::*;
12///
13/// # fn example(ge: &GameEvent) -> anyhow::Result<()> {
14/// // Get a value and convert it
15/// let value = ge.get_value("player_id")?;
16/// let player_id: i32 = value.try_into()?;
17/// # Ok(())
18/// # }
19/// ```
20#[derive(Debug)]
21pub enum EventValue {
22    /// String value
23    String(String),
24    /// 32-bit floating point value
25    Float(f32),
26    /// Signed 32-bit integer
27    Int(i32),
28    /// Boolean value
29    Bool(bool),
30    /// Unsigned 8-bit integer (byte)
31    Byte(u8),
32    /// Unsigned 64-bit integer
33    U64(u64),
34}
35
36impl TryInto<String> for EventValue {
37    type Error = GameEventError;
38
39    fn try_into(self) -> Result<String, GameEventError> {
40        if let EventValue::String(x) = self {
41            Ok(x)
42        } else {
43            Err(GameEventError::ConversionError(
44                format!("{:?}", self),
45                "String".to_string(),
46            ))
47        }
48    }
49}
50
51impl TryInto<String> for &EventValue {
52    type Error = GameEventError;
53
54    fn try_into(self) -> Result<String, GameEventError> {
55        if let EventValue::String(x) = self {
56            Ok(x.to_owned())
57        } else {
58            Err(GameEventError::ConversionError(
59                format!("{:?}", self),
60                "String".to_string(),
61            ))
62        }
63    }
64}
65
66impl TryInto<bool> for EventValue {
67    type Error = GameEventError;
68
69    fn try_into(self) -> Result<bool, GameEventError> {
70        if let EventValue::Bool(x) = self {
71            Ok(x)
72        } else {
73            Err(GameEventError::ConversionError(
74                format!("{:?}", self),
75                "String".to_string(),
76            ))
77        }
78    }
79}
80
81impl TryInto<bool> for &EventValue {
82    type Error = GameEventError;
83
84    fn try_into(self) -> Result<bool, GameEventError> {
85        if let EventValue::Bool(x) = self {
86            Ok(*x)
87        } else {
88            Err(GameEventError::ConversionError(
89                format!("{:?}", self),
90                "String".to_string(),
91            ))
92        }
93    }
94}
95
96impl TryInto<f32> for EventValue {
97    type Error = GameEventError;
98
99    fn try_into(self) -> Result<f32, GameEventError> {
100        if let EventValue::Float(x) = self {
101            Ok(x)
102        } else {
103            Err(GameEventError::ConversionError(
104                format!("{:?}", self),
105                "f32".to_string(),
106            ))
107        }
108    }
109}
110
111impl TryInto<f32> for &EventValue {
112    type Error = GameEventError;
113
114    fn try_into(self) -> Result<f32, GameEventError> {
115        if let EventValue::Float(x) = self {
116            Ok(*x)
117        } else {
118            Err(GameEventError::ConversionError(
119                format!("{:?}", self),
120                "f32".to_string(),
121            ))
122        }
123    }
124}
125
126macro_rules! impl_try_into_for_integers {
127    ($target:ty) => {
128        impl TryInto<$target> for EventValue {
129            type Error = GameEventError;
130
131            fn try_into(self) -> Result<$target, GameEventError> {
132                match self {
133                    EventValue::Int(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
134                        GameEventError::ConversionError(
135                            format!("{:?}", x),
136                            stringify!($target).to_string(),
137                        )
138                    })?),
139                    EventValue::Byte(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
140                        GameEventError::ConversionError(
141                            format!("{:?}", x),
142                            stringify!($target).to_string(),
143                        )
144                    })?),
145                    EventValue::U64(x) => Ok(TryInto::<$target>::try_into(x).map_err(|_| {
146                        GameEventError::ConversionError(
147                            format!("{:?}", x),
148                            stringify!($target).to_string(),
149                        )
150                    })?),
151                    EventValue::Float(x) => Ok(x as $target),
152                    _ => Err(GameEventError::ConversionError(
153                        format!("{:?}", self),
154                        stringify!($target).to_string(),
155                    )),
156                }
157            }
158        }
159
160        impl TryInto<$target> for &EventValue {
161            type Error = GameEventError;
162
163            fn try_into(self) -> Result<$target, GameEventError> {
164                match self {
165                    EventValue::Int(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
166                        GameEventError::ConversionError(
167                            format!("{:?}", *x),
168                            stringify!($target).to_string(),
169                        )
170                    })?),
171                    EventValue::Byte(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
172                        GameEventError::ConversionError(
173                            format!("{:?}", x),
174                            stringify!($target).to_string(),
175                        )
176                    })?),
177                    EventValue::U64(x) => Ok(TryInto::<$target>::try_into(*x).map_err(|_| {
178                        GameEventError::ConversionError(
179                            format!("{:?}", *x),
180                            stringify!($target).to_string(),
181                        )
182                    })?),
183                    EventValue::Float(x) => Ok(*x as $target),
184                    _ => Err(GameEventError::ConversionError(
185                        format!("{:?}", self),
186                        stringify!($target).to_string(),
187                    )),
188                }
189            }
190        }
191    };
192}
193
194impl_try_into_for_integers!(i8);
195impl_try_into_for_integers!(i16);
196impl_try_into_for_integers!(i32);
197impl_try_into_for_integers!(i64);
198impl_try_into_for_integers!(i128);
199impl_try_into_for_integers!(u8);
200impl_try_into_for_integers!(u16);
201impl_try_into_for_integers!(u32);
202impl_try_into_for_integers!(u64);
203impl_try_into_for_integers!(u128);
204impl_try_into_for_integers!(usize);
205impl_try_into_for_integers!(isize);