aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Queseth <eskilq@kth.se>2020-11-02 21:47:47 +0100
committerEskil Queseth <eskilq@kth.se>2020-11-02 21:47:47 +0100
commit0cb39d13bba3dc5ffa3231e6021066e4191a43a4 (patch)
tree5c4a14dac57fc84e688932783f02d6c06ff0515b
parent46861ce465d6f1d86e80007742a850fd1cfa9bad (diff)
downloadmum-0cb39d13bba3dc5ffa3231e6021066e4191a43a4.tar.gz
refactor and add audio out config command
-rw-r--r--mumctl/src/main.rs109
-rw-r--r--mumd/src/state.rs18
-rw-r--r--mumlib/src/config.rs31
3 files changed, 65 insertions, 93 deletions
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs
index 9471b6a..162616c 100644
--- a/mumctl/src/main.rs
+++ b/mumctl/src/main.rs
@@ -150,6 +150,13 @@ fn main() {
"audio.input_volume" => {
if let Ok(volume) = value.parse() {
send_command(Command::InputVolumeSet(volume)).unwrap();
+ config.audio.input_volume = Some(volume);
+ }
+ }
+ "audio.output_volume" => {
+ if let Ok(volume) = value.parse() {
+ send_command(Command::InputVolumeSet(volume)).unwrap();
+ config.audio.output_volume = Some(volume);
}
}
_ => {
@@ -171,27 +178,25 @@ fn main() {
return;
};
- if let Some(config) = config {
- if !config::cfg_exists() {
- println!(
- "Config file not found. Create one in {}? [Y/n]",
- config::get_creatable_cfg_path()
- );
- let stdin = std::io::stdin();
- let response = stdin.lock().lines().next();
- match response.map(|e| e.map(|e| &e == "Y")) {
- Some(Ok(true)) => {
- config.write_default_cfg(true).unwrap();
- }
- _ => {}
+ if !config::cfg_exists() {
+ println!(
+ "Config file not found. Create one in {}? [Y/n]",
+ config::get_creatable_cfg_path()
+ );
+ let stdin = std::io::stdin();
+ let response = stdin.lock().lines().next();
+ match response.map(|e| e.map(|e| &e == "Y")) {
+ Some(Ok(true)) => {
+ config.write_default_cfg(true).unwrap();
}
- } else {
- config.write_default_cfg(false).unwrap();
+ _ => {}
}
+ } else {
+ config.write_default_cfg(false).unwrap();
}
}
-fn match_server_connect(matches: &clap::ArgMatches<'_>, config: &Option<mumlib::config::Config>) {
+fn match_server_connect(matches: &clap::ArgMatches<'_>, config: &mumlib::config::Config) {
let host = matches.value_of("host").unwrap();
let username = matches.value_of("username");
let port = match matches.value_of("port").map(|e| e.parse()) {
@@ -200,41 +205,35 @@ fn match_server_connect(matches: &clap::ArgMatches<'_>, config: &Option<mumlib::
Some(Ok(v)) => Some(v),
};
if let Some(port) = port {
- let response = match config.as_ref().and_then(|e| {
- e.servers
- .as_ref()
- .and_then(|e| e.iter().find(|e| e.name == host))
- }) {
- Some(config) => {
- let host = config.host.as_str();
- let port = config.port.unwrap_or(port);
- let username = config.username.as_ref().map(|e| e.as_str()).or(username);
+ let (host, port, username) = match config.servers
+ .as_ref()
+ .and_then(|e| e.iter()
+ .find(|e| e.name == host))
+ {
+ Some(server_config) => {
+ let host = server_config.host.as_str();
+ let port = server_config.port.unwrap_or(port);
+ let username = server_config.username.as_ref().map(|e| e.as_str()).or(username);
if username.is_none() {
println!("{} no username specified", "error:".red());
return;
}
- send_command(Command::ServerConnect {
- host: host.to_string(),
- port,
- username: username.unwrap().to_string(),
- accept_invalid_cert: true, //TODO
- })
- .map(|e| (e, host))
+ (host, port, username.unwrap())
}
None => {
if username.is_none() {
println!("{} no username specified", "error:".red());
return;
}
- send_command(Command::ServerConnect {
- host: host.to_string(),
- port,
- username: username.unwrap().to_string(),
- accept_invalid_cert: true, //TODO
- })
- .map(|e| (e, host))
+ (host, port, username.unwrap())
}
};
+ let response = send_command(Command::ServerConnect {
+ host: host.to_string(),
+ port,
+ username: username.to_string(),
+ accept_invalid_cert: true, //TODO
+ }).map(|e| (e, host));
match response {
Ok((e, host)) => {
if let Some(CommandResponse::ServerConnect { welcome_message }) = e {
@@ -253,14 +252,8 @@ fn match_server_connect(matches: &clap::ArgMatches<'_>, config: &Option<mumlib::
fn match_server_config(
matches: &clap::ArgMatches<'_>,
- config: &mut Option<mumlib::config::Config>,
+ config: &mut mumlib::config::Config,
) {
- if config.is_none() {
- *config = Some(mumlib::config::Config::default());
- }
-
- let config = config.as_mut().unwrap();
-
if let Some(server_name) = matches.value_of("server_name") {
if let Some(servers) = &mut config.servers {
let server = servers.iter_mut().find(|s| s.name == server_name);
@@ -365,14 +358,8 @@ fn match_server_config(
fn match_server_rename(
matches: &clap::ArgMatches<'_>,
- config: &mut Option<mumlib::config::Config>,
+ config: &mut mumlib::config::Config,
) {
- if config.is_none() {
- *config = Some(mumlib::config::Config::default());
- }
-
- let config = config.as_mut().unwrap();
-
if let Some(servers) = &mut config.servers {
let prev_name = matches.value_of("prev_name").unwrap();
let next_name = matches.value_of("next_name").unwrap();
@@ -386,14 +373,8 @@ fn match_server_rename(
fn match_server_remove(
matches: &clap::ArgMatches<'_>,
- config: &mut Option<mumlib::config::Config>,
+ config: &mut mumlib::config::Config,
) {
- if config.is_none() {
- *config = Some(mumlib::config::Config::default());
- }
-
- let config = config.as_mut().unwrap();
-
let name = matches.value_of("name").unwrap();
if let Some(servers) = &mut config.servers {
match servers.iter().position(|server| server.name == name) {
@@ -409,13 +390,7 @@ fn match_server_remove(
}
}
-fn match_server_add(matches: &clap::ArgMatches<'_>, config: &mut Option<mumlib::config::Config>) {
- if config.is_none() {
- *config = Some(mumlib::config::Config::default());
- }
-
- let mut config = config.as_mut().unwrap();
-
+fn match_server_add(matches: &clap::ArgMatches<'_>, config: &mut mumlib::config::Config) {
let name = matches.value_of("name").unwrap().to_string();
let host = matches.value_of("host").unwrap().to_string();
// optional arguments map None to None
diff --git a/mumd/src/state.rs b/mumd/src/state.rs
index 2060845..4d8c139 100644
--- a/mumd/src/state.rs
+++ b/mumd/src/state.rs
@@ -39,7 +39,7 @@ pub enum StatePhase {
}
pub struct State {
- config: Option<Config>,
+ config: Config,
server: Option<Server>,
audio: Audio,
@@ -375,16 +375,12 @@ impl State {
}
pub fn reload_config(&mut self) {
- if let Some(config) = mumlib::config::read_default_cfg() {
- self.config = Some(config);
- let config = &self.config.as_ref().unwrap();
- if let Some(audio_config) = &config.audio {
- if let Some(input_volume) = audio_config.input_volume {
- self.audio.set_input_volume(input_volume);
- }
- }
- } else {
- warn!("config file not found");
+ self.config = mumlib::config::read_default_cfg();
+ if let Some(input_volume) = self.config.audio.input_volume {
+ self.audio.set_input_volume(input_volume);
+ }
+ if let Some(output_volume) = self.config.audio.output_volume {
+ self.audio.set_output_volume(output_volume);
}
}
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs
index e6b97fd..a971b2d 100644
--- a/mumlib/src/config.rs
+++ b/mumlib/src/config.rs
@@ -13,7 +13,7 @@ struct TOMLConfig {
#[derive(Clone, Debug, Default)]
pub struct Config {
- pub audio: Option<AudioConfig>,
+ pub audio: AudioConfig,
pub servers: Option<Vec<ServerConfig>>,
}
@@ -44,9 +44,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)]
@@ -113,7 +114,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| {
@@ -130,7 +131,11 @@ impl TryFrom<TOMLConfig> for Config {
impl From<Config> for TOMLConfig {
fn from(config: Config) -> Self {
TOMLConfig {
- audio: config.audio,
+ audio: if config.audio.output_volume.is_some() || config.audio.input_volume.is_some() {
+ Some(config.audio)
+ } else {
+ None
+ },
servers: config.servers.map(|servers| {
servers
.into_iter()
@@ -141,15 +146,11 @@ impl From<Config> for TOMLConfig {
}
}
-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
}