Skip to main content

nodedb_array/schema/
dim_spec.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Dimension specification.
4
5use serde::{Deserialize, Serialize};
6
7use crate::types::Domain;
8
9/// Dimension type tag. Each variant has a one-to-one mapping with
10/// [`crate::types::coord::value::CoordValue`] — the encoder dispatches
11/// off this tag.
12#[derive(
13    Debug,
14    Clone,
15    Copy,
16    PartialEq,
17    Eq,
18    Serialize,
19    Deserialize,
20    zerompk::ToMessagePack,
21    zerompk::FromMessagePack,
22)]
23#[serde(rename_all = "snake_case")]
24#[msgpack(c_enum)]
25pub enum DimType {
26    Int64,
27    Float64,
28    /// Wall-clock milliseconds since epoch — same wire shape as `Int64`
29    /// but treated separately so SQL surfaces and analyzers can format
30    /// it correctly.
31    TimestampMs,
32    String,
33}
34
35/// One dimension of an [`super::ArraySchema`].
36#[derive(
37    Debug,
38    Clone,
39    PartialEq,
40    Eq,
41    Serialize,
42    Deserialize,
43    zerompk::ToMessagePack,
44    zerompk::FromMessagePack,
45)]
46pub struct DimSpec {
47    pub name: String,
48    pub dtype: DimType,
49    pub domain: Domain,
50}
51
52impl DimSpec {
53    pub fn new(name: impl Into<String>, dtype: DimType, domain: Domain) -> Self {
54        Self {
55            name: name.into(),
56            dtype,
57            domain,
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use crate::types::domain::DomainBound;
66
67    #[test]
68    fn dim_spec_round_trip_eq() {
69        let d = DimSpec::new(
70            "chrom",
71            DimType::Int64,
72            Domain::new(DomainBound::Int64(0), DomainBound::Int64(24)),
73        );
74        let e = d.clone();
75        assert_eq!(d, e);
76    }
77}