wow_dbc/wrath_tables/
gm_survey_current_survey.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::gm_survey_surveys::GMSurveySurveysKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct GMSurveyCurrentSurvey {
12    pub rows: Vec<GMSurveyCurrentSurveyRow>,
13}
14
15impl DbcTable for GMSurveyCurrentSurvey {
16    type Row = GMSurveyCurrentSurveyRow;
17
18    const FILENAME: &'static str = "GMSurveyCurrentSurvey.dbc";
19
20    fn rows(&self) -> &[Self::Row] { &self.rows }
21    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
22
23    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
24        let mut header = [0_u8; HEADER_SIZE];
25        b.read_exact(&mut header)?;
26        let header = parse_header(&header)?;
27
28        if header.record_size != 8 {
29            return Err(crate::DbcError::InvalidHeader(
30                crate::InvalidHeaderError::RecordSize {
31                    expected: 8,
32                    actual: header.record_size,
33                },
34            ));
35        }
36
37        if header.field_count != 2 {
38            return Err(crate::DbcError::InvalidHeader(
39                crate::InvalidHeaderError::FieldCount {
40                    expected: 2,
41                    actual: header.field_count,
42                },
43            ));
44        }
45
46        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
47        b.read_exact(&mut r)?;
48
49        let mut rows = Vec::with_capacity(header.record_count as usize);
50
51        for mut chunk in r.chunks(header.record_size as usize) {
52            let chunk = &mut chunk;
53
54            // id: primary_key (GMSurveyCurrentSurvey) int32
55            let id = GMSurveyCurrentSurveyKey::new(crate::util::read_i32_le(chunk)?);
56
57            // gm_survey_id: foreign_key (GMSurveySurveys) int32
58            let gm_survey_id = GMSurveySurveysKey::new(crate::util::read_i32_le(chunk)?.into());
59
60
61            rows.push(GMSurveyCurrentSurveyRow {
62                id,
63                gm_survey_id,
64            });
65        }
66
67        Ok(GMSurveyCurrentSurvey { rows, })
68    }
69
70    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
71        let header = DbcHeader {
72            record_count: self.rows.len() as u32,
73            field_count: 2,
74            record_size: 8,
75            string_block_size: 1,
76        };
77
78        b.write_all(&header.write_header())?;
79
80        for row in &self.rows {
81            // id: primary_key (GMSurveyCurrentSurvey) int32
82            b.write_all(&row.id.id.to_le_bytes())?;
83
84            // gm_survey_id: foreign_key (GMSurveySurveys) int32
85            b.write_all(&(row.gm_survey_id.id as i32).to_le_bytes())?;
86
87        }
88
89        b.write_all(&[0_u8])?;
90
91        Ok(())
92    }
93
94}
95
96impl Indexable for GMSurveyCurrentSurvey {
97    type PrimaryKey = GMSurveyCurrentSurveyKey;
98    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
99        let key = key.try_into().ok()?;
100        self.rows.iter().find(|a| a.id.id == key.id)
101    }
102
103    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
104        let key = key.try_into().ok()?;
105        self.rows.iter_mut().find(|a| a.id.id == key.id)
106    }
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
110pub struct GMSurveyCurrentSurveyKey {
111    pub id: i32
112}
113
114impl GMSurveyCurrentSurveyKey {
115    pub const fn new(id: i32) -> Self {
116        Self { id }
117    }
118
119}
120
121impl From<u8> for GMSurveyCurrentSurveyKey {
122    fn from(v: u8) -> Self {
123        Self::new(v.into())
124    }
125}
126
127impl From<u16> for GMSurveyCurrentSurveyKey {
128    fn from(v: u16) -> Self {
129        Self::new(v.into())
130    }
131}
132
133impl From<i8> for GMSurveyCurrentSurveyKey {
134    fn from(v: i8) -> Self {
135        Self::new(v.into())
136    }
137}
138
139impl From<i16> for GMSurveyCurrentSurveyKey {
140    fn from(v: i16) -> Self {
141        Self::new(v.into())
142    }
143}
144
145impl From<i32> for GMSurveyCurrentSurveyKey {
146    fn from(v: i32) -> Self {
147        Self::new(v)
148    }
149}
150
151impl TryFrom<u32> for GMSurveyCurrentSurveyKey {
152    type Error = u32;
153    fn try_from(v: u32) -> Result<Self, Self::Error> {
154        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
155    }
156}
157
158impl TryFrom<usize> for GMSurveyCurrentSurveyKey {
159    type Error = usize;
160    fn try_from(v: usize) -> Result<Self, Self::Error> {
161        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
162    }
163}
164
165impl TryFrom<u64> for GMSurveyCurrentSurveyKey {
166    type Error = u64;
167    fn try_from(v: u64) -> Result<Self, Self::Error> {
168        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
169    }
170}
171
172impl TryFrom<i64> for GMSurveyCurrentSurveyKey {
173    type Error = i64;
174    fn try_from(v: i64) -> Result<Self, Self::Error> {
175        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
176    }
177}
178
179impl TryFrom<isize> for GMSurveyCurrentSurveyKey {
180    type Error = isize;
181    fn try_from(v: isize) -> Result<Self, Self::Error> {
182        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
183    }
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
187pub struct GMSurveyCurrentSurveyRow {
188    pub id: GMSurveyCurrentSurveyKey,
189    pub gm_survey_id: GMSurveySurveysKey,
190}
191