godot_binary_serialization/decoder/
float.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
54
55
56
57
58
59
60
61
62
63
use anyhow::anyhow;
use byteorder::{ByteOrder, LittleEndian};

use crate::types::{primitive::GodotFloat, SerializeFlag, TYPE_PADDING};

use super::Decoder;

impl Decoder {
    /// Decodes a bytes into a Godot float
    pub fn decode_float(bytes: &[u8], flag: &SerializeFlag) -> anyhow::Result<GodotFloat> {
        Self::decode_raw_float(bytes, 4, flag)
    }

    /// Uses a serialization flag and the offset in bytes to decode bytes into a Godot float
    pub fn decode_raw_float(
        bytes: &[u8],
        offset: usize,
        flag: &SerializeFlag,
    ) -> anyhow::Result<GodotFloat> {
        let mut length = 4;

        if flag == &SerializeFlag::Bit64 {
            length = 8;
        }

        if bytes.len() < TYPE_PADDING as usize + length {
            return Err(anyhow!(
                "Byte slice too short to decode float with flag {flag:?}"
            ));
        }

        if flag == &SerializeFlag::Bit64 {
            return Ok(GodotFloat {
                value: LittleEndian::read_f64(&bytes[offset..offset + length]),
                byte_size: TYPE_PADDING as usize + length,
            });
        }

        Ok(GodotFloat {
            value: LittleEndian::read_f32(&bytes[offset..offset + length]) as f64,
            byte_size: TYPE_PADDING as usize + length,
        })
    }
}

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

    #[test]
    fn decode_float64() {
        let bytes: &[u8] = &[3, 0, 1, 0, 123, 20, 174, 71, 225, 122, 228, 63];
        let (_type, flag) = Decoder::get_type_and_flags(bytes).unwrap();
        let float = Decoder::decode_float(bytes, &flag).unwrap();
        let value = 0.64;

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