Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.
Rust downloadmanagerguihacktoberfesthomebrewlightweightlinuxlinux-distributionlocal-firstmacosmacos-packagemedia-toolboxopen-sourcerustsveltetauritrendingtrending-repositorieswindowswindows-desktopytdlp
feat: add Platform enum with display and parsing implementations for media platforms
Tonho83a910edc2d1
diff --git a/src-tauri/src/platforms/mod.rs b/src-tauri/src/platforms/mod.rsindex 53662cc8..b0b8b79f 100644--- a/src-tauri/src/platforms/mod.rs+++ b/src-tauri/src/platforms/mod.rs@@ -1,2 +1,103 @@pub mod traits;pub mod hotmart;++use std::fmt;+use std::str::FromStr;+use serde::{Deserialize, Serialize};++#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]+#[serde(rename_all = "lowercase")]+pub enum Platform {+ Hotmart,+ YouTube,+ Instagram,+ TikTok,+ Twitter,+ Reddit,+ Facebook,+ Twitch,+ Vimeo,+}++impl fmt::Display for Platform {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let name = match self {+ Platform::Hotmart => "hotmart",+ Platform::YouTube => "youtube",+ Platform::Instagram => "instagram",+ Platform::TikTok => "tiktok",+ Platform::Twitter => "twitter",+ Platform::Reddit => "reddit",+ Platform::Facebook => "facebook",+ Platform::Twitch => "twitch",+ Platform::Vimeo => "vimeo",+ };+ write!(f, "{}", name)+ }+}++impl FromStr for Platform {+ type Err = String;++ fn from_str(s: &str) -> Result<Self, Self::Err> {+ match s.to_lowercase().as_str() {+ "hotmart" => Ok(Platform::Hotmart),+ "youtube" | "yt" => Ok(Platform::YouTube),+ "instagram" | "ig" => Ok(Platform::Instagram),+ "tiktok" | "tt" => Ok(Platform::TikTok),+ "twitter" | "x" => Ok(Platform::Twitter),+ "reddit" => Ok(Platform::Reddit),+ "facebook" | "fb" => Ok(Platform::Facebook),+ "twitch" => Ok(Platform::Twitch),+ "vimeo" => Ok(Platform::Vimeo),+ _ => Err(format!("Unknown platform: {}", s)),+ }+ }+}++impl Platform {+ pub fn from_url(url_str: &str) -> Option<Self> {+ let parsed = url::Url::parse(url_str).ok()?;+ let host = parsed.host_str()?.to_lowercase();++ let matches = |domain: &str| -> bool {+ host == domain || host.ends_with(&format!(".{}", domain))+ };++ if matches("hotmart.com") {+ Some(Platform::Hotmart)+ } else if matches("youtube.com") || matches("youtu.be") {+ Some(Platform::YouTube)+ } else if matches("instagram.com") {+ Some(Platform::Instagram)+ } else if matches("tiktok.com") {+ Some(Platform::TikTok)+ } else if matches("twitter.com") || matches("x.com") {+ Some(Platform::Twitter)+ } else if matches("reddit.com") {+ Some(Platform::Reddit)+ } else if matches("facebook.com") || matches("fb.watch") || matches("fb.com") {+ Some(Platform::Facebook)+ } else if matches("twitch.tv") {+ Some(Platform::Twitch)+ } else if matches("vimeo.com") {+ Some(Platform::Vimeo)+ } else {+ None+ }+ }++ pub fn all() -> &'static [Platform] {+ &[+ Platform::Hotmart,+ Platform::YouTube,+ Platform::Instagram,+ Platform::TikTok,+ Platform::Twitter,+ Platform::Reddit,+ Platform::Facebook,+ Platform::Twitch,+ Platform::Vimeo,+ ]+ }+}