Skip to main content

memlink_protocol/
config.rs

1//! Serialization configuration.
2//!
3//! Defines SerializerConfig with max_size, reject_unknown_fields,
4//! and encoding options for controlling serialization behavior.
5
6use crate::magic::MAX_PAYLOAD_SIZE;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct SerializerConfig {
10    pub max_size: usize,
11    pub reject_unknown_fields: bool,
12    pub use_variable_length_encoding: bool,
13}
14
15impl Default for SerializerConfig {
16    fn default() -> Self {
17        Self::msgpack_default()
18    }
19}
20
21impl SerializerConfig {
22    pub const fn new(
23        max_size: usize,
24        reject_unknown_fields: bool,
25        use_variable_length_encoding: bool,
26    ) -> Self {
27        Self {
28            max_size,
29            reject_unknown_fields,
30            use_variable_length_encoding,
31        }
32    }
33
34    pub const fn msgpack_default() -> Self {
35        Self {
36            max_size: MAX_PAYLOAD_SIZE,
37            reject_unknown_fields: false,
38            use_variable_length_encoding: true,
39        }
40    }
41
42    pub const fn with_max_size(mut self, max_size: usize) -> Self {
43        self.max_size = max_size;
44        self
45    }
46
47    pub const fn with_reject_unknown_fields(mut self, reject: bool) -> Self {
48        self.reject_unknown_fields = reject;
49        self
50    }
51
52    pub const fn with_variable_length_encoding(mut self, use_varint: bool) -> Self {
53        self.use_variable_length_encoding = use_varint;
54        self
55    }
56
57    pub fn validate_size(&self, size: usize) -> crate::error::Result<()> {
58        if size > self.max_size {
59            Err(crate::error::ProtocolError::PayloadTooLarge(size, self.max_size))
60        } else {
61            Ok(())
62        }
63    }
64}
65
66#[derive(Debug)]
67pub struct FlatBufferSerializer {
68    _private: (),
69}