Skip to main content

rsjson_lua/
config.rs

1// SPDX-License-Identifier: MIT
2
3use std::ops::Deref;
4
5#[derive(mlua::UserData, mlua::FromLua, Clone)]
6pub struct EncodeConfig {
7    #[lua(skip)]
8    options: mlua::DeserializeOptions,
9    #[lua(skip)]
10    pub(crate) indent: Option<usize>,
11    #[lua(skip)]
12    pub(crate) prefix: String,
13}
14
15impl From<mlua::DeserializeOptions> for EncodeConfig {
16    fn from(value: mlua::DeserializeOptions) -> Self {
17        EncodeConfig {
18            options: value,
19            indent: None,
20            prefix: " ".to_string(),
21        }
22    }
23}
24
25impl From<EncodeConfig> for mlua::DeserializeOptions {
26    fn from(value: EncodeConfig) -> Self {
27        value.options
28    }
29}
30
31impl AsRef<mlua::DeserializeOptions> for EncodeConfig {
32    fn as_ref(&self) -> &mlua::DeserializeOptions {
33        &self.options
34    }
35}
36
37impl Deref for EncodeConfig {
38    type Target = mlua::DeserializeOptions;
39
40    fn deref(&self) -> &Self::Target {
41        &self.options
42    }
43}
44
45impl Default for EncodeConfig {
46    fn default() -> Self {
47        mlua::DeserializeOptions::new().into()
48    }
49}
50
51#[mlua::userdata_impl]
52impl EncodeConfig {
53    #[lua(name = "new", infallible)]
54    pub(crate) fn lua_new() -> Self {
55        Self::default()
56    }
57
58    #[lua(name = "indent", getter, infallible)]
59    pub(crate) fn lua_indent(&self) -> Option<usize> {
60        self.indent
61    }
62
63    #[lua(name = "set_indent", infallible)]
64    pub(crate) fn lua_set_indent(&mut self, indent: Option<usize>) -> Self {
65        self.indent = indent;
66        self.clone()
67    }
68
69    #[lua(name = "prefix", getter, infallible)]
70    pub(crate) fn lua_prefix(&self) -> String {
71        self.prefix.clone()
72    }
73
74    #[lua(name = "set_prefix", infallible)]
75    pub(crate) fn lua_set_prefix(&mut self, prefix: &str) -> Self {
76        self.prefix = prefix.to_string();
77        self.clone()
78    }
79
80    #[lua(name = "deny_unsupported_types", getter, infallible)]
81    pub(crate) fn lua_deny_unsupported_types(&self) -> bool {
82        self.options.deny_unsupported_types
83    }
84
85    #[lua(name = "set_deny_unsupported_types", infallible)]
86    pub(crate) fn lua_set_deny_unsupported_types(&mut self, enable: bool) -> Self {
87        self.options = self.options.deny_unsupported_types(enable);
88        self.clone()
89    }
90
91    #[lua(name = "deny_recursive_tables", getter, infallible)]
92    pub(crate) fn lua_deny_recursive_tables(&self) -> bool {
93        self.options.deny_recursive_tables
94    }
95
96    #[lua(name = "set_deny_recursive_tables", infallible)]
97    pub(crate) fn lua_set_deny_recursive_tables(&mut self, enable: bool) -> Self {
98        self.options = self.options.deny_recursive_tables(enable);
99        self.clone()
100    }
101
102    #[lua(name = "sort_keys", getter, infallible)]
103    pub(crate) fn lua_sort_keys(&self) -> bool {
104        self.options.sort_keys
105    }
106
107    #[lua(name = "set_sort_keys", infallible)]
108    pub(crate) fn lua_set_sort_keys(&mut self, enable: bool) -> Self {
109        self.options = self.options.sort_keys(enable);
110        self.clone()
111    }
112
113    #[lua(name = "encode_empty_tables_as_array", getter, infallible)]
114    pub(crate) fn lua_encode_empty_tables_as_array(&self) -> bool {
115        self.options.encode_empty_tables_as_array
116    }
117
118    #[lua(name = "set_encode_empty_tables_as_array", infallible)]
119    pub(crate) fn lua_set_encode_empty_tables_as_array(&mut self, enable: bool) -> Self {
120        self.options = self.options.encode_empty_tables_as_array(enable);
121        self.clone()
122    }
123
124    #[lua(name = "detect_mixed_tables", getter, infallible)]
125    pub(crate) fn lua_detect_mixed_tables(&self) -> bool {
126        self.options.detect_mixed_tables
127    }
128
129    #[lua(name = "set_detect_mixed_tables", infallible)]
130    pub(crate) fn lua_set_detect_mixed_tables(&mut self, enable: bool) -> Self {
131        self.options = self.options.detect_mixed_tables(enable);
132        self.clone()
133    }
134}
135
136#[derive(mlua::UserData, mlua::FromLua, Clone)]
137pub struct DecodeConfig {
138    #[lua(skip)]
139    options: mlua::SerializeOptions,
140    #[lua(skip)]
141    pub(crate) cast_u64_to_f64: bool,
142}
143
144impl From<mlua::SerializeOptions> for DecodeConfig {
145    fn from(value: mlua::SerializeOptions) -> Self {
146        DecodeConfig {
147            options: value,
148            cast_u64_to_f64: false,
149        }
150    }
151}
152
153impl From<DecodeConfig> for mlua::SerializeOptions {
154    fn from(value: DecodeConfig) -> Self {
155        value.options
156    }
157}
158
159impl AsRef<mlua::SerializeOptions> for DecodeConfig {
160    fn as_ref(&self) -> &mlua::SerializeOptions {
161        &self.options
162    }
163}
164
165impl Deref for DecodeConfig {
166    type Target = mlua::SerializeOptions;
167
168    fn deref(&self) -> &Self::Target {
169        &self.options
170    }
171}
172
173impl Default for DecodeConfig {
174    fn default() -> Self {
175        mlua::SerializeOptions::new()
176            .detect_serde_json_arbitrary_precision(true)
177            .into()
178    }
179}
180
181#[mlua::userdata_impl]
182impl DecodeConfig {
183    #[lua(name = "new", infallible)]
184    pub(crate) fn lua_new() -> Self {
185        Self::default()
186    }
187
188    #[lua(name = "null", getter, infallible)]
189    pub(crate) fn lua_null(&self) -> bool {
190        self.options.serialize_unit_to_null && self.options.serialize_none_to_null
191    }
192
193    #[lua(name = "set_null", infallible)]
194    pub(crate) fn lua_set_null(&mut self, enable: bool) -> Self {
195        self.options = self
196            .options
197            .serialize_unit_to_null(enable)
198            .serialize_none_to_null(enable);
199        self.clone()
200    }
201
202    #[lua(name = "cast_u64_to_f64", getter, infallible)]
203    pub(crate) fn lua_cast_u64_to_f64(&self) -> bool {
204        self.cast_u64_to_f64
205    }
206
207    #[lua(name = "set_cast_u64_to_f64", infallible)]
208    pub(crate) fn lua_set_cast_u64_to_f64(&mut self, enable: bool) -> Self {
209        self.cast_u64_to_f64 = enable;
210        self.clone()
211    }
212
213    #[lua(name = "array_metatable", getter, infallible)]
214    pub(crate) fn lua_array_metatable(&self) -> bool {
215        self.options.set_array_metatable
216    }
217
218    #[lua(name = "set_array_metatable", infallible)]
219    pub(crate) fn lua_set_array_metatable(&mut self, enable: bool) -> Self {
220        self.options = self.options.set_array_metatable(enable);
221        self.clone()
222    }
223}