Skip to main content

nodedb_array/schema/
attr_spec.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Attribute specification.
4
5use serde::{Deserialize, Serialize};
6
7/// Attribute type tag. One-to-one with
8/// [`crate::types::cell_value::value::CellValue`] (excluding `Null`,
9/// which is per-cell, not per-attribute).
10#[derive(
11    Debug,
12    Clone,
13    Copy,
14    PartialEq,
15    Eq,
16    Serialize,
17    Deserialize,
18    zerompk::ToMessagePack,
19    zerompk::FromMessagePack,
20)]
21#[serde(rename_all = "snake_case")]
22#[msgpack(c_enum)]
23pub enum AttrType {
24    Int64,
25    Float64,
26    String,
27    Bytes,
28}
29
30/// One attribute column on an [`super::ArraySchema`].
31#[derive(
32    Debug,
33    Clone,
34    PartialEq,
35    Eq,
36    Serialize,
37    Deserialize,
38    zerompk::ToMessagePack,
39    zerompk::FromMessagePack,
40)]
41pub struct AttrSpec {
42    pub name: String,
43    pub dtype: AttrType,
44    /// Whether the attribute may carry [`crate::types::CellValue::Null`].
45    pub nullable: bool,
46}
47
48impl AttrSpec {
49    pub fn new(name: impl Into<String>, dtype: AttrType, nullable: bool) -> Self {
50        Self {
51            name: name.into(),
52            dtype,
53            nullable,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn attr_spec_round_trip_eq() {
64        let a = AttrSpec::new("qual", AttrType::Float64, true);
65        let b = a.clone();
66        assert_eq!(a, b);
67    }
68}