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}
44
45impl Default for Config {
46 fn default() -> Self {
47 Config {
48 compatible: false,
49 xlang: true,
50 share_meta: false,
51 compress_string: false,
52 check_string_read: true,
53 max_dyn_depth: 5,
54 check_struct_version: false,
55 track_ref: false,
56 }
57 }
58}
59
60impl Config {
61 /// Creates a new Config with default values.
62 pub fn new() -> Self {
63 Self::default()
64 }
65
66 /// Check if compatible mode is enabled.
67 #[inline(always)]
68 pub fn is_compatible(&self) -> bool {
69 self.compatible
70 }
71
72 /// Check if xlang mode is enabled.
73 #[inline(always)]
74 pub fn is_xlang(&self) -> bool {
75 self.xlang
76 }
77
78 /// Check if meta sharing is enabled.
79 #[inline(always)]
80 pub fn is_share_meta(&self) -> bool {
81 self.share_meta
82 }
83
84 /// Check if string compression is enabled.
85 #[inline(always)]
86 pub fn is_compress_string(&self) -> bool {
87 self.compress_string
88 }
89
90 /// Check if UTF-8 string payload validation is enabled.
91 #[inline(always)]
92 pub fn is_check_string_read(&self) -> bool {
93 self.check_string_read
94 }
95
96 /// Get maximum dynamic depth.
97 #[inline(always)]
98 pub fn max_dyn_depth(&self) -> u32 {
99 self.max_dyn_depth
100 }
101
102 /// Check if class version checking is enabled.
103 #[inline(always)]
104 pub fn is_check_struct_version(&self) -> bool {
105 self.check_struct_version
106 }
107
108 /// Check if reference tracking is enabled.
109 #[inline(always)]
110 pub fn is_track_ref(&self) -> bool {
111 self.track_ref
112 }
113}