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