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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use byteorder::{LittleEndian, WriteBytesExt};
use failure::{bail, Error};

use crate::{
    chunks::TOKEN_XML_TAG_START,
    model::{
        owned::{AttributeBuf, OwnedBuf},
        AttributeTrait, TagStart,
    },
};

/// Representation of a XML Tag start chunk
#[derive(Debug)]
pub struct XmlTagStartBuf {
    /// Attributes of the tag
    attributes: Vec<AttributeBuf>,
    /// Index of the string on the main string table
    name: u32,
    /// Index of the namespace
    namespace: u32,
    /// ¿Line of the xml?
    line: u32,
    /// Unknown field
    field1: u32,
    /// Unknown field
    field2: u32,
    /// ¿Class?
    class: u32,
}

impl XmlTagStartBuf {
    /// Creates a new `XmlTagStartBuf` with the given data
    pub fn new(line: u32, field1: u32, namespace: u32, name: u32, field2: u32, class: u32) -> Self {
        Self {
            attributes: Vec::new(),
            name,
            namespace,
            line,
            field1,
            field2,
            class,
        }
    }

    /// Adds a new attribute to the XML tag
    pub fn add_attribute(&mut self, attribute: AttributeBuf) {
        self.attributes.push(attribute);
    }
}

impl TagStart for XmlTagStartBuf {
    type Attribute = AttributeBuf;

    fn get_line(&self) -> Result<u32, Error> {
        Ok(self.line)
    }

    fn get_field1(&self) -> Result<u32, Error> {
        Ok(self.field1)
    }

    fn get_namespace_index(&self) -> Result<u32, Error> {
        Ok(self.namespace)
    }

    fn get_element_name_index(&self) -> Result<u32, Error> {
        Ok(self.name)
    }

    fn get_field2(&self) -> Result<u32, Error> {
        Ok(self.field2)
    }

    fn get_attributes_amount(&self) -> Result<u32, Error> {
        Ok(self.attributes.len() as u32)
    }

    fn get_class(&self) -> Result<u32, Error> {
        Ok(self.class)
    }

    fn get_attribute(&self, index: u32) -> Result<Self::Attribute, Error> {
        if let Some(attr) = self.attributes.get(index as usize) {
            Ok(*attr)
        } else {
            bail!("requested attribute out of bounds")
        }
    }
}

impl OwnedBuf for XmlTagStartBuf {
    fn get_token(&self) -> u16 {
        TOKEN_XML_TAG_START
    }

    fn get_body_data(&self) -> Result<Vec<u8>, Error> {
        let mut out = Vec::new();

        out.write_u32::<LittleEndian>(self.namespace)?;
        out.write_u32::<LittleEndian>(self.name)?;
        out.write_u32::<LittleEndian>(self.field2)?;
        out.write_u32::<LittleEndian>(self.attributes.len() as u32)?;
        out.write_u32::<LittleEndian>(self.class)?;

        for a in &self.attributes {
            out.write_u32::<LittleEndian>(a.get_namespace()?)?;
            out.write_u32::<LittleEndian>(a.get_name()?)?;
            out.write_u32::<LittleEndian>(a.get_class()?)?;
            out.write_u32::<LittleEndian>(a.get_resource_value()?)?;
            out.write_u32::<LittleEndian>(a.get_data()?)?;
        }

        Ok(out)
    }

    fn get_header(&self) -> Result<Vec<u8>, Error> {
        let mut out = Vec::new();

        out.write_u32::<LittleEndian>(self.line)?;
        out.write_u32::<LittleEndian>(self.field1)?;

        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::{AttributeTrait, OwnedBuf, TagStart, XmlTagStartBuf};
    use crate::{
        chunks::XmlTagStartWrapper, model::owned::AttributeBuf, raw_chunks::EXAMPLE_TAG_START,
        test::compare_chunks,
    };

    #[test]
    fn it_can_generate_a_chunk_with_the_given_data() {
        let attribute1 = AttributeBuf::new(1, 2, 3, 5, 6);
        let attribute2 = AttributeBuf::new(7, 8, 9, 11, 12);

        let mut tag_start = XmlTagStartBuf::new(10, 22, 0xFFFF_FFFF, 7, 5, 3);
        tag_start.add_attribute(attribute1);
        tag_start.add_attribute(attribute2);

        assert_eq!(10, tag_start.get_line().unwrap());
        assert_eq!(22, tag_start.get_field1().unwrap());
        assert_eq!(7, tag_start.get_element_name_index().unwrap());
        assert_eq!(5, tag_start.get_field2().unwrap());
        assert_eq!(3, tag_start.get_class().unwrap());
        assert_eq!(2, tag_start.get_attributes_amount().unwrap());
        assert_eq!(0xFFFF_FFFF, tag_start.get_namespace_index().unwrap());
        let first_attribute = tag_start.get_attribute(0).unwrap();
        assert_eq!(1, first_attribute.get_namespace().unwrap());
        let third_attribute = tag_start.get_attribute(2);
        assert!(third_attribute.is_err());
    }

    #[test]
    fn identity() {
        let raw = EXAMPLE_TAG_START;
        let wrapper = XmlTagStartWrapper::new(raw);

        let owned = wrapper.to_buffer().unwrap();
        let new_raw = owned.to_vec().unwrap();

        compare_chunks(&raw, &new_raw);
    }
}