simetry/iracing/
car_positions.rs

1use crate::iracing::{BitField, VarData, VarType};
2
3#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
4pub enum CarPositions {
5    /// Check is off.
6    #[default]
7    Off = 0,
8    /// No cars around us.
9    Clear = 1,
10    /// There is a car to our left.
11    CarLeft = 2,
12    /// There is a car to our right.
13    CarRight = 3,
14    /// There are cars on each side.
15    CarLeftRight = 4,
16    /// There are two cars to our left.
17    CarsLeft = 5,
18    /// There are two cars to our right.
19    CarsRight = 6,
20}
21
22impl CarPositions {
23    pub fn car_left(self) -> bool {
24        match self {
25            CarPositions::CarLeft | CarPositions::CarLeftRight | CarPositions::CarsLeft => true,
26            CarPositions::Off
27            | CarPositions::Clear
28            | CarPositions::CarRight
29            | CarPositions::CarsRight => false,
30        }
31    }
32
33    pub fn car_right(self) -> bool {
34        match self {
35            CarPositions::CarRight | CarPositions::CarLeftRight | CarPositions::CarsRight => true,
36            CarPositions::Off
37            | CarPositions::Clear
38            | CarPositions::CarLeft
39            | CarPositions::CarsLeft => false,
40        }
41    }
42}
43
44impl VarData for CarPositions {
45    fn parse(var_type: VarType, data: &[u8]) -> Option<Self> {
46        let bit_field = BitField::parse(var_type, data)?;
47        Some(match bit_field.0 {
48            0 => CarPositions::Off,
49            1 => CarPositions::Clear,
50            2 => CarPositions::CarLeft,
51            3 => CarPositions::CarRight,
52            4 => CarPositions::CarLeftRight,
53            5 => CarPositions::CarsLeft,
54            6 => CarPositions::CarsRight,
55            _ => return None,
56        })
57    }
58}