godot_binary_serialization/decoder/
float.rs1use anyhow::anyhow;
2use byteorder::{ByteOrder, LittleEndian};
3
4use crate::types::{primitive::GodotFloat, SerializeFlag, TYPE_PADDING};
5
6use super::Decoder;
7
8impl Decoder {
9 pub fn decode_float(bytes: &[u8], flag: &SerializeFlag) -> anyhow::Result<GodotFloat> {
11 Self::decode_raw_float(bytes, 4, flag)
12 }
13
14 pub fn decode_raw_float(
16 bytes: &[u8],
17 offset: usize,
18 flag: &SerializeFlag,
19 ) -> anyhow::Result<GodotFloat> {
20 let mut length = 4;
21
22 if flag == &SerializeFlag::Bit64 {
23 length = 8;
24 }
25
26 if bytes.len() < TYPE_PADDING as usize + length {
27 return Err(anyhow!(
28 "Byte slice too short to decode float with flag {flag:?}"
29 ));
30 }
31
32 if flag == &SerializeFlag::Bit64 {
33 return Ok(GodotFloat {
34 value: LittleEndian::read_f64(&bytes[offset..offset + length]),
35 byte_size: TYPE_PADDING as usize + length,
36 });
37 }
38
39 Ok(GodotFloat {
40 value: LittleEndian::read_f32(&bytes[offset..offset + length]) as f64,
41 byte_size: TYPE_PADDING as usize + length,
42 })
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use crate::decoder::Decoder;
49
50 #[test]
51 fn decode_float64() {
52 let bytes: &[u8] = &[3, 0, 1, 0, 123, 20, 174, 71, 225, 122, 228, 63];
53 let (_type, flag) = Decoder::get_type_and_flags(bytes).unwrap();
54 let float = Decoder::decode_float(bytes, &flag).unwrap();
55 let value = 0.64;
56
57 assert_eq!(
58 float.value, value,
59 "Expected value of {} but got {} instead",
60 value, float.value
61 );
62 }
63}