diff options
Diffstat (limited to 'mumlib')
| -rw-r--r-- | mumlib/src/command.rs | 24 | ||||
| -rw-r--r-- | mumlib/src/config.rs | 61 | ||||
| -rw-r--r-- | mumlib/src/error.rs | 2 | ||||
| -rw-r--r-- | mumlib/src/lib.rs | 1 |
4 files changed, 64 insertions, 24 deletions
diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index 63dd5f9..28b4d79 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -10,6 +10,8 @@ pub enum Command { ChannelList, ConfigReload, InputVolumeSet(f32), + OutputVolumeSet(f32), + UserVolumeSet(String, f32), ServerConnect { host: String, port: u16, @@ -21,11 +23,27 @@ pub enum Command { DeafenSelf, MuteSelf, MuteOther(String), + ServerStatus { + host: String, + port: u16, + }, } #[derive(Debug, Deserialize, Serialize)] pub enum CommandResponse { - ChannelList { channels: Channel }, - ServerConnect { welcome_message: Option<String> }, - Status { server_state: Server }, + ChannelList { + channels: Channel, + }, + ServerConnect { + welcome_message: Option<String>, + }, + Status { + server_state: Server, + }, + ServerStatus { + version: u32, + users: u32, + max_users: u32, + bandwidth: u32, + }, } diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index e6b97fd..3a2fa27 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,6 +1,8 @@ +use crate::DEFAULT_PORT; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs; +use std::net::{SocketAddr, ToSocketAddrs}; use std::path::Path; use toml::value::Array; use toml::Value; @@ -13,8 +15,8 @@ struct TOMLConfig { #[derive(Clone, Debug, Default)] pub struct Config { - pub audio: Option<AudioConfig>, - pub servers: Option<Vec<ServerConfig>>, + pub audio: AudioConfig, + pub servers: Vec<ServerConfig>, } impl Config { @@ -44,9 +46,10 @@ impl Config { } } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct AudioConfig { pub input_volume: Option<f32>, + pub output_volume: Option<f32>, } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -58,6 +61,18 @@ pub struct ServerConfig { pub password: Option<String>, } +impl ServerConfig { + pub fn to_socket_addr(&self) -> Option<SocketAddr> { + match (self.host.as_str(), self.port.unwrap_or(DEFAULT_PORT)) + .to_socket_addrs() + .map(|mut e| e.next()) + { + Ok(Some(addr)) => Some(addr), + _ => None, + } + } +} + pub fn get_cfg_path() -> String { if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { let path = format!("{}/mumdrc", var); @@ -113,7 +128,7 @@ impl TryFrom<TOMLConfig> for Config { fn try_from(config: TOMLConfig) -> Result<Self, Self::Error> { Ok(Config { - audio: config.audio, + audio: config.audio.unwrap_or_default(), servers: config .servers .map(|servers| { @@ -122,7 +137,8 @@ impl TryFrom<TOMLConfig> for Config { .map(|s| s.try_into::<ServerConfig>()) .collect() }) - .transpose()?, + .transpose()? + .unwrap_or(Vec::new()), }) } } @@ -130,26 +146,29 @@ impl TryFrom<TOMLConfig> for Config { impl From<Config> for TOMLConfig { fn from(config: Config) -> Self { TOMLConfig { - audio: config.audio, - servers: config.servers.map(|servers| { - servers + audio: if config.audio.output_volume.is_some() || config.audio.input_volume.is_some() { + Some(config.audio) + } else { + None + }, + servers: Some( + config + .servers .into_iter() .map(|s| Value::try_from::<ServerConfig>(s).unwrap()) - .collect() - }), + .collect(), + ), } } } -pub fn read_default_cfg() -> Option<Config> { - Some( - Config::try_from( - toml::from_str::<TOMLConfig>(&match fs::read_to_string(get_cfg_path()) { - Ok(f) => f.to_string(), - Err(_) => return None, - }) - .expect("invalid TOML in config file"), //TODO - ) - .expect("invalid config in TOML"), - ) //TODO +pub fn read_default_cfg() -> Config { + Config::try_from( + toml::from_str::<TOMLConfig>(&match fs::read_to_string(get_cfg_path()) { + Ok(f) => f, + Err(_) => return Config::default(), + }) + .expect("invalid TOML in config file"), //TODO + ) + .expect("invalid config in TOML") //TODO } diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index e8b0323..c9eff52 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -11,6 +11,7 @@ pub enum Error { ChannelIdentifierError(String, ChannelIdentifierError), InvalidUserIdentifierError(String), InvalidServerAddrError(String, u16), + InvalidUsernameError(String), } impl Display for Error { @@ -21,6 +22,7 @@ impl Display for Error { Error::ChannelIdentifierError(id, kind) => write!(f, "{}: {}", kind, id), Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}: {}", addr, port), Error::InvalidUserIdentifierError(name) => write!(f, "Invalid username: {}", name), + Error::InvalidUsernameError(username) => write!(f, "Invalid username: {}", username), } } } diff --git a/mumlib/src/lib.rs b/mumlib/src/lib.rs index a54990e..439efa9 100644 --- a/mumlib/src/lib.rs +++ b/mumlib/src/lib.rs @@ -7,6 +7,7 @@ use colored::*; use log::*; pub const SOCKET_PATH: &str = "/var/tmp/mumd"; +pub const DEFAULT_PORT: u16 = 64738; pub fn setup_logger<T: Into<fern::Output>>(target: T, color: bool) { fern::Dispatch::new() |
