Skip to main content

nodedb_types/
array_cell.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Typed payload for `Value::ArrayCell`.
4//!
5//! An `ArrayCell` carries a single N-dimensional array cell across the
6//! SQL / wire boundary: its coordinates (one `Value` per dimension) and
7//! its attributes (one `Value` per attribute column). The array engine
8//! converts between its own typed `CoordValue` / `CellValue` and this
9//! generic carrier at engine boundaries.
10//!
11//! Using `nodedb_types::Value` for both coords and attrs keeps
12//! `nodedb-types` free of any dependency on `nodedb-array`.
13//!
14//! Two cells are equal when their coords and attrs are structurally
15//! equal. Ordering is lexicographic on coords first, then attrs — this
16//! matches N-d array semantics where cells are ordered by coordinate.
17
18use serde::{Deserialize, Serialize};
19
20use crate::value::Value;
21
22/// One N-dimensional array cell — coordinates plus attribute values.
23#[derive(
24    Debug,
25    Clone,
26    PartialEq,
27    Serialize,
28    Deserialize,
29    zerompk::ToMessagePack,
30    zerompk::FromMessagePack,
31)]
32pub struct ArrayCell {
33    /// One `Value` per dimension. Length = rank of the array.
34    pub coords: Vec<Value>,
35    /// One `Value` per attribute column.
36    pub attrs: Vec<Value>,
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    fn sample_cell() -> ArrayCell {
44        ArrayCell {
45            coords: vec![Value::Integer(1), Value::Integer(2)],
46            attrs: vec![Value::Float(3.5), Value::String("label".into())],
47        }
48    }
49
50    #[test]
51    fn zerompk_roundtrip() {
52        let cell = sample_cell();
53        let bytes = zerompk::to_msgpack_vec(&cell).expect("encode");
54        let decoded: ArrayCell = zerompk::from_msgpack(&bytes).expect("decode");
55        assert_eq!(decoded, cell);
56    }
57
58    #[test]
59    fn json_roundtrip() {
60        let cell = sample_cell();
61        let s = sonic_rs::to_string(&cell).expect("json encode");
62        let decoded: ArrayCell = sonic_rs::from_str(&s).expect("json decode");
63        assert_eq!(decoded, cell);
64    }
65
66    #[test]
67    fn equality_structural() {
68        let a = sample_cell();
69        let b = sample_cell();
70        assert_eq!(a, b);
71
72        let mut c = sample_cell();
73        c.coords[0] = Value::Integer(999);
74        assert_ne!(a, c);
75    }
76}