Skip to main content

fory_core/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/// Configuration for Fory serialization.
19///
20/// This struct holds all the configuration options that control how Fory
21/// serializes and deserializes data. It is shared between the main `Fory`
22/// instance and the `WriteContext`/`ReadContext` to ensure consistent behavior.
23#[derive(Clone, Debug)]
24pub struct Config {
25    /// Whether compatible mode is enabled for schema evolution support.
26    pub compatible: bool,
27    /// Whether xlang mode is enabled.
28    pub xlang: bool,
29    /// Whether metadata sharing is enabled.
30    pub share_meta: bool,
31    /// Whether meta string compression is enabled.
32    pub compress_string: bool,
33    /// Whether UTF-8 string payloads are validated before constructing Rust strings.
34    pub check_string_read: bool,
35    /// Maximum depth for nested dynamic object serialization.
36    pub max_dyn_depth: u32,
37    /// Whether class version checking is enabled.
38    pub check_struct_version: bool,
39    /// Whether reference tracking is enabled.
40    /// When enabled, shared references and circular references are tracked
41    /// and preserved during serialization/deserialization.
42    pub track_ref: bool,
43    /// Approximate graph-memory gate for one root deserialization.
44    /// Mainly gates materialized collections, maps, arrays, structs, and objects.
45    /// Leaf values are gated by unread input bytes instead, and actual process
46    /// memory can be higher. Defaults to 128 MiB. Value must be a positive byte limit.
47    pub max_graph_memory_bytes: usize,
48    /// Root allowance for collection elements and map entries not backed by one
49    /// newly consumed input byte. Zero is a strict limit.
50    pub max_unbacked_container_items: usize,
51    /// Maximum accepted field count in one received struct TypeMeta.
52    pub max_type_fields: u32,
53    /// Maximum accepted body size in one received TypeMeta.
54    pub max_type_meta_bytes: u32,
55    /// Maximum accepted remote metadata versions for one logical type.
56    pub max_schema_versions_per_type: u32,
57    /// Maximum accepted average remote metadata versions across logical types.
58    pub max_average_schema_versions_per_type: u32,
59}
60
61impl Default for Config {
62    fn default() -> Self {
63        Config {
64            compatible: false,
65            xlang: true,
66            share_meta: false,
67            compress_string: false,
68            check_string_read: true,
69            max_dyn_depth: 5,
70            check_struct_version: false,
71            track_ref: false,
72            max_graph_memory_bytes: 128 * 1024 * 1024,
73            max_unbacked_container_items: 8192,
74            max_type_fields: 512,
75            max_type_meta_bytes: 4096,
76            max_schema_versions_per_type: 10,
77            max_average_schema_versions_per_type: 3,
78        }
79    }
80}
81
82impl Config {
83    /// Creates a new Config with default values.
84    pub fn new() -> Self {
85        Self::default()
86    }
87
88    /// Check if compatible mode is enabled.
89    #[inline(always)]
90    pub fn is_compatible(&self) -> bool {
91        self.compatible
92    }
93
94    /// Check if xlang mode is enabled.
95    #[inline(always)]
96    pub fn is_xlang(&self) -> bool {
97        self.xlang
98    }
99
100    /// Check if meta sharing is enabled.
101    #[inline(always)]
102    pub fn is_share_meta(&self) -> bool {
103        self.share_meta
104    }
105
106    /// Check if string compression is enabled.
107    #[inline(always)]
108    pub fn is_compress_string(&self) -> bool {
109        self.compress_string
110    }
111
112    /// Check if UTF-8 string payload validation is enabled.
113    #[inline(always)]
114    pub fn is_check_string_read(&self) -> bool {
115        self.check_string_read
116    }
117
118    /// Get maximum dynamic depth.
119    #[inline(always)]
120    pub fn max_dyn_depth(&self) -> u32 {
121        self.max_dyn_depth
122    }
123
124    /// Check if class version checking is enabled.
125    #[inline(always)]
126    pub fn is_check_struct_version(&self) -> bool {
127        self.check_struct_version
128    }
129
130    /// Check if reference tracking is enabled.
131    #[inline(always)]
132    pub fn is_track_ref(&self) -> bool {
133        self.track_ref
134    }
135
136    /// Get the approximate graph-memory gate per root deserialization.
137    #[inline(always)]
138    pub fn max_graph_memory_bytes(&self) -> usize {
139        self.max_graph_memory_bytes
140    }
141
142    /// Get the root allowance for container items not backed by input bytes.
143    #[inline(always)]
144    pub fn max_unbacked_container_items(&self) -> usize {
145        self.max_unbacked_container_items
146    }
147
148    /// Get maximum accepted field count in one received struct TypeMeta.
149    #[inline(always)]
150    pub fn max_type_fields(&self) -> usize {
151        self.max_type_fields as usize
152    }
153
154    /// Get maximum accepted body size in one received TypeMeta.
155    #[inline(always)]
156    pub fn max_type_meta_bytes(&self) -> usize {
157        self.max_type_meta_bytes as usize
158    }
159
160    /// Get maximum accepted remote metadata versions for one logical type.
161    #[inline(always)]
162    pub fn max_schema_versions_per_type(&self) -> usize {
163        self.max_schema_versions_per_type as usize
164    }
165
166    /// Get maximum accepted average remote metadata versions across logical types.
167    #[inline(always)]
168    pub fn max_average_schema_versions_per_type(&self) -> usize {
169        self.max_average_schema_versions_per_type as usize
170    }
171}