delta-pack 0.2.0

Binary serialization with delta compression for real-time state synchronization
Documentation
// Auto-generated by DeltaPack - do not edit
#![allow(dead_code, unused_imports, clippy::all)]

use delta_pack::IndexMap;
use delta_pack::{Decoder, Encoder};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Primitives {
    #[serde(rename = "stringField")]
    pub string_field: String,
    #[serde(rename = "signedIntField")]
    pub signed_int_field: i64,
    #[serde(rename = "unsignedIntField")]
    pub unsigned_int_field: u64,
    #[serde(rename = "boundedIntField")]
    pub bounded_int_field: i64,
    #[serde(rename = "floatField")]
    pub float_field: f32,
    #[serde(rename = "booleanField")]
    pub boolean_field: bool,
}

impl Default for Primitives {
    fn default() -> Self {
        Self {
            string_field: String::new(),
            signed_int_field: 0,
            unsigned_int_field: 0,
            bounded_int_field: 0,
            float_field: 0.0,
            boolean_field: false,
        }
    }
}

impl Primitives {
    pub fn equals(&self, other: &Self) -> bool {
        self.string_field == other.string_field
            && self.signed_int_field == other.signed_int_field
            && self.unsigned_int_field == other.unsigned_int_field
            && self.bounded_int_field == other.bounded_int_field
            && delta_pack::equals_float(self.float_field, other.float_field)
            && self.boolean_field == other.boolean_field
    }

    pub fn encode(&self) -> Vec<u8> {
        Encoder::encode(|encoder| {
            self.encode_into(encoder);
            encoder.finish()
        })
    }

    pub fn encode_into(&self, encoder: &mut Encoder) {
        encoder.push_string(&self.string_field);
        encoder.push_int(self.signed_int_field);
        encoder.push_uint(self.unsigned_int_field);
        encoder.push_enum((self.bounded_int_field - (-10)) as u32, 5);
        encoder.push_float(self.float_field);
        encoder.push_boolean(self.boolean_field);
    }

    pub fn encode_diff(a: &Self, b: &Self) -> Vec<u8> {
        Encoder::encode(|encoder| {
            encoder.push_object_diff(
                a,
                b,
                |x, y| x.equals(y),
                |enc| {
                    Self::encode_diff_into(a, b, enc);
                },
            );
            encoder.finish()
        })
    }

    pub fn encode_diff_into(a: &Self, b: &Self, encoder: &mut Encoder) {
        encoder.push_field_diff(
            &a.string_field,
            &b.string_field,
            |x, y| x == y,
            |enc, a, b| enc.push_string_diff(a, b),
        );
        encoder.push_field_diff(
            &a.signed_int_field,
            &b.signed_int_field,
            |x, y| x == y,
            |enc, &a, &b| enc.push_int_diff(a, b),
        );
        encoder.push_field_diff(
            &a.unsigned_int_field,
            &b.unsigned_int_field,
            |x, y| x == y,
            |enc, &a, &b| enc.push_uint_diff(a, b),
        );
        encoder.push_field_diff(
            &a.bounded_int_field,
            &b.bounded_int_field,
            |x, y| x == y,
            |enc, &a, &b| enc.push_enum_diff((a - (-10)) as u32, (b - (-10)) as u32, 5),
        );
        encoder.push_field_diff(
            &a.float_field,
            &b.float_field,
            |x, y| delta_pack::equals_float(*x, *y),
            |enc, &a, &b| enc.push_float_diff(a, b),
        );
        encoder.push_boolean_diff(a.boolean_field, b.boolean_field);
    }

    pub fn decode(buf: &[u8]) -> Self {
        Decoder::decode(buf, |decoder| Self::decode_from(decoder))
    }

    pub fn decode_from(decoder: &mut Decoder) -> Self {
        Self {
            string_field: decoder.next_string(),
            signed_int_field: decoder.next_int(),
            unsigned_int_field: decoder.next_uint(),
            bounded_int_field: decoder.next_enum(5) as i64 + (-10),
            float_field: decoder.next_float(),
            boolean_field: decoder.next_boolean(),
        }
    }

    pub fn decode_diff(obj: &Self, diff: &[u8]) -> Self {
        Decoder::decode(diff, |decoder| {
            decoder.next_object_diff(obj, |dec| Self::decode_diff_from(obj, dec))
        })
    }

    pub fn decode_diff_from(obj: &Self, decoder: &mut Decoder) -> Self {
        Self {
            string_field: decoder
                .next_field_diff(&obj.string_field, |dec, a| dec.next_string_diff(a)),
            signed_int_field: decoder
                .next_field_diff(&obj.signed_int_field, |dec, &a| dec.next_int_diff(a)),
            unsigned_int_field: decoder
                .next_field_diff(&obj.unsigned_int_field, |dec, &a| dec.next_uint_diff(a)),
            bounded_int_field: decoder.next_field_diff(&obj.bounded_int_field, |dec, &a| {
                dec.next_enum_diff((a - (-10)) as u32, 5) as i64 + (-10)
            }),
            float_field: decoder
                .next_field_diff(&obj.float_field, |dec, &a| dec.next_float_diff(a)),
            boolean_field: decoder.next_boolean_diff(obj.boolean_field),
        }
    }
}