abxml/model/owned/xml/
namespace_end.rs

1use std::rc::Rc;
2
3use byteorder::{LittleEndian, WriteBytesExt};
4use failure::Error;
5
6use crate::{
7    chunks::TOKEN_XML_END_NAMESPACE,
8    model::{
9        owned::OwnedBuf,
10        {NamespaceEnd, StringTable},
11    },
12};
13
14#[derive(Debug, Copy, Clone)]
15pub struct XmlNamespaceEndBuf {
16    line: u32,
17    prefix_index: u32,
18    namespace_index: u32,
19}
20
21impl XmlNamespaceEndBuf {
22    pub fn new(line: u32, prefix_index: u32, namespace_index: u32) -> Self {
23        Self {
24            line,
25            prefix_index,
26            namespace_index,
27        }
28    }
29}
30
31impl NamespaceEnd for XmlNamespaceEndBuf {
32    fn get_line(&self) -> Result<u32, Error> {
33        Ok(self.line)
34    }
35
36    fn get_prefix<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>, Error> {
37        let string = string_table.get_string(self.prefix_index)?;
38
39        Ok(string)
40    }
41
42    fn get_namespace<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>, Error> {
43        let string = string_table.get_string(self.namespace_index)?;
44
45        Ok(string)
46    }
47}
48
49impl OwnedBuf for XmlNamespaceEndBuf {
50    fn get_token(&self) -> u16 {
51        TOKEN_XML_END_NAMESPACE
52    }
53
54    fn get_body_data(&self) -> Result<Vec<u8>, Error> {
55        let mut out = Vec::new();
56
57        out.write_u32::<LittleEndian>(self.prefix_index)?;
58        out.write_u32::<LittleEndian>(self.namespace_index)?;
59
60        Ok(out)
61    }
62
63    fn get_header(&self) -> Result<Vec<u8>, Error> {
64        let mut out = Vec::new();
65
66        out.write_u32::<LittleEndian>(self.line)?;
67        out.write_u32::<LittleEndian>(0xFFFF_FFFF)?;
68
69        Ok(out)
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::{NamespaceEnd, OwnedBuf, XmlNamespaceEndBuf};
76    use crate::{
77        chunks::XmlNamespaceEndWrapper, raw_chunks::EXAMPLE_NAMESPACE_END, test::compare_chunks,
78    };
79
80    #[test]
81    fn it_can_generate_a_chunk_with_the_given_data() {
82        let namespace_end = XmlNamespaceEndBuf::new(99, 1001, 2203);
83
84        assert_eq!(99, namespace_end.get_line().unwrap());
85    }
86
87    #[test]
88    fn identity() {
89        let wrapper = XmlNamespaceEndWrapper::new(EXAMPLE_NAMESPACE_END);
90
91        let owned = wrapper.to_buffer().unwrap();
92        let new_raw = owned.to_vec().unwrap();
93
94        compare_chunks(&new_raw, &EXAMPLE_NAMESPACE_END);
95    }
96}