Skip to main content

datex_core/global/protocol_structures/
injected_values.rs

1use crate::{
2    global::protocol_structures::instruction_data::StackIndex, prelude::*,
3};
4use binrw::{
5    BinRead, BinResult, BinWrite, Endian,
6    io::{Read, Seek, Write},
7};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
10pub enum SharedInjectedValueType {
11    // shared x
12    Move = 2,
13    // 'mut shared mut x
14    RefMut = 1,
15    // 'shared x
16    Ref = 0,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
20pub enum LocalInjectedValueType {
21    /// The value is moved into the child scope and no longer used afterward
22    Move = 2,
23    /// The value is temporarily borrowed in the child scope - the changed value must be written back to the parent scope afterward
24    RefMut = 1,
25    /// The value is moved into the child scope but still used afterward (clone or immutable ref (&x))
26    Copy = 0,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub enum InjectedValueType {
31    Local(LocalInjectedValueType),
32    Shared(SharedInjectedValueType),
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, BinRead, BinWrite)]
36pub struct InjectedValueDeclaration {
37    pub(crate) index: StackIndex,
38    pub(crate) ty: InjectedValueType,
39}
40
41impl From<InjectedValueType> for u8 {
42    fn from(injected_value_type: InjectedValueType) -> Self {
43        match injected_value_type {
44            InjectedValueType::Local(local_type) => match local_type {
45                LocalInjectedValueType::Move => 0,
46                LocalInjectedValueType::Copy => 1,
47                LocalInjectedValueType::RefMut => 2,
48            },
49            InjectedValueType::Shared(shared_type) => match shared_type {
50                SharedInjectedValueType::Move => 3,
51                SharedInjectedValueType::Ref => 4,
52                SharedInjectedValueType::RefMut => 5,
53            },
54        }
55    }
56}
57
58impl TryFrom<u8> for InjectedValueType {
59    type Error = ();
60    fn try_from(value: u8) -> Result<Self, Self::Error> {
61        match value {
62            0 => Ok(InjectedValueType::Local(LocalInjectedValueType::Move)),
63            1 => Ok(InjectedValueType::Local(LocalInjectedValueType::Copy)),
64            2 => Ok(InjectedValueType::Local(LocalInjectedValueType::RefMut)),
65            3 => Ok(InjectedValueType::Shared(SharedInjectedValueType::Move)),
66            4 => Ok(InjectedValueType::Shared(SharedInjectedValueType::Ref)),
67            5 => Ok(InjectedValueType::Shared(SharedInjectedValueType::RefMut)),
68            _ => Err(()),
69        }
70    }
71}
72
73impl BinWrite for InjectedValueType {
74    type Args<'a> = ();
75
76    fn write_options<W: Write + Seek>(
77        &self,
78        writer: &mut W,
79        endian: Endian,
80        _: Self::Args<'_>,
81    ) -> BinResult<()> {
82        // only handle le for now
83        if endian != Endian::Little {
84            return Err(binrw::Error::AssertFail {
85                pos: writer.stream_position().unwrap_or(0),
86                message:
87                    "Only little-endian is supported for InjectedValueType"
88                        .to_string(),
89            });
90        }
91        // write type
92        writer.write_all(&[u8::from(*self)])?;
93
94        Ok(())
95    }
96}
97
98impl BinRead for InjectedValueType {
99    type Args<'a> = ();
100
101    fn read_options<R: Read + Seek>(
102        reader: &mut R,
103        endian: Endian,
104        _: Self::Args<'_>,
105    ) -> BinResult<Self> {
106        // only handle le for now
107        if endian != Endian::Little {
108            return Err(binrw::Error::AssertFail {
109                pos: reader.stream_position().unwrap_or(0),
110                message:
111                    "Only little-endian is supported for InjectedValueType"
112                        .to_string(),
113            });
114        }
115        // read type
116        let mut type_buf = [0u8; 1];
117        reader.read_exact(&mut type_buf)?;
118        let value_type =
119            InjectedValueType::try_from(type_buf[0]).map_err(|_| {
120                binrw::Error::AssertFail {
121                    pos: reader.stream_position().unwrap_or(0),
122                    message: format!(
123                        "Invalid InjectedValueType value: {}",
124                        type_buf[0]
125                    ),
126                }
127            })?;
128        Ok(value_type)
129    }
130}