wolfrpg_map_parser/command/db_management_command/
state.rs1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::byte_utils::parse_string;
4use crate::command::db_management_command::base::Base;
5use crate::command::db_management_command::csv::CSV;
6use crate::command::db_management_command::string;
7
8type DBStrings = (Option<String>, Option<String>, Option<String>);
9
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[derive(PartialEq, Clone)]
12pub enum State {
13 Base(Base),
14 String(string::String),
15 CSV(CSV)
16}
17
18impl State {
19 pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self, DBStrings) {
20 let (bytes_read, command, strings): (usize, Base, DBStrings) = Base::parse(bytes);
21
22 (bytes_read, Self::Base(command), strings)
23 }
24
25 pub(crate) fn parse_string(bytes: &[u8]) -> (usize, Self, DBStrings) {
26 let (bytes_read, command, strings): (usize, string::String, DBStrings)
27 = string::String::parse(bytes);
28
29 (bytes_read, Self::String(command), strings)
30 }
31
32 pub(crate) fn parse_csv(bytes: &[u8]) -> (usize, Self, DBStrings) {
33 let (bytes_read, command, strings): (usize, CSV, DBStrings) = CSV::parse(bytes);
34
35 (bytes_read, Self::CSV(command), strings)
36 }
37
38 pub(crate) fn parse_strings(string_count: u8, bytes: &[u8]) -> (usize, DBStrings) {
39 let mut offset: usize = 0;
40
41 let db_type_string: Option<String> = if string_count > 1 {
42 let (bytes_read, db_type_string): (usize, String) = parse_string(&bytes[offset..]);
43 offset += bytes_read;
44 Some(db_type_string)
45 } else {
46 None
47 };
48
49 let data_string: Option<String> = if string_count > 2 {
50 let (bytes_read, data_string): (usize, String) = parse_string(&bytes[offset..]);
51 offset += bytes_read;
52 Some(data_string)
53 } else {
54 None
55 };
56
57 let field_string: Option<String> = if string_count > 3 {
58 let (bytes_read, field_string): (usize, String) = parse_string(&bytes[offset..]);
59 offset += bytes_read;
60 Some(field_string)
61 } else {
62 None
63 };
64
65 (offset, (db_type_string, data_string, field_string))
66 }
67}