diff options
| author | Jokler <jokler@protonmail.com> | 2020-03-14 17:57:48 +0100 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-03-14 18:13:43 +0100 |
| commit | 778f5216d269e7c861be706ce65b778b3db86f7c (patch) | |
| tree | 561c4196b6f7dd28bc5943dcbb0c8448f90c2f27 /src/command.rs | |
| parent | 6f9806abdbfa61583b25fd544c41a017a574686c (diff) | |
| download | pokebot-778f5216d269e7c861be706ce65b778b3db86f7c.tar.gz pokebot-778f5216d269e7c861be706ce65b778b3db86f7c.zip | |
Add a way of making relative volume changes
Additionally this moves seeking into the command parsing for
consistency.
Diffstat (limited to 'src/command.rs')
| -rw-r--r-- | src/command.rs | 72 |
1 files changed, 70 insertions, 2 deletions
diff --git a/src/command.rs b/src/command.rs index db22028..35070b8 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,5 +1,8 @@ +use std::time::Duration; + use structopt::clap::AppSettings::*; use structopt::StructOpt; +use log::debug; #[derive(StructOpt, Debug)] #[structopt( @@ -20,7 +23,7 @@ pub enum Command { /// Pauses audio playback Pause, /// Seeks by a specified amount - Seek { amount: String }, + Seek { amount: Seek }, /// Stops audio playback Stop, /// Switches to the next queue entry @@ -29,7 +32,72 @@ pub enum Command { /// Clears the playback queue Clear, /// Changes the volume to the specified value - Volume { percent: f64 }, + Volume { volume: VolumeChange }, /// Leaves the channel Leave, } + +#[derive(Copy, Clone, Debug)] +pub enum Seek { + Positive(Duration), + Negative(Duration), + Absolute(Duration), +} + +impl std::str::FromStr for Seek { + type Err = humantime::DurationError; + + fn from_str(mut amount: &str) -> std::result::Result<Self, Self::Err> { + let sign = match amount.chars().next() { + Some('+') => 1, + Some('-') => -1, + _ => 0, + }; + let is_relative = sign != 0; + + if is_relative { + amount = &amount[1..]; + } + + let duration = humantime::parse_duration(amount)?; + + match sign { + 1 => Ok(Seek::Positive(duration)), + -1 => Ok(Seek::Negative(duration)), + _ => Ok(Seek::Absolute(duration)), + } + } +} + +#[derive(Copy, Clone, Debug)] +pub enum VolumeChange { + Positive(f64), + Negative(f64), + Absolute(f64), +} + +// TODO This runs twice, report to clap? +impl std::str::FromStr for VolumeChange { + type Err = std::num::ParseFloatError; + + fn from_str(mut amount: &str) -> std::result::Result<Self, Self::Err> { + let sign = match amount.chars().next() { + Some('+') => 1, + Some('-') => -1, + _ => 0, + }; + let is_relative = sign != 0; + + if is_relative { + amount = &amount[1..]; + } + + let amount = f64::from_str(amount)? * 0.01; + + match sign { + 1 => Ok(VolumeChange::Positive(amount)), + -1 => Ok(VolumeChange::Negative(amount)), + _ => Ok(VolumeChange::Absolute(amount)), + } + } +} |
