1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
use super::data::original_data_dir; use anyhow::{anyhow, Result}; #[derive(Debug)] pub struct Episodes { current: usize, count: usize, } impl Episodes { pub fn find_installed() -> Self { let count = Self::count_installed(); Self { current: 0usize, count, } } fn count_installed() -> usize { let mut count = 0; for i in 0..9 { if Self::is_installed(i) { count = i; } } count } fn is_installed(number: usize) -> bool { let data_path = original_data_dir(); for f in super::data::required_file_names() { let f = format!("{}.dn{}", f, number); let file_path = data_path.join(f); match std::fs::File::open(file_path) { Ok(_) => {} Err(_) => { return false; } } } true } pub fn switch_next(&mut self) -> usize { self.current += 1; self.current %= self.count; self.current } pub fn switch_to(&mut self, episode: usize) -> Result<()> { if self.count > episode { self.current = episode; Ok(()) } else { Err(anyhow!( "Episode with number {} not installed", episode + 1 )) } } pub fn current(&self) -> usize { self.current } pub fn count(&self) -> usize { self.count } pub fn file_extension(&self) -> String { format!("dn{}", self.current + 1) } }