refactor conf

This commit is contained in:
Marc Planard 2023-06-04 17:33:28 +02:00
parent 61ede78868
commit edba874ae7
6 changed files with 69 additions and 37 deletions

View File

@ -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"

View File

@ -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(())
}

View File

@ -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<u8>,
pub dac_url: Option<String>,
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 {

View File

@ -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"

View File

@ -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"

View File

@ -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")));
}