Initial commit.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2020-03-25 03:34:58 +04:00
commit d31a502bad
10 changed files with 741 additions and 0 deletions

61
src/run_modes.rs Normal file
View File

@ -0,0 +1,61 @@
use crate::{Opt, RunMode};
use crate::result::{AppResult, AppError};
use crate::initialization::init_config;
use crate::config::{update_episode, update_config, Config};
use std::process::Command;
pub fn run(opts: Opt) -> AppResult<()> {
let mode = opts.mode.unwrap_or_else(|| RunMode::Play);
match mode {
RunMode::Init => {
init_config()
}
RunMode::Play => {
play()
}
RunMode::Prev => {
update_episode(prev_episode)
}
RunMode::Next => {
update_episode(next_episode)
}
RunMode::Update(options) => {
update_config(options)
}
}
}
pub fn prev_episode(current: usize) -> AppResult<usize> {
if let Some(episode) = current.checked_sub(1) {
Ok(episode)
} else {
Err(AppError::RuntimeError(
String::from("Episode can't be less than zero.")
))
}
}
pub fn next_episode(current: usize) -> AppResult<usize> {
if let Some(episode) = current.checked_add(1) {
Ok(episode)
} else {
Err(AppError::RuntimeError(
String::from("Reached usize limit. Sorry.")
))
}
}
pub fn play() -> AppResult<()> {
let conf = Config::read()?;
let mut episode = conf.get_current_episode()?;
while !episode.is_empty() {
let mut child = Command::new("sh")
.arg("-c")
.arg(conf.command.replace("{}", episode.as_str()))
.spawn()?;
child.wait()?;
update_episode(next_episode)?;
episode = conf.get_current_episode()?;
}
Ok(())
}