62 lines
1.6 KiB
Rust
62 lines
1.6 KiB
Rust
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 => {
|
|
update_config()
|
|
}
|
|
}
|
|
}
|
|
|
|
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)?;
|
|
let conf = Config::read()?;
|
|
episode = conf.get_current_episode()?;
|
|
}
|
|
Ok(())
|
|
} |