/src/bson-rust/src/raw/document_buf/raw_writer.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::{raw::write_cstring, RawBsonRef}; |
2 | | |
3 | | pub(super) struct RawWriter<'a> { |
4 | | data: &'a mut Vec<u8>, |
5 | | } |
6 | | |
7 | | impl<'a> RawWriter<'a> { |
8 | 253k | pub(super) fn new(data: &'a mut Vec<u8>) -> Self { |
9 | 253k | Self { data } |
10 | 253k | } |
11 | | |
12 | 253k | pub(super) fn append(&mut self, key: &str, value: RawBsonRef) -> crate::error::Result<()> { |
13 | 253k | let original_len = self.data.len(); |
14 | 253k | self.data[original_len - 1] = value.element_type() as u8; |
15 | 253k | |
16 | 253k | write_cstring(self.data, key)?; |
17 | 253k | value.append_to(self.data)?; |
18 | | |
19 | | // append trailing null byte |
20 | 253k | self.data.push(0); |
21 | 253k | // update length |
22 | 253k | let new_len = (self.data.len() as i32).to_le_bytes(); |
23 | 253k | self.data[0..4].copy_from_slice(&new_len); |
24 | 253k | |
25 | 253k | Ok(()) |
26 | 253k | } |
27 | | } |