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 allowed size for binary data in bytes.
44 /// Prevents excessive memory allocation from untrusted payloads.
45 pub max_binary_size: u32,
46 /// Maximum allowed number of elements in a collection or entries in a map.
47 /// Prevents excessive memory allocation from untrusted payloads.
48 pub max_collection_size: u32,
49}
50
51impl Default for Config {
52 fn default() -> Self {
53 Config {
54 compatible: false,
55 xlang: true,
56 share_meta: false,
57 compress_string: false,
58 check_string_read: true,
59 max_dyn_depth: 5,
60 check_struct_version: false,
61 track_ref: false,
62 max_binary_size: 64 * 1024 * 1024, // 64MB default
63 max_collection_size: 1024 * 1024, // 1M elements default
64 }
65 }
66}
67
68impl Config {
69 /// Creates a new Config with default values.
70 pub fn new() -> Self {
71 Self::default()
72 }
73
74 /// Check if compatible mode is enabled.
75 #[inline(always)]
76 pub fn is_compatible(&self) -> bool {
77 self.compatible
78 }
79
80 /// Check if xlang mode is enabled.
81 #[inline(always)]
82 pub fn is_xlang(&self) -> bool {
83 self.xlang
84 }
85
86 /// Check if meta sharing is enabled.
87 #[inline(always)]
88 pub fn is_share_meta(&self) -> bool {
89 self.share_meta
90 }
91
92 /// Check if string compression is enabled.
93 #[inline(always)]
94 pub fn is_compress_string(&self) -> bool {
95 self.compress_string
96 }
97
98 /// Check if UTF-8 string payload validation is enabled.
99 #[inline(always)]
100 pub fn is_check_string_read(&self) -> bool {
101 self.check_string_read
102 }
103
104 /// Get maximum dynamic depth.
105 #[inline(always)]
106 pub fn max_dyn_depth(&self) -> u32 {
107 self.max_dyn_depth
108 }
109
110 /// Check if class version checking is enabled.
111 #[inline(always)]
112 pub fn is_check_struct_version(&self) -> bool {
113 self.check_struct_version
114 }
115
116 /// Check if reference tracking is enabled.
117 #[inline(always)]
118 pub fn is_track_ref(&self) -> bool {
119 self.track_ref
120 }
121
122 /// Get maximum allowed binary data size in bytes.
123 #[inline(always)]
124 pub fn max_binary_size(&self) -> u32 {
125 self.max_binary_size
126 }
127
128 /// Get maximum allowed collection/map element count.
129 #[inline(always)]
130 pub fn max_collection_size(&self) -> u32 {
131 self.max_collection_size
132 }
133}