Skip to main content

smlm_hawk_core/io/
mod.rs

1pub use self::version_1::{Config as V1Config};
2
3mod version_1;
4
5use crate::config::{Config as CoreConfig};
6
7use std::fmt::{Display, Formatter, Error as FmtError};
8
9#[derive(Debug, PartialEq, Serialize, Deserialize)]
10pub enum Config
11{
12	Version1(V1Config)
13}
14
15impl Config
16{
17	pub fn to_config(&self) -> CoreConfig
18	{
19		match self
20		{
21			Self::Version1(config) => config.to_config()
22		}
23	}
24
25	pub fn to_json(&self) -> String
26	{
27		match serde_json::to_string(self)
28		{
29			Ok(s) => s,
30			Err(e) => e.to_string()
31		}
32	}
33}
34
35impl From<&CoreConfig> for Config
36{
37	fn from(config: &CoreConfig) -> Self
38	{
39		Self::Version1(V1Config::from(config))
40	}
41}
42
43impl Display for Config
44{
45	fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>
46	{
47		write!(f, "{}", self.to_json())
48	}
49}
50
51#[cfg(test)]
52mod tests 
53{
54	use super::*;
55
56	const EXPECTED : &str = "{\"Version1\":{\"threading\":\"Parallel\",\"memory\":\"Contiguous\",\"run_style\":\"InMemory\",\"algorithm\":{\"n_levels\":3,\"output_style\":\"Sequential\",\"negative_handling\":\"Absolute\"},\"validation\":null}}";
57
58	#[test]
59	fn round_trip() 
60	{
61		let config = Config::from(&CoreConfig::default());
62		assert_eq!(config.to_string(), EXPECTED);
63		let config = serde_json::from_str::<Config>(EXPECTED).unwrap();
64		assert_eq!(config, Config::from(&CoreConfig::default()));
65	}
66}