wolfrpg_map_parser/command/db_management_command/
string.rs

1use crate::byte_utils::parse_string;
2#[cfg(feature = "serde")]
3use serde::{Serialize, Deserialize};
4use std::string::String as StdString;
5use crate::command::db_management_command::state::State;
6
7type DBStrings = (Option<StdString>, Option<StdString>, Option<StdString>);
8
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[derive(PartialEq, Clone)]
11pub struct String {
12    value: StdString,
13}
14
15impl String {
16    pub(crate) fn parse(bytes: &[u8]) -> (usize, Self, DBStrings) {
17        let mut offset: usize = 0;
18
19        offset += 1; // padding
20
21        // I could not manually create a command with string count != 4, but I found a map 
22        // that has a command with string count 0, so we'll handle that case.
23        let string_count: u8 = bytes[offset];
24        offset += 1;
25
26        let (bytes_read, value): (usize, StdString) = parse_string(&bytes[offset..]);
27        offset += bytes_read;
28
29        let (bytes_read, db_strings): (usize, DBStrings)
30            = State::parse_strings(string_count, &bytes[offset..]);
31        offset += bytes_read;
32
33        let state: Self = Self {
34            value
35        };
36
37        (offset, state, db_strings)
38    }
39
40    pub fn value(&self) -> &str {
41        &self.value
42    }
43
44    pub fn value_mut(&mut self) -> &mut StdString {
45        &mut self.value
46    }
47}