tnp_extensions/lib.rs
1#![forbid(unsafe_code)]
2#![warn(missing_docs)]
3//! A simple crate to provide some convenience methods for the
4// crate.
5//!```rust
6//! use torrent_name_parser::Metadata;
7//! use tnp_extensions::TNPExtensions;
8//!
9//! let m = Metadata::from("narcos.s01e10.1080p.bluray.x264-rovers").unwrap();
10//! println!("first Ep:{}", m.first_episode().unwrap());
11//! println!("last Epi:{}", m.last_episode().unwrap());
12//! println!("is_multi: {}", m.is_mutli_episodes());
13//!
14//! let m = Metadata::from("narcos.s01e10e11.1080p.bluray.x264-rovers.srt").unwrap();
15//! println!("first Ep:{}", m.first_episode().unwrap());
16//! println!("last Epi:{}", m.last_episode().unwrap());
17//! println!("is_multi: {}", m.is_mutli_episodes());
18//! println!("is_subtitle: {}", m.is_subtitle());
19//!```
20#[cfg(test)]
21mod tests;
22
23/// A trait containing convenience methods
24pub trait TNPExtensions {
25 /// Determine if the file is subtitle based on file extension
26 fn is_subtitle(&self) -> bool;
27 /// Determine if the torrent name contains multiple episodes.
28 fn is_mutli_episodes(&self) -> bool;
29 /// Provide access to the first episode number
30 fn first_episode(&self) -> Option<&i32>;
31 /// Provide access to the last episode number
32 fn last_episode(&self) -> Option<&i32>;
33}
34impl TNPExtensions for torrent_name_parser::Metadata {
35 fn is_subtitle(&self) -> bool {
36 match self.extension() {
37 Some(ext) => match ext.to_ascii_lowercase().as_str() {
38 "srt" | "ssa" | "ttml" | "sbv" | "dfxp" | "vtt" => true,
39 _ => false,
40 },
41 None => false,
42 }
43 }
44 fn is_mutli_episodes(&self) -> bool {
45 self.episodes().len() > 1
46 }
47 fn first_episode(&self) -> Option<&i32> {
48 if !self.episodes().is_empty() {
49 return Some(&self.episodes()[0]);
50 };
51 None
52 }
53 fn last_episode(&self) -> Option<&i32> {
54 self.episodes().last()
55 }
56}