ic_dbms_api/dbms/sanitize/
null_if_empty.rs1use crate::prelude::{IcDbmsResult, Sanitize, Value};
2
3pub struct NullIfEmptySanitizer;
18
19impl Sanitize for NullIfEmptySanitizer {
20 fn sanitize(&self, value: Value) -> IcDbmsResult<Value> {
21 match value {
22 Value::Text(text) if text.as_str().is_empty() => Ok(Value::Null),
23 other => Ok(other),
24 }
25 }
26}
27
28#[cfg(test)]
29mod tests {
30
31 use super::*;
32
33 #[test]
34 fn test_null_if_empty_sanitizer() {
35 let sanitizer = NullIfEmptySanitizer;
36 let empty_string = Value::Text("".into());
37 let non_empty_string = Value::Text("Hello".into());
38 let number_value = Value::Int32(32.into());
39
40 let sanitized_empty = sanitizer.sanitize(empty_string).unwrap();
41 let sanitized_non_empty = sanitizer.sanitize(non_empty_string).unwrap();
42 let sanitized_number = sanitizer.sanitize(number_value).unwrap();
43 assert_eq!(sanitized_empty, Value::Null);
44 assert_eq!(sanitized_non_empty, Value::Text("Hello".into()));
45 assert_eq!(sanitized_number, Value::Int32(32.into()));
46 }
47}