Skip to main content

smlm_hawk_core/io/version_1/
mod.rs

1use crate::config::{Config as CoreConfig, 
2					Threading as CoreThreading, 
3					Memory as CoreMemory, 
4					RunStyle as CoreRunStyle,
5					NegativeHandling as CoreNegativeHandling,
6					OutputStyle as CoreOutputStyle,
7					AlgorithmConfig as CoreAlgorithm,
8					Validation as CoreValidation};
9
10use serde_json;
11
12use std::fmt::{Display, Formatter, Error as FmtError};
13
14#[derive(Debug, PartialEq, Serialize, Deserialize)]
15pub enum OutputStyle 
16{
17	Sequential,
18	Interleaved,
19}
20
21impl From<CoreOutputStyle> for OutputStyle
22{
23	fn from(os: CoreOutputStyle) -> OutputStyle
24	{
25		match os
26		{
27			CoreOutputStyle::Sequential => Self::Sequential,
28			CoreOutputStyle::Interleaved => Self::Interleaved
29		}
30	}
31}
32
33#[derive(Debug, PartialEq, Serialize, Deserialize)]
34enum NegativeHandling 
35{
36	Absolute,
37	Separate,
38}
39
40impl From<CoreNegativeHandling> for NegativeHandling
41{
42	fn from(nh: CoreNegativeHandling) -> Self
43	{
44		match nh
45		{
46			CoreNegativeHandling::Absolute => Self::Absolute,
47			CoreNegativeHandling::Separate => Self::Separate
48		}
49	}
50}
51
52#[derive(Debug, PartialEq, Serialize, Deserialize)]
53struct AlgorithmConfig 
54{
55	n_levels: u64,
56	output_style: OutputStyle,
57	negative_handling : NegativeHandling
58}
59
60impl AlgorithmConfig
61{
62	pub fn to_algorithm(&self) -> CoreAlgorithm
63	{
64
65		CoreAlgorithm::new(self.n_levels, self.to_core_style(), self.to_core_negative_handling())
66	}
67
68	fn to_core_style(&self) -> CoreOutputStyle
69	{
70		match self.output_style
71		{
72			OutputStyle::Sequential => CoreOutputStyle::Sequential,
73			OutputStyle::Interleaved => CoreOutputStyle::Interleaved
74		}
75	}
76
77	fn to_core_negative_handling(&self) -> CoreNegativeHandling
78	{
79		match self.negative_handling
80		{
81			NegativeHandling::Absolute => CoreNegativeHandling::Absolute,
82			NegativeHandling::Separate => CoreNegativeHandling::Separate
83		}
84	}
85}
86
87impl From<&CoreAlgorithm> for AlgorithmConfig
88{
89	fn from(config: &CoreAlgorithm) -> Self
90	{
91		Self
92		{
93			n_levels: config.n_levels(),
94			output_style : OutputStyle::from(config.output_style()),
95			negative_handling: NegativeHandling::from(config.negative_handling())
96		}
97	}
98}
99
100#[derive(Debug, PartialEq, Serialize, Deserialize)]
101enum RunStyle 
102{
103	InMemory,
104	Stream,
105	PStream,
106}
107
108impl RunStyle
109{
110	pub fn to_runstyle(&self) -> CoreRunStyle
111	{
112		match self
113		{
114			Self::InMemory => CoreRunStyle::InMemory,
115			Self::Stream => CoreRunStyle::Stream,
116			Self::PStream => CoreRunStyle::PStream
117		}
118	}
119}
120
121impl From<&CoreRunStyle> for RunStyle
122{
123	fn from(run_style: &CoreRunStyle) -> Self 
124	{ 
125		match run_style 
126		{
127			CoreRunStyle::InMemory => Self::InMemory,
128			CoreRunStyle::Stream => Self::Stream,
129			CoreRunStyle::PStream => Self::PStream
130		}
131	}
132}
133
134#[derive(Debug, PartialEq, Serialize, Deserialize)]
135enum Memory 
136{
137	Fragmented,
138	Contiguous,
139}
140
141impl Memory
142{
143	pub fn to_memory(&self) -> CoreMemory
144	{
145		match self
146		{
147			Self::Fragmented => CoreMemory::Fragmented,
148			Self::Contiguous => CoreMemory::Contiguous
149		}
150	}
151}
152
153impl From<&CoreMemory> for Memory
154{
155	fn from(memory: &CoreMemory) -> Self
156	{
157		match memory
158		{
159			CoreMemory::Fragmented => Self::Fragmented,
160			CoreMemory::Contiguous => Self::Contiguous
161		}
162	}
163}
164
165#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
166enum Threading 
167{
168	Parallel,
169	Series,
170}
171
172impl Threading
173{
174	pub fn to_threading(&self) -> CoreThreading
175	{
176		match self
177		{
178			Self::Parallel => CoreThreading::parallel(),
179			Self::Series => CoreThreading::series()
180		}
181	}
182}
183
184impl From<&CoreThreading> for Threading
185{
186	fn from(threading: &CoreThreading) -> Self
187	{
188		if threading.is_parallel() {Threading::Parallel} else{Threading::Series}
189	}
190}
191
192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
193pub enum Validation 
194{
195	LimitOutputsToUnder32Bits(bool),
196}
197
198impl Validation
199{
200	fn to_validation(&self) -> CoreValidation
201	{
202		match self
203		{
204			Self::LimitOutputsToUnder32Bits(value) => CoreValidation::LimitOutputsToUnder32Bits(*value)
205		}
206	}
207}
208
209impl From<&CoreValidation> for Validation
210{
211	fn from(validation: &CoreValidation) -> Self
212	{
213		let value = validation.limit_to_under_32();
214		Validation::LimitOutputsToUnder32Bits(value)
215	}
216}
217
218
219#[derive(Debug, PartialEq, Serialize, Deserialize)]
220pub struct Config 
221{
222	threading: Threading,
223	memory: Memory,
224	run_style: RunStyle,
225	algorithm: AlgorithmConfig,
226	validation: Option<Validation>
227}
228
229impl Config
230{
231	fn new(threading: Threading, memory: Memory, run_style: RunStyle, algorithm_config: AlgorithmConfig, validation: Option<Validation>) -> Self
232	{
233		Self{threading, memory, run_style, algorithm: algorithm_config, validation}
234	}
235
236	pub fn to_config(&self) -> CoreConfig
237	{
238		CoreConfig::new(
239			self.threading.to_threading(),
240			self.memory.to_memory(),
241			self.run_style.to_runstyle(),
242			self.algorithm.to_algorithm(),
243			self.validation.as_ref().map(|v| v.to_validation())
244		)
245	}
246
247	pub fn to_json(&self) -> String
248	{
249		match serde_json::to_string(self)
250		{
251			Ok(s) => s,
252			Err(e) => e.to_string()
253		}
254	}
255}
256
257impl From<&CoreConfig> for Config
258{
259	fn from(config: &CoreConfig) -> Self
260	{
261		Self::new(
262			Threading::from(config.threading()),
263			Memory::from(config.memory()),
264			RunStyle::from(config.run_style()),
265			AlgorithmConfig::from(config.algorithm_config()),
266			config.validation().map(Validation::from)
267			)
268	}
269}
270
271impl Display for Config
272{
273	fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>
274	{
275		write!(f, "{}", self.to_json())
276	}
277}
278
279#[cfg(test)]
280mod tests 
281{
282	use super::*;
283
284	const EXPECTED : &str = "{\"threading\":\"Parallel\",\"memory\":\"Contiguous\",\"run_style\":\"InMemory\",\"algorithm\":{\"n_levels\":3,\"output_style\":\"Sequential\",\"negative_handling\":\"Absolute\"},\"validation\":null}";
285	
286	#[test]
287	fn to_string_test()
288	{
289		let config = CoreConfig::default();
290		assert_eq!(Config::from(&config).to_string(), EXPECTED)
291	}
292
293	#[test]
294	fn validation_string_test()
295	{
296		let validation = CoreValidation::LimitOutputsToUnder32Bits(true);
297		let config = CoreConfig::new(CoreThreading::default(), CoreMemory::default(), CoreRunStyle::default(), CoreAlgorithm::default(), Some(validation));
298		let expected = "{\"threading\":\"Parallel\",\"memory\":\"Contiguous\",\"run_style\":\"InMemory\",\"algorithm\":{\"n_levels\":3,\"output_style\":\"Sequential\",\"negative_handling\":\"Absolute\"},\"validation\":{\"LimitOutputsToUnder32Bits\":true}}";
299		assert_eq!(Config::from(&config).to_string(), expected)
300	}
301
302	#[test]
303	fn from_str_test()
304	{
305		let config = serde_json::from_str::<Config>(EXPECTED).unwrap();
306		let expected = Config::from(&CoreConfig::default());
307		assert_eq!(config, expected);
308	}
309}