From 38b0e405d00490ac61738086de868bf91a07dbb4 Mon Sep 17 00:00:00 2001 From: lov3b Date: Sun, 5 Mar 2023 19:51:21 +0100 Subject: [PATCH] More refactors --- src/getm3u8.rs | 4 +-- src/grandmother.rs | 14 ++++----- src/lib.rs | 6 ++-- src/offlineparser.rs | 42 +++++++++++++++++++++++++++ src/{parser.rs => onlineparser.rs} | 46 ++++-------------------------- src/opt.rs | 3 +- src/playlist.rs | 19 +++--------- 7 files changed, 65 insertions(+), 69 deletions(-) create mode 100644 src/offlineparser.rs rename src/{parser.rs => onlineparser.rs} (71%) diff --git a/src/getm3u8.rs b/src/getm3u8.rs index defa90b..ee9e5b6 100644 --- a/src/getm3u8.rs +++ b/src/getm3u8.rs @@ -28,5 +28,5 @@ pub trait GetPlayPath { fn get_path_to_play(&self, link: Rc) -> Result, String>; } -pub trait M3u8PlayPath: GetM3u8 + GetPlayPath {} -impl M3u8PlayPath for T {} +pub trait Parser: GetM3u8 + GetPlayPath {} +impl Parser for T {} diff --git a/src/grandmother.rs b/src/grandmother.rs index ffc3fca..013b6de 100644 --- a/src/grandmother.rs +++ b/src/grandmother.rs @@ -1,15 +1,15 @@ #[allow(unused_imports)] use crate::GetM3u8; use crate::{ - getm3u8::{M3u8PlayPath, WatchedFind}, - Configuration, OfflineParser, Parser, Playlist, MAX_TRIES, + getm3u8::{Parser, WatchedFind}, + Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES, }; use std::fs; type Error = String; pub struct GrandMother { - pub parser: Box, + pub parser: Box, pub playlist: Option, pub config: Configuration, } @@ -20,8 +20,8 @@ impl GrandMother { let seen_links = config.seen_links.iter().map(|x| x.as_str()).collect(); let playlist = playlist.await?; let playlist_content = playlist.get_saved_or_download().await?; - let parser: Box = - Box::new(Parser::new(&playlist_content, &seen_links).await); + let parser: Box = + Box::new(OnlineParser::new(&playlist_content, &seen_links).await); Ok(Self { parser, @@ -31,7 +31,7 @@ impl GrandMother { } pub fn new_offline(config: Configuration) -> Self { - let parser: Box = Box::new(OfflineParser::new(&config)); + let parser: Box = Box::new(OfflineParser::new(&config)); Self { parser, playlist: None, @@ -69,7 +69,7 @@ impl GrandMother { .iter() .map(|x| x.link.as_str()) .collect(); - self.parser = Box::new(Parser::new(&content, &watched_links).await); + self.parser = Box::new(OnlineParser::new(&content, &watched_links).await); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 7bf07dc..5fb06de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,9 @@ mod downloader; pub mod getm3u8; mod grandmother; mod m3u8; +mod offlineparser; +mod onlineparser; mod opt; -mod parser; mod playlist; use std::io::{stdin, stdout, Stdin, StdoutLock, Write}; @@ -15,8 +16,9 @@ pub use downloader::download_with_progress; pub use getm3u8::{GetM3u8, GetPlayPath, WatchedFind}; pub use grandmother::GrandMother; pub use m3u8::{M3u8, OfflineEntry}; +pub use offlineparser::OfflineParser; +pub use onlineparser::OnlineParser; pub use opt::{Mode, Opt}; -pub use parser::{OfflineParser, Parser}; pub use playlist::Playlist; pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; diff --git a/src/offlineparser.rs b/src/offlineparser.rs new file mode 100644 index 0000000..43960c7 --- /dev/null +++ b/src/offlineparser.rs @@ -0,0 +1,42 @@ +use std::{ops::Deref, rc::Rc}; + +use serde::Serialize; + +use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry}; + +#[derive(Serialize)] +pub struct OfflineParser { + m3u8_items: Rc>, +} +impl OfflineParser { + pub fn new(config: &Configuration) -> Self { + Self { + m3u8_items: config.offlinefile_content.clone(), + } + } +} + +impl Deref for OfflineParser { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &*self.m3u8_items + } +} + +impl GetPlayPath for OfflineParser { + fn get_path_to_play(&self, link: Rc) -> Result, String> { + for offline_entry in &*self.m3u8_items { + if *offline_entry.link == *link { + return Ok(offline_entry.path.clone()); + } + } + Err("Not stored for offline use".to_owned()) + } +} + +impl GetM3u8 for OfflineParser { + fn get_m3u8(&self) -> Vec<&M3u8> { + self.m3u8_items.iter().map(|x| &**x).collect() + } +} diff --git a/src/parser.rs b/src/onlineparser.rs similarity index 71% rename from src/parser.rs rename to src/onlineparser.rs index 8931569..d6f5765 100644 --- a/src/parser.rs +++ b/src/onlineparser.rs @@ -4,11 +4,11 @@ use serde::Serialize; use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry}; -pub struct Parser { +pub struct OnlineParser { m3u8_items: Vec, } -impl Parser { +impl OnlineParser { pub async fn new(m3u_content: &str, watched_links: &Vec<&str>) -> Self { Self { m3u8_items: Self::parse_m3u8(m3u_content, watched_links), @@ -23,14 +23,6 @@ impl Parser { .collect() } - /* - * I know that this is also frowned upon, but it is perfectly safe right here, - * even though the borrowchecker complains - */ - // async fn refresh(&self) { - // unsafe { get_mut_ref(&self.pla) }.forcefully_update().await; - // } - pub async fn forcefully_update(&mut self, content: &str) { let seen_links: &Vec<&str> = &self .m3u8_items @@ -80,7 +72,7 @@ impl Parser { } } -impl Deref for Parser { +impl Deref for OnlineParser { type Target = Vec; fn deref(&self) -> &Self::Target { @@ -88,42 +80,14 @@ impl Deref for Parser { } } -impl GetM3u8 for Parser { +impl GetM3u8 for OnlineParser { fn get_m3u8(&self) -> Vec<&M3u8> { self.m3u8_items.iter().collect() } } -impl GetPlayPath for Parser { +impl GetPlayPath for OnlineParser { fn get_path_to_play<'a>(&'a self, link: Rc) -> Result, String> { Ok(link.clone()) } } -#[derive(Serialize)] -pub struct OfflineParser { - m3u8_items: Rc>, -} -impl OfflineParser { - pub fn new(config: &Configuration) -> Self { - Self { - m3u8_items: config.offlinefile_content.clone(), - } - } -} - -impl GetPlayPath for OfflineParser { - fn get_path_to_play(&self, link: Rc) -> Result, String> { - for offline_entry in &*self.m3u8_items { - if *offline_entry.link == *link { - return Ok(offline_entry.path.clone()); - } - } - Err("Not stored for offline use".to_owned()) - } -} - -impl GetM3u8 for OfflineParser { - fn get_m3u8(&self) -> Vec<&M3u8> { - self.m3u8_items.iter().map(|x| &**x).collect() - } -} diff --git a/src/opt.rs b/src/opt.rs index 168e3b8..e728fe2 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -5,8 +5,7 @@ use structopt::StructOpt; #[structopt(name = "ilovetv")] pub struct Opt { #[structopt(short, long, default_value = "ask")] - /// Choose whether to launch in offlinemode, onlinemode or wheter to ask during startup. - /// In offlinemode it's only possible to watch downloaded entries + /// Possible options: online, offline and ask pub mode: Mode, } diff --git a/src/playlist.rs b/src/playlist.rs index 5f5c132..5aecba4 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,9 +1,4 @@ -use std::{ - fs, - ops::{Deref, DerefMut}, - path::PathBuf, - rc::Rc, -}; +use std::{fs, ops::Deref, path::PathBuf, rc::Rc}; use crate::{download_with_progress, downloader::DualWriter, MAX_TRIES}; @@ -79,10 +74,10 @@ impl Playlist { let url = self .url .as_ref() - .clone() - .ok_or_else(|| String::from("In offline mode"))?; + .ok_or_else(|| String::from("In offline mode"))? + .clone(); - let downloaded = download_with_progress(url, None) + let downloaded = download_with_progress(&url, None) .await .and_then(DualWriter::get_string); if let Ok(content) = downloaded { @@ -102,9 +97,3 @@ impl Deref for Playlist { &self.content } } - -impl DerefMut for Playlist { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.content - } -}