umya_spreadsheet/structs/
to_marker.rs

1// to
2use crate::writer::driver::*;
3use quick_xml::events::{BytesStart, Event};
4use quick_xml::Reader;
5use quick_xml::Writer;
6use std::io::Cursor;
7
8#[derive(Clone, Default, Debug)]
9pub struct ToMarker {
10    col: usize,
11    col_off: usize,
12    row: usize,
13    row_off: usize,
14}
15impl ToMarker {
16    #[inline]
17    pub fn get_col(&self) -> &usize {
18        &self.col
19    }
20
21    #[inline]
22    pub fn set_col(&mut self, value: usize) -> &mut ToMarker {
23        self.col = value;
24        self
25    }
26
27    #[inline]
28    pub fn get_col_off(&self) -> &usize {
29        &self.col_off
30    }
31
32    #[inline]
33    pub fn set_col_off(&mut self, value: usize) -> &mut ToMarker {
34        self.col_off = value;
35        self
36    }
37
38    #[inline]
39    pub fn get_row(&self) -> &usize {
40        &self.row
41    }
42
43    #[inline]
44    pub fn set_row(&mut self, value: usize) -> &mut ToMarker {
45        self.row = value;
46        self
47    }
48
49    #[inline]
50    pub fn get_row_off(&self) -> &usize {
51        &self.row_off
52    }
53
54    #[inline]
55    pub fn set_row_off(&mut self, value: usize) -> &mut ToMarker {
56        self.row_off = value;
57        self
58    }
59
60    #[inline]
61    pub(crate) fn _adjustment_insert_row(&mut self, num_rows: &usize) {
62        self.row += num_rows;
63    }
64
65    #[inline]
66    pub(crate) fn _adjustment_insert_column(&mut self, num_cols: &usize) {
67        self.col += num_cols;
68    }
69
70    #[inline]
71    pub(crate) fn _adjustment_remove_row(&mut self, num_rows: &usize) {
72        self.row = self.row.saturating_sub(*num_rows).max(1);
73    }
74
75    #[inline]
76    pub(crate) fn _adjustment_remove_column(&mut self, num_cols: &usize) {
77        self.col = self.col.saturating_sub(*num_cols).max(1);
78    }
79
80    pub(crate) fn set_attributes<R: std::io::BufRead>(
81        &mut self,
82        reader: &mut Reader<R>,
83        _e: &BytesStart,
84    ) {
85        let mut string_value: String = String::new();
86        let mut buf = Vec::new();
87        loop {
88            match reader.read_event_into(&mut buf) {
89                Ok(Event::Text(e)) => string_value = e.unescape().unwrap().to_string(),
90                Ok(Event::End(ref e)) => match e.name().0 {
91                    b"xdr:col" => {
92                        self.col = string_value.parse::<usize>().unwrap();
93                    }
94                    b"xdr:colOff" => {
95                        self.col_off = string_value.parse::<usize>().unwrap();
96                    }
97                    b"xdr:row" => {
98                        self.row = string_value.parse::<usize>().unwrap();
99                    }
100                    b"xdr:rowOff" => {
101                        self.row_off = string_value.parse::<usize>().unwrap();
102                    }
103                    b"to" => return,
104                    _ => (),
105                },
106                Ok(Event::Eof) => panic!("Error: Could not find {} end element", "to"),
107                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
108                _ => (),
109            }
110            buf.clear();
111        }
112    }
113
114    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
115        // to
116        write_start_tag(writer, "to", vec![], false);
117
118        // xdr:col
119        write_start_tag(writer, "xdr:col", vec![], false);
120        write_text_node(writer, &self.col.to_string());
121        write_end_tag(writer, "xdr:col");
122
123        // xdr:colOff
124        write_start_tag(writer, "xdr:colOff", vec![], false);
125        write_text_node(writer, &self.col_off.to_string());
126        write_end_tag(writer, "xdr:colOff");
127
128        // xdr:row
129        write_start_tag(writer, "xdr:row", vec![], false);
130        write_text_node(writer, &self.row.to_string());
131        write_end_tag(writer, "xdr:row");
132
133        // xdr:rowOff
134        write_start_tag(writer, "xdr:rowOff", vec![], false);
135        write_text_node(writer, &self.row_off.to_string());
136        write_end_tag(writer, "xdr:rowOff");
137
138        write_end_tag(writer, "to");
139    }
140}