wolfrpg_map_parser/command/
set_variable_command.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#[cfg(feature = "serde")]
use serde::Serialize;
use state::State;
use crate::byte_utils::as_u32_le;
use crate::command::set_variable_command::operators::Operators;
use crate::command::set_variable_command::options::Options;
pub mod base;
pub mod assignment;
pub mod calculation;
pub mod options;
pub mod operators;
pub mod range;
pub mod state;

#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SetVariableCommand {
    variable: u32,
    left_side: u32,
    right_side: u32,
    options: Options,
    operators: Operators,
    state: State
}

impl SetVariableCommand {
    fn parse(bytes: &[u8], parse_state: fn(&[u8]) -> (usize, State)) -> (usize, Self) {
        let mut offset: usize = 0;

        let variable: u32 = as_u32_le(&bytes[offset..offset + 4]);
        offset += 4;

        let left_side: u32 = as_u32_le(&bytes[offset..offset + 4]);
        offset += 4;

        let right_side: u32 = as_u32_le(&bytes[offset..offset + 4]);
        offset += 4;

        let options: u8 = bytes[offset];
        let options: Options = Options::new(options);
        offset += 1;

        let operators: u8 = bytes[offset];
        let operators: Operators = Operators::new(operators);
        offset += 1;

        let (bytes_read, state): (usize, State) = parse_state(bytes);

        offset += bytes_read;

        (offset, Self {
            variable,
            left_side,
            right_side,
            options,
            operators,
            state
        })
    }

    pub fn parse_base(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_base)
    }

    pub fn parse_range(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_range)
    }

    pub fn variable(&self) -> u32 {
        self.variable
    }

    pub fn variable_mut(&mut self) -> &mut u32 {
        &mut self.variable
    }

    pub fn left_side(&self) -> u32 {
        self.left_side
    }

    pub fn left_side_mut(&mut self) -> &mut u32 {
        &mut self.left_side
    }

    pub fn right_side(&self) -> u32 {
        self.right_side
    }

    pub fn right_side_mut(&mut self) -> &mut u32 {
        &mut self.right_side
    }

    pub fn options(&self) -> &Options {
        &self.options
    }

    pub fn options_mut(&mut self) -> &mut Options {
        &mut self.options
    }

    pub fn operators(&self) -> &Operators {
        &self.operators
    }

    pub fn operators_mut(&mut self) -> &mut Operators {
        &mut self.operators
    }

    pub fn state(&self) -> &State {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }
}