|
| 1 | +use std::{ |
| 2 | + collections::VecDeque, |
| 3 | + io::{Seek, Write}, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::Result; |
| 7 | +use zip::{write::SimpleFileOptions, ZipWriter}; |
| 8 | + |
| 9 | +pub struct TypedSheet<'a, W: Write + Seek> { |
| 10 | + pub sheet_buf: &'a mut ZipWriter<W>, |
| 11 | + pub _name: String, |
| 12 | + // pub id: u16, |
| 13 | + // pub is_closed: bool, |
| 14 | + col_num_to_letter: Vec<Vec<u8>>, |
| 15 | + current_row_num: u32, |
| 16 | +} |
| 17 | + |
| 18 | +impl<'a, W: Write + Seek> TypedSheet<'a, W> { |
| 19 | + pub fn new(name: String, id: u16, writer: &'a mut ZipWriter<W>) -> Self { |
| 20 | + let options = SimpleFileOptions::default() |
| 21 | + .compression_method(zip::CompressionMethod::Deflated) |
| 22 | + .compression_level(Some(1)) |
| 23 | + .large_file(true); |
| 24 | + |
| 25 | + writer |
| 26 | + .start_file(format!("xl/worksheets/sheet{}.xml", id), options) |
| 27 | + .ok(); |
| 28 | + |
| 29 | + // Writes Sheet Header |
| 30 | + writer.write(b"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">\n<sheetData>\n").ok(); |
| 31 | + |
| 32 | + TypedSheet { |
| 33 | + sheet_buf: writer, |
| 34 | + // id, |
| 35 | + _name: name, |
| 36 | + // is_closed: false, |
| 37 | + col_num_to_letter: Vec::with_capacity(64), |
| 38 | + current_row_num: 0, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // TOOD: Use ShortVec over Vec for cell ID |
| 43 | + pub fn write_row(&mut self, data: Vec<&[u8]>, types: &Vec<&str>) -> Result<()> { |
| 44 | + self.current_row_num += 1; |
| 45 | + |
| 46 | + let mut final_vec = Vec::with_capacity(512 * data.len()); |
| 47 | + |
| 48 | + // TODO: Proper Error Handling |
| 49 | + let (row_in_chars_arr, digits) = self.num_to_bytes(self.current_row_num); |
| 50 | + |
| 51 | + final_vec.write(b"<row r=\"")?; |
| 52 | + final_vec.write(&row_in_chars_arr[9 - digits..])?; |
| 53 | + final_vec.write(b"\">")?; |
| 54 | + |
| 55 | + let mut col = 0; |
| 56 | + if self.current_row_num == 1 { |
| 57 | + for datum in data { |
| 58 | + let (ref_id, pos) = self.ref_id(col, (row_in_chars_arr, digits))?; |
| 59 | + |
| 60 | + final_vec.write(b"<c r=\"")?; |
| 61 | + final_vec.write(&ref_id.as_slice()[0..pos])?; |
| 62 | + final_vec.write(b"\" t=\"str\"><v>")?; |
| 63 | + |
| 64 | + let (mut chars, chars_pos) = self.escape_in_place(datum); |
| 65 | + let mut current_pos = 0; |
| 66 | + for char_pos in chars_pos { |
| 67 | + final_vec.write(&datum[current_pos..char_pos])?; |
| 68 | + final_vec.write(chars.pop_front().unwrap())?; |
| 69 | + current_pos = char_pos + 1; |
| 70 | + } |
| 71 | + |
| 72 | + final_vec.write(&datum[current_pos..])?; |
| 73 | + final_vec.write(b"</v></c>")?; |
| 74 | + |
| 75 | + col += 1; |
| 76 | + } |
| 77 | + } else { |
| 78 | + for datum in data { |
| 79 | + let (ref_id, pos) = self.ref_id(col, (row_in_chars_arr, digits))?; |
| 80 | + |
| 81 | + let col_type = *types.get(col).unwrap_or(&"s"); |
| 82 | + |
| 83 | + final_vec.write(b"<c r=\"")?; |
| 84 | + final_vec.write(&ref_id.as_slice()[0..pos])?; |
| 85 | + final_vec.write(b"\" t=\"")?; |
| 86 | + final_vec.write(col_type.as_bytes())?; |
| 87 | + final_vec.write(b"\"><v>")?; |
| 88 | + |
| 89 | + let (mut chars, chars_pos) = self.escape_in_place(datum); |
| 90 | + let mut current_pos = 0; |
| 91 | + for char_pos in chars_pos { |
| 92 | + final_vec.write(&datum[current_pos..char_pos])?; |
| 93 | + final_vec.write(chars.pop_front().unwrap())?; |
| 94 | + current_pos = char_pos + 1; |
| 95 | + } |
| 96 | + |
| 97 | + final_vec.write(&datum[current_pos..])?; |
| 98 | + final_vec.write(b"</v></c>")?; |
| 99 | + |
| 100 | + col += 1; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + final_vec.write(b"</row>")?; |
| 105 | + |
| 106 | + self.sheet_buf.write(&final_vec)?; |
| 107 | + |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | + |
| 111 | + fn escape_in_place(&self, bytes: &[u8]) -> (VecDeque<&[u8]>, VecDeque<usize>) { |
| 112 | + let mut special_chars: VecDeque<&[u8]> = VecDeque::new(); |
| 113 | + let mut special_char_pos: VecDeque<usize> = VecDeque::new(); |
| 114 | + let len = bytes.len(); |
| 115 | + for x in 0..len { |
| 116 | + let _ = match bytes[x] { |
| 117 | + b'<' => { |
| 118 | + special_chars.push_back(b"<".as_slice()); |
| 119 | + special_char_pos.push_back(x); |
| 120 | + } |
| 121 | + b'>' => { |
| 122 | + special_chars.push_back(b">".as_slice()); |
| 123 | + special_char_pos.push_back(x); |
| 124 | + } |
| 125 | + b'\'' => { |
| 126 | + special_chars.push_back(b"'".as_slice()); |
| 127 | + special_char_pos.push_back(x); |
| 128 | + } |
| 129 | + b'&' => { |
| 130 | + special_chars.push_back(b"&".as_slice()); |
| 131 | + special_char_pos.push_back(x); |
| 132 | + } |
| 133 | + b'"' => { |
| 134 | + special_chars.push_back(b""".as_slice()); |
| 135 | + special_char_pos.push_back(x); |
| 136 | + } |
| 137 | + _ => (), |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + (special_chars, special_char_pos) |
| 142 | + } |
| 143 | + |
| 144 | + pub fn close(&mut self) -> Result<()> { |
| 145 | + self.sheet_buf.write(b"\n</sheetData>\n</worksheet>\n")?; |
| 146 | + Ok(()) |
| 147 | + } |
| 148 | + |
| 149 | + fn num_to_bytes(&self, n: u32) -> ([u8; 9], usize) { |
| 150 | + // Convert from number to string manually |
| 151 | + let mut row_in_chars_arr: [u8; 9] = [0; 9]; |
| 152 | + let mut row = n; |
| 153 | + let mut char_pos = 8; |
| 154 | + let mut digits = 0; |
| 155 | + while row > 0 { |
| 156 | + row_in_chars_arr[char_pos] = b'0' + (row % 10) as u8; |
| 157 | + row = row / 10; |
| 158 | + char_pos -= 1; |
| 159 | + digits += 1; |
| 160 | + } |
| 161 | + |
| 162 | + (row_in_chars_arr, digits) |
| 163 | + } |
| 164 | + |
| 165 | + fn ref_id(&mut self, col: usize, row: ([u8; 9], usize)) -> Result<([u8; 12], usize)> { |
| 166 | + let mut final_arr: [u8; 12] = [0; 12]; |
| 167 | + let letter = self.col_to_letter(col); |
| 168 | + |
| 169 | + let mut pos: usize = 0; |
| 170 | + for c in letter { |
| 171 | + final_arr[pos] = *c; |
| 172 | + pos += 1; |
| 173 | + } |
| 174 | + |
| 175 | + let (row_in_chars_arr, digits) = row; |
| 176 | + |
| 177 | + for i in 0..digits { |
| 178 | + final_arr[pos] = row_in_chars_arr[(8 - digits) + i + 1]; |
| 179 | + pos += 1; |
| 180 | + } |
| 181 | + |
| 182 | + Ok((final_arr, pos)) |
| 183 | + } |
| 184 | + |
| 185 | + fn col_to_letter(&mut self, col: usize) -> &[u8] { |
| 186 | + if self.col_num_to_letter.len() < col + 1 as usize { |
| 187 | + let mut result = Vec::with_capacity(2); |
| 188 | + let mut col = col as i16; |
| 189 | + |
| 190 | + loop { |
| 191 | + result.push(b'A' + (col % 26) as u8); |
| 192 | + col = col / 26 - 1; |
| 193 | + if col < 0 { |
| 194 | + break; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + result.reverse(); |
| 199 | + self.col_num_to_letter.push(result); |
| 200 | + } |
| 201 | + |
| 202 | + &self.col_num_to_letter[col] |
| 203 | + } |
| 204 | +} |
0 commit comments