nuts_storable/
lib.rs

1use std::collections::HashMap;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum DateTimeUnit {
5    Seconds,
6    Milliseconds,
7    Microseconds,
8    Nanoseconds,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ItemType {
13    U64,
14    I64,
15    F64,
16    F32,
17    Bool,
18    String,
19    DateTime64(DateTimeUnit),
20    TimeDelta64(DateTimeUnit),
21}
22
23#[derive(Debug, Clone, PartialEq)]
24pub enum Value {
25    U64(Vec<u64>),
26    I64(Vec<i64>),
27    F64(Vec<f64>),
28    F32(Vec<f32>),
29    Bool(Vec<bool>),
30    ScalarString(String),
31    DateTime64(DateTimeUnit, Vec<i64>),
32    TimeDelta64(DateTimeUnit, Vec<i64>),
33    ScalarU64(u64),
34    ScalarI64(i64),
35    ScalarF64(f64),
36    ScalarF32(f32),
37    ScalarBool(bool),
38    Strings(Vec<String>),
39}
40
41impl From<Vec<u64>> for Value {
42    fn from(value: Vec<u64>) -> Self {
43        Value::U64(value)
44    }
45}
46impl From<Vec<i64>> for Value {
47    fn from(value: Vec<i64>) -> Self {
48        Value::I64(value)
49    }
50}
51impl From<Vec<f64>> for Value {
52    fn from(value: Vec<f64>) -> Self {
53        Value::F64(value)
54    }
55}
56impl From<Vec<f32>> for Value {
57    fn from(value: Vec<f32>) -> Self {
58        Value::F32(value)
59    }
60}
61impl From<Vec<bool>> for Value {
62    fn from(value: Vec<bool>) -> Self {
63        Value::Bool(value)
64    }
65}
66impl From<u64> for Value {
67    fn from(value: u64) -> Self {
68        Value::ScalarU64(value)
69    }
70}
71impl From<i64> for Value {
72    fn from(value: i64) -> Self {
73        Value::ScalarI64(value)
74    }
75}
76impl From<f64> for Value {
77    fn from(value: f64) -> Self {
78        Value::ScalarF64(value)
79    }
80}
81impl From<f32> for Value {
82    fn from(value: f32) -> Self {
83        Value::ScalarF32(value)
84    }
85}
86impl From<bool> for Value {
87    fn from(value: bool) -> Self {
88        Value::ScalarBool(value)
89    }
90}
91
92pub trait HasDims {
93    fn dim_sizes(&self) -> HashMap<String, u64>;
94    fn coords(&self) -> HashMap<String, Value> {
95        HashMap::new()
96    }
97}
98
99pub trait Storable<P: HasDims + ?Sized>: Send + Sync {
100    fn names(parent: &P) -> Vec<&str>;
101    fn item_type(parent: &P, item: &str) -> ItemType;
102    fn dims<'a>(parent: &'a P, item: &str) -> Vec<&'a str>;
103
104    fn get_all<'a>(&'a mut self, parent: &'a P) -> Vec<(&'a str, Option<Value>)>;
105}
106
107impl<P: HasDims> Storable<P> for Vec<f64> {
108    fn names(_parent: &P) -> Vec<&str> {
109        vec!["value"]
110    }
111
112    fn item_type(_parent: &P, _item: &str) -> ItemType {
113        ItemType::F64
114    }
115
116    fn dims<'a>(_parent: &'a P, _item: &str) -> Vec<&'a str> {
117        vec!["dim"]
118    }
119
120    fn get_all<'a>(&'a mut self, _parent: &'a P) -> Vec<(&'a str, Option<Value>)> {
121        vec![("value", Some(Value::F64(self.clone())))]
122    }
123}
124
125impl<P: HasDims> Storable<P> for () {
126    fn names(_parent: &P) -> Vec<&str> {
127        vec![]
128    }
129
130    fn item_type(_parent: &P, _item: &str) -> ItemType {
131        panic!("No items in unit type")
132    }
133
134    fn dims<'a>(_parent: &'a P, _item: &str) -> Vec<&'a str> {
135        panic!("No items in unit type")
136    }
137
138    fn get_all(&mut self, _parent: &P) -> Vec<(&str, Option<Value>)> {
139        vec![]
140    }
141}