libsurfer/
data_container.rs

1use crate::time::{TimeScale, TimeUnit};
2use crate::transaction_container::{StreamScopeRef, TransactionContainer, TransactionStreamRef};
3use crate::wave_container::{MetaData, SimulationStatus, VariableRef, WaveContainer};
4use crate::wave_data::ScopeType;
5use crate::wave_data::ScopeType::{StreamScope, WaveScope};
6use num::BigUint;
7
8#[allow(clippy::large_enum_variant)]
9pub enum DataContainer {
10    Waves(WaveContainer),
11    Transactions(TransactionContainer),
12    Empty,
13}
14
15#[derive(Debug, Clone)]
16pub enum VariableType {
17    Variable(VariableRef),
18    Generator(TransactionStreamRef),
19}
20
21impl VariableType {
22    pub fn name(&self) -> String {
23        match self {
24            VariableType::Variable(v) => v.name.clone(),
25            VariableType::Generator(g) => g.name.clone(),
26        }
27    }
28}
29
30impl DataContainer {
31    pub fn __new_empty() -> Self {
32        DataContainer::Empty
33    }
34
35    pub fn as_waves(&self) -> Option<&WaveContainer> {
36        match self {
37            DataContainer::Waves(w) => Some(w),
38            DataContainer::Transactions(_) => None,
39            DataContainer::Empty => None,
40        }
41    }
42
43    pub fn as_waves_mut(&mut self) -> Option<&mut WaveContainer> {
44        match self {
45            DataContainer::Waves(w) => Some(w),
46            DataContainer::Transactions(_) => None,
47            DataContainer::Empty => None,
48        }
49    }
50
51    pub fn as_transactions(&self) -> Option<&TransactionContainer> {
52        match self {
53            DataContainer::Waves(_) => None,
54            DataContainer::Transactions(t) => Some(t),
55            DataContainer::Empty => None,
56        }
57    }
58
59    pub fn as_transactions_mut(&mut self) -> Option<&mut TransactionContainer> {
60        match self {
61            DataContainer::Waves(_) => None,
62            DataContainer::Transactions(t) => Some(t),
63            DataContainer::Empty => None,
64        }
65    }
66
67    pub fn is_waves(&self) -> bool {
68        match self {
69            DataContainer::Waves(_) => true,
70            DataContainer::Transactions(_) => false,
71            DataContainer::Empty => false,
72        }
73    }
74
75    pub fn is_transactions(&self) -> bool {
76        match self {
77            DataContainer::Waves(_) => false,
78            DataContainer::Transactions(_) => true,
79            DataContainer::Empty => false,
80        }
81    }
82
83    pub fn max_timestamp(&self) -> Option<BigUint> {
84        match self {
85            DataContainer::Waves(w) => w.max_timestamp(),
86            DataContainer::Transactions(t) => t.max_timestamp(),
87            DataContainer::Empty => None,
88        }
89    }
90
91    pub fn root_scopes(&self) -> Vec<ScopeType> {
92        match self {
93            DataContainer::Waves(w) => {
94                let scopes = w.root_scopes();
95                scopes
96                    .iter()
97                    .map(|s| ScopeType::WaveScope(s.clone()))
98                    .collect()
99            }
100            DataContainer::Transactions(_) => {
101                vec![ScopeType::StreamScope(StreamScopeRef::Root)]
102            }
103            DataContainer::Empty => vec![],
104        }
105    }
106
107    pub fn scope_exists(&self, scope: &ScopeType) -> bool {
108        match (self, scope) {
109            (DataContainer::Waves(waves), WaveScope(scope)) => waves.scope_exists(scope),
110            (DataContainer::Transactions(transactions), StreamScope(scope)) => {
111                transactions.stream_scope_exists(scope)
112            }
113            (_, _) => false,
114        }
115    }
116
117    pub fn scope_names(&self) -> Vec<String> {
118        match self {
119            DataContainer::Waves(w) => w.scope_names(),
120            DataContainer::Transactions(t) => t.stream_names(),
121            DataContainer::Empty => vec![],
122        }
123    }
124
125    pub fn variable_names(&self) -> Vec<String> {
126        match self {
127            DataContainer::Waves(w) => w.variable_names(),
128            DataContainer::Transactions(t) => t.generator_names(),
129            DataContainer::Empty => vec![],
130        }
131    }
132
133    pub fn variables_in_scope(&self, scope: &ScopeType) -> Vec<VariableType> {
134        match (self, scope) {
135            (DataContainer::Waves(w), WaveScope(s)) => {
136                let variables = w.variables_in_scope(s);
137                variables
138                    .iter()
139                    .map(|v| VariableType::Variable(v.clone()))
140                    .collect()
141            }
142            (DataContainer::Transactions(t), StreamScope(s)) => {
143                let variables = t.generators_in_stream(s);
144                variables
145                    .iter()
146                    .map(|g| VariableType::Generator(g.clone()))
147                    .collect()
148            }
149            _ => panic!("Container and Scope are of incompatible types"),
150        }
151    }
152
153    pub fn metadata(&self) -> MetaData {
154        match self {
155            DataContainer::Waves(w) => w.metadata(),
156            DataContainer::Transactions(t) => t.metadata(),
157            DataContainer::Empty => MetaData {
158                date: None,
159                version: None,
160                timescale: TimeScale {
161                    unit: TimeUnit::None,
162                    multiplier: None,
163                },
164            },
165        }
166    }
167
168    pub fn body_loaded(&self) -> bool {
169        match self {
170            DataContainer::Waves(w) => w.body_loaded(),
171            DataContainer::Transactions(t) => t.body_loaded(),
172            DataContainer::Empty => true,
173        }
174    }
175
176    pub fn is_fully_loaded(&self) -> bool {
177        match self {
178            DataContainer::Waves(w) => w.is_fully_loaded(),
179            DataContainer::Transactions(t) => t.is_fully_loaded(),
180            DataContainer::Empty => true,
181        }
182    }
183
184    pub fn simulation_status(&self) -> Option<SimulationStatus> {
185        match self {
186            DataContainer::Waves(w) => w.simulation_status(),
187            DataContainer::Transactions(_) => None,
188            DataContainer::Empty => None,
189        }
190    }
191}