binary_codec/
variable.rs

1use crate::{DeserializationError, SerializationError, SerializerConfig, utils::{ensure_size, get_read_size, slice, write_size}};
2
3pub fn read_string(bytes: &[u8], size_key: Option<&str>, config: &mut SerializerConfig) -> Result<String, DeserializationError> {
4    let len = get_read_size(bytes, size_key, config)?;
5    config.reset_bits(true);
6    let slice = slice(config, bytes, len, true)?;
7    let string = String::from_utf8(slice.to_vec()).expect("Not valid UTF-8 bytes to create string");
8    
9    Ok(string)
10}
11
12pub fn write_string(value: &str, size_key: Option<&str>, buffer: &mut Vec<u8>, config: &mut SerializerConfig) -> Result<(), SerializationError> {
13    config.reset_bits(false);
14    write_size(value.len(), size_key, buffer, config)?; 
15
16    buffer.extend_from_slice(&value.as_bytes());
17    config.pos += value.len();
18    Ok(())
19}
20
21pub fn read_object<T>(bytes: &[u8], size_key: Option<&str>, config: &mut SerializerConfig) -> Result<T, DeserializationError>  
22    where T : crate::BinaryDeserializer 
23{
24    let len = get_read_size(bytes, size_key, config)?;
25
26    // If exact size of buffer is available, don't slice
27    if ensure_size(config, bytes, len)? {
28        T::from_bytes(bytes, Some(config))
29    } else {
30        // Create an isolated slice like we do for a String, but with its own config
31        config.reset_bits(true);
32        let mut temp_config = config.clone();
33        temp_config.reset();
34
35        let slice = slice(config, bytes, len, true)?;
36        T::from_bytes(&slice, Some(&mut temp_config))
37    }
38}
39
40pub fn write_object<T>(value: &T, size_key: Option<&str>, buffer: &mut Vec<u8>, config: &mut SerializerConfig) -> Result<(), SerializationError>  
41    where T : crate::BinarySerializer 
42{
43    // If length name is provided, we need to ensure the length matches
44    // So we write it to a different buffer
45    if size_key.is_some() {
46        let mut temp_buffer = Vec::new();
47        config.reset_bits(false);
48        value.write_bytes(&mut temp_buffer, Some(config))?;
49        write_size(temp_buffer.len(), size_key, buffer, config)?;
50        buffer.extend_from_slice(&temp_buffer);
51        Ok(())
52    } else {
53        value.write_bytes(buffer, Some(config))
54    }
55}