From edba874ae743f88ebc9c5299ce0dcd111fbae472 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sun, 4 Jun 2023 17:33:28 +0200 Subject: [PATCH] refactor conf --- copyme.Settings.toml | 8 +++---- examples/simple_client.rs | 8 +++++-- src/conf.rs | 32 ++++++++++++++++++---------- tests/settings/valid.toml | 15 +++++++------ tests/settings/valid_etherdream.toml | 15 +++++++++++++ tests/test_conf.rs | 28 +++++++++++++----------- 6 files changed, 69 insertions(+), 37 deletions(-) create mode 100644 tests/settings/valid_etherdream.toml diff --git a/copyme.Settings.toml b/copyme.Settings.toml index 24b446c..262751f 100644 --- a/copyme.Settings.toml +++ b/copyme.Settings.toml @@ -11,10 +11,10 @@ debug = "true" redis_url = "redis://127.0.0.1:6379/" # Either Helios or Etherdream -dac_family = "Helios" - # For Helios. USB Device Id of the DAC -dac_id = 0 +[dac.helios] +id = 0 # For Etherdream. IP of the DAC -dac_url = "192.168.1.68" +# [dac.etherdream] +# url = "192.168.1.68" diff --git a/examples/simple_client.rs b/examples/simple_client.rs index c3ba977..a62c4cc 100644 --- a/examples/simple_client.rs +++ b/examples/simple_client.rs @@ -11,8 +11,12 @@ fn do_something() -> redis::RedisResult<()> { /* do something here */ //let val : String = con.get("/pl/0/0")?; - let _ = con.set("/pl/0/0", "[(150.0, 230.0, 65280), (170.0, 170.0, 65280), (230.0, 170.0, 65280), (210.0, 230.0, 65280), (150.0, 230.0, 65280)]")?; - + let _ = con.set("/pl/0/0", "[(150.0, 230.0, 65280), \ + (170.0, 170.0, 65280), \ + (230.0, 170.0, 65280), \ + (210.0, 230.0, 65280), \ + (150.0, 230.0, 65280)]")?; + Ok(()) } diff --git a/src/conf.rs b/src/conf.rs index 87e85c8..4d64fb2 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,21 +1,31 @@ use config::Config; -use serde::Deserialize; +use serde::{Serialize,Deserialize}; use crate::errors::LJResult; -#[derive(Deserialize, Debug)] -pub enum DacFamily { - Helios, - Etherdream, -} - -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Conf { pub laser_id: u8, pub debug: bool, pub redis_url: String, - pub dac_family: DacFamily, - pub dac_id: Option, - pub dac_url: Option, + pub dac: DacFamily +} + +#[derive(Serialize, Deserialize, Debug)] +pub enum DacFamily { + #[serde(rename = "helios")] + Helios(HeliosConf), + #[serde(rename = "etherdream")] + Etherdream(EtherDreamConf), +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct HeliosConf { + pub id: u8 +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct EtherDreamConf { + pub url: String } impl Conf { diff --git a/tests/settings/valid.toml b/tests/settings/valid.toml index 1d9689c..262751f 100644 --- a/tests/settings/valid.toml +++ b/tests/settings/valid.toml @@ -1,4 +1,5 @@ -# file: valid.toml +# file: Settings.toml +# Rename me ! # The main key of your laser in LJ laser_id = 0 @@ -6,14 +7,14 @@ laser_id = 0 # Activate for more debug debug = "true" -# Redis URL as IP:port -redis_url = "127.0.0.1" +# Redis URL as redis://IP:port/ +redis_url = "redis://127.0.0.1:6379/" # Either Helios or Etherdream -dac_family = "Helios" - # For Helios. USB Device Id of the DAC -dac_id = 0 +[dac.helios] +id = 0 # For Etherdream. IP of the DAC -dac_url = "192.168.1.68" +# [dac.etherdream] +# url = "192.168.1.68" diff --git a/tests/settings/valid_etherdream.toml b/tests/settings/valid_etherdream.toml new file mode 100644 index 0000000..b136ccf --- /dev/null +++ b/tests/settings/valid_etherdream.toml @@ -0,0 +1,15 @@ +# file: Settings.toml +# Rename me ! + +# The main key of your laser in LJ +laser_id = 0 + +# Activate for more debug +debug = "true" + +# Redis URL as redis://IP:port/ +redis_url = "redis://127.0.0.1:6379/" + +# For Etherdream. IP of the DAC +[dac.etherdream] +url = "192.168.1.68" diff --git a/tests/test_conf.rs b/tests/test_conf.rs index 72dc44e..1c7b77b 100644 --- a/tests/test_conf.rs +++ b/tests/test_conf.rs @@ -1,4 +1,4 @@ -use lj_rust::conf::{Conf, DacFamily}; +use lj_rust::conf::{Conf, DacFamily, HeliosConf, EtherDreamConf}; #[test] fn it_loads_a_valid_conf() { @@ -14,19 +14,21 @@ fn it_fails_invalid_conf() { #[test] fn it_finds_struct_fields() { - let config = match Conf::new("tests/settings/valid") { - Ok(c) => c, - Err(err) => { - panic!("Unable to load config file: {:?}", err) - } - }; - assert_eq!(config.laser_id, u8::from(0)); + let config = Conf::new("tests/settings/valid").unwrap(); + assert_eq!(config.laser_id, 0); assert_eq!(config.debug, true); - assert_eq!(config.redis_url, String::from("127.0.0.1")); - assert!(match config.dac_family { - DacFamily::Helios => true, + assert_eq!(config.redis_url, "redis://127.0.0.1:6379/"); + assert!(match config.dac { + DacFamily::Helios(HeliosConf { id: 0 }) => true, + _ => false, + }); +} + +#[test] +fn it_finds_etherdream_fields() { + let config = Conf::new("tests/settings/valid_etherdream").unwrap(); + assert!(match config.dac { + DacFamily::Etherdream(EtherDreamConf { url }) if url == "192.168.1.68" => true, _ => false, }); - assert_eq!(config.dac_id, Some(0)); - assert_eq!(config.dac_url, Some(String::from("192.168.1.68"))); }