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 /// Maximum accepted field count in one received struct TypeMeta.
49 pub max_type_fields: u32,
50 /// Maximum accepted body size in one received TypeMeta.
51 pub max_type_meta_bytes: u32,
52 /// Maximum accepted remote metadata versions for one logical type.
53 pub max_schema_versions_per_type: u32,
54 /// Maximum accepted average remote metadata versions across logical types.
55 pub max_average_schema_versions_per_type: u32,
56}
57
58impl Default for Config {
59 fn default() -> Self {
60 Config {
61 compatible: false,
62 xlang: true,
63 share_meta: false,
64 compress_string: false,
65 check_string_read: true,
66 max_dyn_depth: 5,
67 check_struct_version: false,
68 track_ref: false,
69 max_graph_memory_bytes: 128 * 1024 * 1024,
70 max_type_fields: 512,
71 max_type_meta_bytes: 4096,
72 max_schema_versions_per_type: 10,
73 max_average_schema_versions_per_type: 3,
74 }
75 }
76}
77
78impl Config {
79 /// Creates a new Config with default values.
80 pub fn new() -> Self {
81 Self::default()
82 }
83
84 /// Check if compatible mode is enabled.
85 #[inline(always)]
86 pub fn is_compatible(&self) -> bool {
87 self.compatible
88 }
89
90 /// Check if xlang mode is enabled.
91 #[inline(always)]
92 pub fn is_xlang(&self) -> bool {
93 self.xlang
94 }
95
96 /// Check if meta sharing is enabled.
97 #[inline(always)]
98 pub fn is_share_meta(&self) -> bool {
99 self.share_meta
100 }
101
102 /// Check if string compression is enabled.
103 #[inline(always)]
104 pub fn is_compress_string(&self) -> bool {
105 self.compress_string
106 }
107
108 /// Check if UTF-8 string payload validation is enabled.
109 #[inline(always)]
110 pub fn is_check_string_read(&self) -> bool {
111 self.check_string_read
112 }
113
114 /// Get maximum dynamic depth.
115 #[inline(always)]
116 pub fn max_dyn_depth(&self) -> u32 {
117 self.max_dyn_depth
118 }
119
120 /// Check if class version checking is enabled.
121 #[inline(always)]
122 pub fn is_check_struct_version(&self) -> bool {
123 self.check_struct_version
124 }
125
126 /// Check if reference tracking is enabled.
127 #[inline(always)]
128 pub fn is_track_ref(&self) -> bool {
129 self.track_ref
130 }
131
132 /// Get the approximate graph-memory gate per root deserialization.
133 #[inline(always)]
134 pub fn max_graph_memory_bytes(&self) -> usize {
135 self.max_graph_memory_bytes
136 }
137
138 /// Get maximum accepted field count in one received struct TypeMeta.
139 #[inline(always)]
140 pub fn max_type_fields(&self) -> usize {
141 self.max_type_fields as usize
142 }
143
144 /// Get maximum accepted body size in one received TypeMeta.
145 #[inline(always)]
146 pub fn max_type_meta_bytes(&self) -> usize {
147 self.max_type_meta_bytes as usize
148 }
149
150 /// Get maximum accepted remote metadata versions for one logical type.
151 #[inline(always)]
152 pub fn max_schema_versions_per_type(&self) -> usize {
153 self.max_schema_versions_per_type as usize
154 }
155
156 /// Get maximum accepted average remote metadata versions across logical types.
157 #[inline(always)]
158 pub fn max_average_schema_versions_per_type(&self) -> usize {
159 self.max_average_schema_versions_per_type as usize
160 }
161}