datalogic_rs/value/
mod.rs

1//! Value representation for efficient data processing.
2//!
3//! This module provides a memory-efficient value type that leverages arena allocation.
4//! It replaces direct dependency on `serde_json::Value` with a custom implementation
5//! optimized for rule evaluation.
6
7mod access;
8mod convert;
9mod data_value;
10mod datetime;
11mod number;
12
13pub use access::{parse_path, PathSegment, ValueAccess};
14pub use convert::{
15    data_value_to_json, hash_map_to_data_value, json_to_data_value, FromJson, ToJson,
16};
17pub use data_value::DataValue;
18pub use datetime::{date_diff, format_duration, parse_datetime, parse_duration};
19pub use number::NumberValue;
20
21use crate::arena::DataArena;
22
23/// A trait for types that can be converted to a DataValue.
24pub trait IntoDataValue<'a> {
25    /// Converts the value to a DataValue, allocating in the given arena.
26    fn into_data_value(self, arena: &'a DataArena) -> DataValue<'a>;
27}
28
29/// A trait for types that can be extracted from a DataValue.
30pub trait FromDataValue<T> {
31    /// Extracts a value of type T from a DataValue.
32    fn from_data_value(value: &DataValue) -> Option<T>;
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::arena::DataArena;
39
40    #[test]
41    fn test_data_value_creation() {
42        let arena = DataArena::new();
43
44        // Create different types of values
45        let null = DataValue::null();
46        let boolean = DataValue::bool(true);
47        let number = DataValue::integer(42);
48        let string = DataValue::string(&arena, "hello");
49
50        // Test basic properties
51        assert!(null.is_null());
52        assert!(boolean.is_bool());
53        assert!(number.is_number());
54        assert!(string.is_string());
55
56        // Test value extraction
57        assert_eq!(boolean.as_bool(), Some(true));
58        assert_eq!(number.as_i64(), Some(42));
59        assert_eq!(string.as_str(), Some("hello"));
60    }
61
62    #[test]
63    fn test_array_and_object() {
64        let arena = DataArena::new();
65
66        // Create array using the array constructor method
67        let array = DataValue::array(
68            &arena,
69            &[
70                DataValue::integer(1),
71                DataValue::integer(2),
72                DataValue::integer(3),
73            ],
74        );
75
76        assert!(array.is_array());
77        assert_eq!(array.as_array().unwrap().len(), 3);
78
79        // Create object using the object constructor method
80        let object = DataValue::object(
81            &arena,
82            &[
83                (arena.intern_str("a"), DataValue::integer(1)),
84                (arena.intern_str("b"), DataValue::integer(2)),
85            ],
86        );
87
88        assert!(object.is_object());
89        assert_eq!(object.as_object().unwrap().len(), 2);
90    }
91}