wolfrpg_map_parser/command/db_management_command/
csv.rs

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