First commit

This commit is contained in:
Maarifa Maarifa
2023-01-17 02:26:35 +03:00
commit e7e25b7e43
7 changed files with 989 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

390
Cargo.lock generated Normal file
View File

@@ -0,0 +1,390 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d"
[[package]]
name = "clap"
version = "4.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39"
dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"is-terminal",
"once_cell",
"strsim",
"termcolor",
]
[[package]]
name = "clap_derive"
version = "4.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"libc",
]
[[package]]
name = "io-lifetimes"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ron"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300a51053b1cb55c80b7a9fde4120726ddf25ca241a1cbb926626f62fb136bff"
dependencies = [
"base64",
"bitflags",
"serde",
]
[[package]]
name = "rustix"
version = "0.36.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "serde"
version = "1.0.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "series-troxide"
version = "0.0.1"
dependencies = [
"anyhow",
"clap",
"ron",
"serde",
"thiserror",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
[[package]]
name = "windows_i686_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
[[package]]
name = "windows_i686_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"

21
Cargo.toml Normal file
View File

@@ -0,0 +1,21 @@
[package]
name = "series-troxide"
authors = ["Maarifa Maarifa"]
description = "A Series Tracker with powerfull command-line options"
categories = ["command-line-utilities", "series", "TV"]
repository = "https://github.com/MaarifaMaarifa/series-troxide"
version = "0.0.1"
edition = "2021"
[profile.release]
lto = true
strip = true
codegen-units = 1
panic = "abort"
[dependencies]
thiserror = "1"
anyhow = "1"
ron = "0"
serde = { version = "1", features = ["derive"] }
clap = { version = "4", features = ["derive"] }

23
README.md Normal file
View File

@@ -0,0 +1,23 @@
# Series Troxide
A Series Tracker with powerfull command-line options written in Rust.
## WARNING
Series Troxide is in the very early stages of development. APIs can and will change.
## Design Goals
- **Simple and useful GUI**: Simple for anyone to use (**Work in progress**).
- **Powerfull command-line options**: Terminal-centric folks won't have to leave the terminal.
## Installation
Series Troxide can be installed via the following shell commands assuming you have Cargo and Rustc setup on your machine. You can check the [guide](https://rustup.rs/) incase you're not setup.
```shell
git clone https://github.com/MaarifaMaarifa/series-troxide
cd series-troxide
cargo install --path .
```

143
src/args.rs Normal file
View File

@@ -0,0 +1,143 @@
pub use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(about, version, author)]
pub struct Cli {
#[clap(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
/// Perform actions related to series
Series(SeriesCli),
/// Add season into a series
AddSeason(AddSeasonCli),
/// Add episode into a series
AddEpisode(AddEpisodeCli),
/// Remove season from a series
RemoveSeason(RemoveSeasonCli),
/// Remove episode from a series
RemoveEpisode(RemoveEpisodeCli),
/// Remove a whole series
RemoveSeries(RemoveSeriesCli),
}
#[derive(Parser)]
pub struct AddSeasonCli {
/// Series name to add the season to
pub series: String,
/// Season number or range to be added
pub season: u32,
}
#[derive(Parser)]
pub struct RemoveSeasonCli {
/// Series name to remove season from
pub series: String,
/// Season number or range to be removed
pub season: u32,
}
#[derive(Parser)]
pub struct AddEpisodeCli {
/// Series name to add the episode to
pub series: String,
/// Season number associated
pub season: u32,
/// The episode number or range to be added
pub episode: u32,
}
#[derive(Parser)]
pub struct RemoveEpisodeCli {
/// Series name to remove episode from
pub series: String,
/// Season number associated
pub season: u32,
/// The episode number or range to be removed
pub episode: u32,
}
#[derive(Parser)]
pub struct RemoveSeriesCli {
/// The name of the series to remove
pub series_name: String,
}
#[derive(Parser)]
pub struct SeriesCli {
#[clap(subcommand)]
pub command: SeriesCommand,
}
#[derive(Subcommand)]
pub enum SeriesCommand {
/// List all the current tracked Series
List(ListCli),
/// Add series to the collection
Add(SeriesAddCli),
/// Get the summary of the specified series
Summary(SeriesSummaryCli),
/// Get the total watch time of all series
GetTotalWatchTime(WatchTimeCli),
}
#[derive(Parser)]
pub struct ListCli {
#[clap(subcommand)]
pub sort_command: series_troxide::SeriesSort,
}
#[derive(Parser)]
pub struct SeriesAddCli {
/// The name of the series
pub name: String,
/// The duration of episode in minutes
pub episode_duration: u32,
}
#[derive(Parser)]
pub struct WatchTimeCli {
#[clap(subcommand)]
pub watch_time_command: WatchTimeCommand,
}
#[derive(Parser)]
pub struct SeriesSummaryCli {
/// Series' name
pub name: String,
}
#[derive(Subcommand, Clone)]
pub enum WatchTimeCommand {
/// Watch time in seconds
Seconds,
/// Watch time in minutes
Minutes,
/// Watch time in hours
Hours,
/// Watch time in days
Days,
}

330
src/lib.rs Normal file
View File

@@ -0,0 +1,330 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;
use std::time;
use thiserror::Error;
#[derive(Debug, Error)]
enum SeasonError {
#[error("episode '{0}' does not exist")]
EpisodeNotFound(u32),
#[error("episode '{0}' already exists")]
EpisodeExists(u32),
}
type Episode = u32;
#[derive(Debug, Default, Serialize, Deserialize)]
struct Season {
episodes: HashSet<Episode>,
}
impl Season {
/// Adds an episode into a season
fn add_episode(&mut self, episode: Episode) -> Result<()> {
if !self.episodes.insert(episode) {
return Err(anyhow!(SeasonError::EpisodeExists(episode)));
};
Ok(())
}
/// Removes an episode from a season
fn remove_episode(&mut self, episode: Episode) -> Result<()> {
if !self.episodes.remove(&episode) {
return Err(anyhow!(SeasonError::EpisodeNotFound(episode)));
};
Ok(())
}
/// Get the total number of episodes in a season
fn get_total_episodes(&self) -> usize {
self.episodes.len()
}
}
#[derive(Debug, Error)]
enum SeriesError {
#[error("season '{0}' does not exist")]
SeasonNotFound(u32),
#[error("season '{0}' already exists")]
SeasonAlreadyExists(u32),
// #[error("no available seasons currently assigned")]
// EmptySeasons,
}
/// Struct Representing a Watched Series with it's name, episode
/// duration and it's seasons
#[derive(Debug, Serialize, Deserialize)]
pub struct Series {
name: String,
episode_duration: u32, // Episode duration in minutes
seasons: HashMap<u32, Season>, // hashmap for series number and series pair
}
impl Series {
/// Creates a new instance of Series, initialized with it's
/// name and episode duration
pub fn new(name: String, episode_duration: u32) -> Self {
Self {
name,
episode_duration,
seasons: HashMap::new(),
}
}
/// Adds a new season into the series
pub fn add_season(&mut self, season_number: u32) -> Result<()> {
if self.seasons.contains_key(&season_number) {
return Err(anyhow!(SeriesError::SeasonAlreadyExists(season_number)));
}
let season = Season::default();
self.seasons.insert(season_number, season);
Ok(())
}
/// Removes the given season from the series instance, returning an error if the
/// season does not exist
pub fn remove_season(&mut self, season_number: u32) -> Result<()> {
if self.seasons.remove(&season_number).is_none() {
return Err(anyhow!(SeriesError::SeasonNotFound(season_number)));
}
Ok(())
}
/// Adds an episode on the given season
///
/// # Errors
/// This function returns an error when the season is not found
/// or when no seasons are assigned for the Series
pub fn add_episode(&mut self, season_number: u32, episode_number: u32) -> Result<()> {
if let Some(season) = self.seasons.get_mut(&season_number) {
season.add_episode(episode_number)?;
} else {
return Err(anyhow!(SeriesError::SeasonNotFound(season_number)));
}
Ok(())
}
/// Removes an episode on the given season
pub fn remove_episode(&mut self, season_number: u32, episode_number: u32) -> Result<()> {
if let Some(season) = self.seasons.get_mut(&season_number) {
season.remove_episode(episode_number)?;
} else {
return Err(anyhow!(SeriesError::SeasonNotFound(season_number)));
}
Ok(())
}
/// Get total episodes in the series
pub fn get_total_episodes(&self) -> usize {
self.seasons
.values()
.map(|season| season.get_total_episodes())
.sum()
}
/// Get total watch time in the series
pub fn get_total_watch_time(&self) -> time::Duration {
time::Duration::from_secs(
self.get_total_episodes() as u64 * self.episode_duration as u64 * 60,
)
}
/// Get total seasons in the series
pub fn get_total_seasons(&self) -> usize {
self.seasons.len()
}
}
impl PartialEq for Series {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
/// Enum providing different ways to sort series in the collection
#[derive(clap::Subcommand)]
pub enum SeriesSort {
/// Unsorted
Default,
/// Sort based on watch time
WatchTime,
/// Sort based on watch time but reversed
WatchTimeRev,
/// Sort based on episode count
EpisodeCount,
/// Sort based on episode count but reversed
EpisodeCountRev,
/// Sort based on season count
SeasonCount,
/// Sort based on season count but reversed
SeasonCountRev,
}
#[derive(Debug, Error)]
pub enum SeriesCollectionError {
#[error("series '{0}' already exists")]
SeriesAlreadyExists(String),
#[error("series '{0}' does not exist")]
SeriesNotFound(String),
}
/// struct providing the abstraction for the whole series currently tracked
#[derive(Debug, Serialize, Deserialize)]
pub struct SeriesCollection {
collection: Vec<Series>,
}
impl SeriesCollection {
/// Loads series from a ron file and returns Self
pub fn load_series(path: impl AsRef<Path>) -> Result<Self> {
let file_content = fs::read_to_string(path)?;
let series_collection: Self = ron::from_str(&file_content)?;
Ok(series_collection)
}
/// Adds a new series to the collection
pub fn add_series(&mut self, name: String, episode_duration: u32) -> Result<()> {
let series = Series::new(name, episode_duration);
if self.collection.contains(&series) {
return Err(anyhow!(SeriesCollectionError::SeriesAlreadyExists(
series.name
)));
}
self.collection.push(series);
Ok(())
}
/// Removes series from series collection
pub fn remove_series(&mut self, series_name: &str) -> Result<(), SeriesCollectionError> {
if let Some(series_index) = self
.collection
.iter()
.position(|series| series.name == series_name)
{
self.collection.swap_remove(series_index);
return Ok(());
}
Err(SeriesCollectionError::SeriesNotFound(
series_name.to_string(),
))
}
/// Get an immutable reference from the series collection
pub fn get_series(&self, series_name: &str) -> Result<&Series, SeriesCollectionError> {
for series in &self.collection {
if series.name == series_name {
return Ok(series);
}
}
Err(SeriesCollectionError::SeriesNotFound(
series_name.to_string(),
))
}
/// Get a mutable reference from the series collection
pub fn get_series_mut(
&mut self,
series_name: &str,
) -> Result<&mut Series, SeriesCollectionError> {
for series in &mut self.collection {
if series.name == series_name {
return Ok(series);
}
}
Err(SeriesCollectionError::SeriesNotFound(
series_name.to_string(),
))
}
/// Get names of series based on different sorting
pub fn get_series_names_sorted(&mut self, sort: SeriesSort) -> Vec<&String> {
match sort {
SeriesSort::Default => { /* Do nothing as no sort specified */ }
SeriesSort::WatchTime => {
self.collection.sort_by_key(|a| a.get_total_watch_time());
}
SeriesSort::WatchTimeRev => {
self.collection
.sort_by_key(|a| std::cmp::Reverse(a.get_total_watch_time()));
}
SeriesSort::EpisodeCount => {
self.collection.sort_by_key(|a| a.get_total_episodes());
}
SeriesSort::EpisodeCountRev => {
self.collection
.sort_by_key(|a| std::cmp::Reverse(a.get_total_episodes()));
}
SeriesSort::SeasonCount => {
self.collection.sort_by_key(|a| a.get_total_seasons());
}
SeriesSort::SeasonCountRev => {
self.collection
.sort_by_key(|a| std::cmp::Reverse(a.get_total_seasons()));
}
}
self.collection.iter().map(|series| &series.name).collect()
}
/// Get the total watch time of the whole series
pub fn get_total_watch_time(&self) -> time::Duration {
self.collection
.iter()
.map(|series| series.get_total_watch_time())
.sum()
}
/// Get summary of the given series name
pub fn get_summary(&self, series_name: &str) -> Result<String> {
let series = self.get_series(series_name)?;
let mut season_episodes: Vec<(_, _)> = series .seasons .iter() .map(|(season, episode)| (season, episode.get_total_episodes())) .collect();
season_episodes.sort_by_key(|x| x.0);
let mut summary = format!(
"\
Series Name: {}
Episode Duration: {} mins
Total Seasons: {}
Total Episodes: {}",
series_name,
series.episode_duration,
series.get_total_seasons(),
series.get_total_episodes(),
);
// Appending the {season} => {episode} information to the summary
for (season, episode) in season_episodes {
summary.push_str(&format!("\nSeason {} => {} Episodes", season, episode));
}
Ok(summary)
}
/// Saves the series collection into the ron file
pub fn save_file(&self, path: impl AsRef<Path>) -> Result<()> {
let config = ron::ser::PrettyConfig::new().depth_limit(4);
let file_contents = ron::ser::to_string_pretty(&self, config)?;
fs::write(path, file_contents)?;
Ok(())
}
}

81
src/main.rs Normal file
View File

@@ -0,0 +1,81 @@
mod args;
use anyhow::{Context, Result};
use args::*;
use series_troxide::*;
const SERIES_DATABASE_PATH: &str = "series.ron";
fn main() -> Result<()> {
let cli = Cli::parse();
let mut series_collection = SeriesCollection::load_series(SERIES_DATABASE_PATH).context("Failed to load the database")?;
match cli.command {
Command::AddSeason(add_season_cli) => {
let series = series_collection.get_series_mut(&add_season_cli.series)?;
series
.add_season(add_season_cli.season)
.context("Could not add season")?;
}
Command::AddEpisode(add_episode_cli) => {
let series = series_collection.get_series_mut(&add_episode_cli.series)?;
series
.add_episode(add_episode_cli.season, add_episode_cli.episode)
.context("Could not add episode")?;
}
Command::RemoveSeason(remove_season_cli) => {
let series = series_collection.get_series_mut(&remove_season_cli.series)?;
series
.remove_season(remove_season_cli.season)
.context("Could not remove season")?;
}
Command::RemoveEpisode(remove_episode_cli) => {
let series = series_collection.get_series_mut(&remove_episode_cli.series)?;
series
.remove_episode(remove_episode_cli.season, remove_episode_cli.episode)
.context("Could not remove episode")?;
}
Command::RemoveSeries(remove_series_cli) => {
series_collection
.remove_series(&remove_series_cli.series_name)
.context("Could not remove series")?;
}
Command::Series(series_cli) => match series_cli.command {
SeriesCommand::GetTotalWatchTime(watch_time_cli) => {
match watch_time_cli.watch_time_command {
WatchTimeCommand::Seconds => {
println!("{} seconds", series_collection.get_total_watch_time().as_secs())
},
WatchTimeCommand::Minutes => {
println!("{} minutes", series_collection.get_total_watch_time().as_secs() / 60)
},
WatchTimeCommand::Hours => {
println!("{} hours", series_collection.get_total_watch_time().as_secs() / (60*60))
},
WatchTimeCommand::Days => {
println!("{} days", series_collection.get_total_watch_time().as_secs() / (60*60*24))
},
}
}
SeriesCommand::List(list_cli) => {
series_collection
.get_series_names_sorted(list_cli.sort_command)
.iter()
.for_each(|name| println!("{}", name));
}
SeriesCommand::Add(series_add_cli) => {
series_collection.add_series(series_add_cli.name, series_add_cli.episode_duration).context("Failed to add series")?;
}
SeriesCommand::Summary(series_summary_cli) => {
println!("{}", series_collection.get_summary(&series_summary_cli.name)?);
}
},
}
series_collection
.save_file(SERIES_DATABASE_PATH)
.context("Failed to save the series file")?;
Ok(())
}