use std; use std::collections::HashMap; use toml; pub use tracker::TrackerMode; #[derive(Deserialize)] pub struct UDPConfig { bind_address: String, announce_interval: u32, } impl UDPConfig { pub fn get_address(&self) -> &str { self.bind_address.as_str() } pub fn get_announce_interval(&self) -> u32 { self.announce_interval } } #[derive(Deserialize)] pub struct HTTPConfig { bind_address: String, access_tokens: HashMap, } impl HTTPConfig { pub fn get_address(&self) -> &str { self.bind_address.as_str() } pub fn get_access_tokens(&self) -> &HashMap { &self.access_tokens } } #[derive(Deserialize)] pub struct Configuration { mode: TrackerMode, udp: UDPConfig, http: Option, log_level: Option, db_path: Option, } #[derive(Debug)] pub enum ConfigError { IOError(std::io::Error), ParseError(toml::de::Error), } impl std::fmt::Display for ConfigError { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ConfigError::IOError(e) => e.fmt(formatter), ConfigError::ParseError(e) => e.fmt(formatter), } } } impl std::error::Error for ConfigError {} impl Configuration { pub fn load(data: &[u8]) -> Result { toml::from_slice(data) } pub fn load_file(path: &str) -> Result { match std::fs::read(path) { Err(e) => Err(ConfigError::IOError(e)), Ok(data) => match Self::load(data.as_slice()) { Ok(cfg) => Ok(cfg), Err(e) => Err(ConfigError::ParseError(e)), }, } } pub fn get_mode(&self) -> &TrackerMode { &self.mode } pub fn get_udp_config(&self) -> &UDPConfig { &self.udp } pub fn get_log_level(&self) -> &Option { &self.log_level } pub fn get_http_config(&self) -> &Option { &self.http } pub fn get_db_path(&self) -> &Option { &self.db_path } } impl Default for Configuration { fn default() -> Configuration { Configuration { log_level: None, mode: TrackerMode::DynamicMode, udp: UDPConfig { announce_interval: 120, bind_address: String::from("0.0.0.0:6969"), }, http: None, db_path: None, } } }