diff options
Diffstat (limited to 'yt/src/storage/subscriptions.rs')
-rw-r--r-- | yt/src/storage/subscriptions.rs | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/yt/src/storage/subscriptions.rs b/yt/src/storage/subscriptions.rs index 22edd08..8e089f0 100644 --- a/yt/src/storage/subscriptions.rs +++ b/yt/src/storage/subscriptions.rs @@ -19,7 +19,7 @@ use sqlx::query; use url::Url; use yt_dlp::wrapper::info_json::InfoType; -use crate::app::App; +use crate::{app::App, unreachable::Unreachable}; #[derive(Clone, Debug)] pub struct Subscription { @@ -31,6 +31,7 @@ pub struct Subscription { } impl Subscription { + #[must_use] pub fn new(name: String, url: Url) -> Self { Self { name, url } } @@ -38,14 +39,13 @@ impl Subscription { /// Check whether an URL could be used as a subscription URL pub async fn check_url(url: &Url) -> Result<bool> { - let yt_opts = match json!( { + let Value::Object(yt_opts) = json!( { "playliststart": 1, "playlistend": 10, "noplaylist": false, "extract_flat": "in_playlist", - }) { - Value::Object(map) => map, - _ => unreachable!("This is hardcoded"), + }) else { + unreachable!("This is hardcoded"); }; let info = yt_dlp::extract_info(&yt_opts, url, false, false).await?; @@ -55,10 +55,11 @@ pub async fn check_url(url: &Url) -> Result<bool> { Ok(info._type == Some(InfoType::Playlist)) } -#[derive(Default)] +#[derive(Default, Debug)] pub struct Subscriptions(pub(crate) HashMap<String, Subscription>); -pub async fn remove_all_subscriptions(app: &App) -> Result<()> { +/// Remove all subscriptions +pub async fn remove_all(app: &App) -> Result<()> { query!( " DELETE FROM subscriptions; @@ -71,7 +72,7 @@ pub async fn remove_all_subscriptions(app: &App) -> Result<()> { } /// Get a list of subscriptions -pub async fn get_subscriptions(app: &App) -> Result<Subscriptions> { +pub async fn get(app: &App) -> Result<Subscriptions> { let raw_subs = query!( " SELECT * @@ -88,7 +89,7 @@ pub async fn get_subscriptions(app: &App) -> Result<Subscriptions> { sub.name.clone(), Subscription::new( sub.name, - Url::parse(&sub.url).expect("This should be valid"), + Url::parse(&sub.url).unreachable("It was an URL, when we inserted it."), ), ) }) @@ -117,6 +118,8 @@ pub async fn add_subscription(app: &App, sub: &Subscription) -> Result<()> { Ok(()) } +/// # Panics +/// Only if assertions fail pub async fn remove_subscription(app: &App, sub: &Subscription) -> Result<()> { let output = query!( " |