1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! # Value Conversion Module
//!
//! This module provides traits and utilities for type-safe value conversion
//! and validation, particularly useful for database operations and dynamic
//! type handling.
//!
//! # 值转换模块
//!
//! 该模块提供了类型安全的值转换和验证的trait和工具函数,
//! 特别适用于数据库操作和动态类型处理。
use Any;
/// Trait for converting values to a specific type from a dynamic `Any` reference.
///
/// This trait provides a unified interface for converting runtime values to strongly-typed
/// data structures. It's particularly useful in database operations where field values
/// need to be converted from generic containers to specific types.
///
/// ## Usage
///
/// Implementors should provide logic to safely extract and convert values from the
/// `Any` trait object, handling various input formats including nested `Option` types.
///
/// ## Safety
///
/// Implementations should handle type conversion errors gracefully and provide
/// sensible defaults when conversion fails.
///
/// 从动态 `Any` 引用转换值到特定类型的trait。
///
/// 该trait提供了将运行时值转换为强类型数据结构的统一接口。
/// 在数据库操作中特别有用,可以将字段值从通用容器转换为特定类型。
///
/// ## 使用方法
///
/// 实现者应该提供从 `Any` trait对象安全提取和转换值的逻辑,
/// 处理各种输入格式,包括嵌套的 `Option` 类型。
///
/// ## 安全性
///
/// 实现应该优雅地处理类型转换错误,并在转换失败时提供合理的默认值。
/// Helper function to recursively unwrap any number of Option layers
/// and return the inner value if it exists.
///
/// This function is particularly useful when dealing with nested Option types
/// that may arise from various data sources, such as database nullable fields
/// or JSON parsing operations. It can handle multiple levels of Option wrapping:
///
/// - `Option<Option<T>>` - Nested optional values
/// - `Option<T>` - Single optional values
/// - `T` - Direct values
///
/// # Type Parameters
///
/// * `T` - The target type to extract from the Option wrapper(s)
///
/// # Parameters
///
/// * `value` - A reference to any type that implements `Any`
///
/// # Returns
///
/// * `Some(&T)` - If a value of type T is found within the Option wrapper(s)
/// * `None` - If the value is `None` at any level or cannot be downcast to the target type
///
/// # Examples
///
/// ```rust,no_run
/// use std::any::Any;
///
/// let nested: Option<Option<String>> = Some(Some("Hello".to_string()));
/// let simple: Option<String> = Some("World".to_string());
/// let direct: String = "Direct".to_string();
/// let empty: Option<String> = None;
///
/// assert_eq!(unwrap_option::<String>(&nested), Some(&"Hello".to_string()));
/// assert_eq!(unwrap_option::<String>(&simple), Some(&"World".to_string()));
/// assert_eq!(unwrap_option::<String>(&direct), Some(&"Direct".to_string()));
/// assert_eq!(unwrap_option::<String>(&empty), None);
/// ```
///
/// 递归解包任意数量的Option层并返回内部值(如果存在的话)的辅助函数。
///
/// 该函数在处理嵌套Option类型时特别有用,这些类型可能来自各种数据源,
/// 如数据库可空字段或JSON解析操作。它可以处理多层Option包装:
///
/// - `Option<Option<T>>` - 嵌套可选值
/// - `Option<T>` - 单一可选值
/// - `T` - 直接值
///
/// # 类型参数
///
/// * `T` - 从 Option 包装器中提取的目标类型
///
/// # 参数
///
/// * `value` - 实现了 `Any` 的任意类型的引用
///
/// # 返回值
///
/// * `Some(&T)` - 如果在 Option 包装器中找到类型 T 的值
/// * `None` - 如果在任意层级上值为 `None` 或无法向下转换为目标类型
///
/// # 示例
///
/// ```rust,no_run
/// use std::any::Any;
///
/// let nested: Option<Option<String>> = Some(Some("Hello".to_string()));
/// let simple: Option<String> = Some("World".to_string());
/// let direct: String = "Direct".to_string();
/// let empty: Option<String> = None;
///
/// assert_eq!(unwrap_option::<String>(&nested), Some(&"Hello".to_string()));
/// assert_eq!(unwrap_option::<String>(&simple), Some(&"World".to_string()));
/// assert_eq!(unwrap_option::<String>(&direct), Some(&"Direct".to_string()));
/// assert_eq!(unwrap_option::<String>(&empty), None);
/// ```
/// Helper function to check if a value is empty and handle Option types using a closure.
///
/// This function provides a comprehensive way to determine if a value should be
/// considered "empty" or "absent". It supports multiple types and handles nested
/// Option types gracefully. The function is particularly useful for:
///
/// - Database field validation
/// - Input sanitization
/// - Default value determination
/// - Conditional processing based on data presence
///
/// ## Supported Types
///
/// The function can check emptiness for:
///
/// - **String types**: `String`, `&str` - empty strings or strings equal to "null" (case-insensitive)
/// - **Binary types**: `Vec<u8>`, `&[u8]` - empty byte arrays
/// - **Option types**: Any level of Option nesting, including `Option<Option<T>>`
/// - **Unit type**: `Option<()>` - checks if None
///
/// ## Option Handling
///
/// The function can handle multiple levels of Option wrapping:
/// - `Option<Option<T>>` - Returns true if None at any level or if inner value is empty
/// - `Option<T>` - Returns true if None or if inner value is empty
/// - `T` - Returns true if value is considered empty for its type
///
/// # Parameters
///
/// * `value` - A reference to any type that implements `Any`
///
/// # Returns
///
/// * `true` - If the value is empty, None, or represents an absent value
/// * `false` - If the value contains meaningful data
///
/// # Examples
///
/// ```rust,no_run
/// use std::any::Any;
///
/// // String examples
/// assert_eq!(is_empty_or_none(&""), true); // Empty string
/// assert_eq!(is_empty_or_none(&"null"), true); // Null string (case-insensitive)
/// assert_eq!(is_empty_or_none(&"Hello"), false); // Non-empty string
///
/// // Option examples
/// let none_str: Option<String> = None;
/// let some_empty = Some("".to_string());
/// let some_value = Some("Hello".to_string());
///
/// assert_eq!(is_empty_or_none(&none_str), true); // None option
/// assert_eq!(is_empty_or_none(&some_empty), true); // Some with empty value
/// assert_eq!(is_empty_or_none(&some_value), false); // Some with value
///
/// // Binary examples
/// let empty_vec: Vec<u8> = vec![];
/// let filled_vec = vec![1, 2, 3];
///
/// assert_eq!(is_empty_or_none(&empty_vec), true); // Empty vector
/// assert_eq!(is_empty_or_none(&filled_vec), false); // Non-empty vector
/// ```
///
/// 检查值是否为空并使用闭包处理Option类型的辅助函数。
///
/// 该函数提供了一种全面的方式来确定值是否应被认为“空”或“缺失”。
/// 它支持多种类型并优雅地处理嵌套Option类型。该函数特别适用于:
///
/// - 数据库字段验证
/// - 输入清理
/// - 默认值确定
/// - 基于数据存在性的条件处理
///
/// ## 支持的类型
///
/// 该函数可以检查以下类型的空值:
///
/// - **字符串类型**: `String`、`&str` - 空字符串或等于"null"的字符串(不区分大小写)
/// - **二进制类型**: `Vec<u8>`、`&[u8]` - 空字节数组
/// - **Option类型**: 任意层级的Option嵌套,包括 `Option<Option<T>>`
/// - **单元类型**: `Option<()>` - 检查是否为None
///
/// ## Option处理
///
/// 该函数可以处理多层Option包装:
/// - `Option<Option<T>>` - 如果在任意层级为None或内部值为空则返回true
/// - `Option<T>` - 如果为None或内部值为空则返回true
/// - `T` - 如果该类型的值被认为空则返回true
///
/// # 参数
///
/// * `value` - 实现了 `Any` 的任意类型的引用
///
/// # 返回值
///
/// * `true` - 如果值为空、None或表示缺失的值
/// * `false` - 如果值包含有意义的数据
///
/// # 示例
///
/// ```rust,no_run
/// use std::any::Any;
///
/// // 字符串示例
/// assert_eq!(is_empty_or_none(&""), true); // 空字符串
/// assert_eq!(is_empty_or_none(&"null"), true); // Null字符串(不区分大小写)
/// assert_eq!(is_empty_or_none(&"Hello"), false); // 非空字符串
///
/// // Option示例
/// let none_str: Option<String> = None;
/// let some_empty = Some("".to_string());
/// let some_value = Some("Hello".to_string());
///
/// assert_eq!(is_empty_or_none(&none_str), true); // None option
/// assert_eq!(is_empty_or_none(&some_empty), true); // 包含空值的Some
/// assert_eq!(is_empty_or_none(&some_value), false); // 包含值的Some
///
/// // 二进制示例
/// let empty_vec: Vec<u8> = vec![];
/// let filled_vec = vec![1, 2, 3];
///
/// assert_eq!(is_empty_or_none(&empty_vec), true); // 空向量
/// assert_eq!(is_empty_or_none(&filled_vec), false); // 非空向量
/// ```