godot_binary_serialization/encoder/
mod.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
use anyhow::anyhow;

use crate::types::{
    primitive::{GodotBool, GodotFloat, GodotInteger, GodotString},
    structures::{GodotDictionary, GodotVector2, GodotVector3},
    variant::{AsVariant, GodotVariant},
};

pub mod dictionary;
pub mod float;
pub mod int;
pub mod string;
pub mod vector;
pub mod bool;

/// Encodes a variant from its type into bytes
pub struct Encoder;

impl Encoder {
    /// Takes in a Godot variant and determines how to encode it based on its type
    pub fn encode_variant(variant: &dyn GodotVariant) -> anyhow::Result<Vec<u8>> {
        if let Some(bool) = variant.as_var::<GodotBool>() {
            return Self::encode_bool(bool);
        }

        if let Some(integer) = variant.as_var::<GodotInteger>() {
            return Self::encode_int(integer);
        }

        if let Some(float) = variant.as_var::<GodotFloat>() {
            return Self::encode_float(float);
        }

        if let Some(string) = variant.as_var::<GodotString>() {
            return Self::encode_string(string);
        }

        if let Some(vector2) = variant.as_var::<GodotVector2>() {
            return Self::encode_vector2(vector2);
        }

        if let Some(vector3) = variant.as_var::<GodotVector3>() {
            return Self::encode_vector3(vector3);
        }

        if let Some(dictionary) = variant.as_var::<GodotDictionary>() {
            return Self::encode_dictionary(dictionary);
        }

        Err(anyhow!(
            "Variant of {:?} is not supported by the encoder",
            variant
        ))
    }
}