nuts_storable/
lib.rs

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