fabaccess-bffh/src/config.rs

87 lines
2.5 KiB
Rust
Raw Normal View History

2020-12-12 13:58:04 +01:00
use std::default::Default;
2020-02-14 12:20:17 +01:00
use std::str::FromStr;
2020-02-18 16:55:19 +01:00
use std::path::{Path, PathBuf};
use std::io::Read;
2020-12-12 13:58:04 +01:00
use std::fs;
2020-09-15 14:31:10 +02:00
use std::collections::HashMap;
2020-02-18 16:55:19 +01:00
2020-12-12 13:58:04 +01:00
use serde::{Serialize, Deserialize};
2020-09-15 14:31:10 +02:00
2020-12-12 13:58:04 +01:00
use crate::error::Result;
use crate::machine::MachineDescription;
use crate::db::machine::MachineIdentifier;
2020-09-15 14:31:10 +02:00
2020-12-12 13:58:04 +01:00
pub fn read(path: &Path) -> Result<Config> {
2020-12-14 11:02:46 +01:00
serde_dhall::from_file(path)
.parse()
.map_err(Into::into)
2020-02-14 12:20:17 +01:00
}
2020-12-12 13:58:04 +01:00
#[deprecated]
pub type Settings = Config;
#[derive(Debug, Clone, Serialize, Deserialize)]
2020-12-12 13:58:04 +01:00
pub struct Config {
/// A list of address/port pairs to listen on.
// TODO: This should really be a variant type; that is something that can figure out itself if
// it contains enough information to open a socket (i.e. it checks if it's a valid path (=>
// Unix socket) or IPv4/v6 address)
2020-09-15 14:31:10 +02:00
pub listens: Box<[Listen]>,
2020-02-14 12:20:17 +01:00
2020-12-12 13:58:04 +01:00
/// Machine descriptions to load
pub machines: HashMap<MachineIdentifier, MachineDescription>,
2020-12-14 11:02:46 +01:00
/// Actors to load and their configuration options
pub actors: HashMap<String, ModuleConfig>,
/// Initiators to load and their configuration options
pub initiators: HashMap<String, ModuleConfig>,
pub mqtt_url: String,
2020-02-14 12:20:17 +01:00
}
2020-02-18 16:55:19 +01:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Listen {
pub address: String,
pub port: Option<u16>,
}
2020-12-14 11:02:46 +01:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleConfig {
pub name: String,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub params: HashMap<String, String>
}
impl Default for Config {
2020-02-18 16:55:19 +01:00
fn default() -> Self {
2020-12-14 11:02:46 +01:00
let mut actors: HashMap::<String, ModuleConfig> = HashMap::new();
let mut initiators: HashMap::<String, ModuleConfig> = HashMap::new();
actors.insert("Actor".to_string(), ModuleConfig {
name: "Shelly".to_string(),
params: HashMap::new(),
});
initiators.insert("Initiator".to_string(), ModuleConfig {
name: "TCP-Listen".to_string(),
params: HashMap::new(),
});
2020-12-12 13:58:04 +01:00
Config {
2020-12-14 11:02:46 +01:00
listens: Box::new([
Listen {
address: "localhost".to_string(),
port: Some(DEFAULT_PORT),
}
]),
2020-12-12 13:58:04 +01:00
machines: HashMap::new(),
2020-12-14 11:02:46 +01:00
actors: actors,
initiators: initiators,
mqtt_url: "tcp://localhost:1883".to_string(),
2020-02-18 16:55:19 +01:00
}
}
}
// The default port in the non-assignable i.e. free-use area
pub const DEFAULT_PORT: u16 = 59661;