godot_binary_serialization/decoder/
string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use anyhow::anyhow;
use byteorder::{ByteOrder, LittleEndian};

use crate::types::primitive::GodotString;

use super::Decoder;

impl Decoder {
    /// Decodes bytes into a Godot string. This will fail if the bytes do not match Godot's
    /// serialization rules
    pub fn decode_string(bytes: &[u8]) -> anyhow::Result<GodotString> {
        if bytes.len() < 8 {
            return Err(anyhow!("Not enough bytes for a string"));
        }

        let length = LittleEndian::read_u32(&bytes[4..8]) as usize;
        // Pad 4 bytes because godot
        let pad = (4 - (length % 4)) % 4;

        let total_length = 8 + length + pad;
        if bytes.len() < total_length {
            return Err(anyhow!("Amount of bytes does not match string length"));
        }

        let string = String::from_utf8(bytes[8..8 + length].to_vec())?;

        Ok(GodotString {
            value: string,
            byte_size: total_length,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::decoder::Decoder;

    #[test]
    fn decode_string() {
        let bytes: &[u8] = &[
            4, 0, 0, 0, 10, 0, 0, 0, 98, 97, 110, 97, 110, 97, 66, 108, 111, 120, 0, 0,
        ];
        let (_type, _flag) = Decoder::get_type_and_flags(bytes).unwrap();
        let string = Decoder::decode_string(bytes).unwrap();
        let value = "bananaBlox";

        assert_eq!(
            string.value, value,
            "Expected value of {} but got {} instead",
            value, string.value
        )
    }
}