From 63897f6a63c7d33400a335c7f6dc6945fe070bae Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Fri, 3 Apr 2026 14:17:58 +0200 Subject: [PATCH] Minor clean up --- src/main.rs | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 31bab27..ac0006f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,7 @@ enum Source { Other, } +/// Returns the tweet ID when `id` is non-empty and contains only ASCII digits. fn parse_tweet_id(id: &str) -> Option { if !id.is_empty() && id.chars().all(|char| char.is_ascii_digit()) { Some(id.to_string()) @@ -91,15 +92,16 @@ fn parse_tweet_id(id: &str) -> Option { } } -fn tweet_id_from_path(path: &str) -> Option { - path.split(':').next_back().and_then(parse_tweet_id) -} - -fn resolve_source_path(path: &str, source: &Source) -> String { +// TODO: Get rid of this somehow, probably encoding the ID logic into a struct. +// TODO: Error handling for inputs? +fn expand_shorthand_to_url(path: &str, source: &Source) -> String { if *source == Source::X && path.starts_with("tweet:media:") { format!( "https://x.com/i/status/{}", - tweet_id_from_path(path).unwrap() + path.split(':') + .next_back() + .and_then(parse_tweet_id) + .unwrap() ) } else { path.to_string() @@ -394,7 +396,7 @@ fn main() -> Result<()> { } // Sources, for which yt-dlp is needed - let path = resolve_source_path(path, &source); + let path = expand_shorthand_to_url(path, &source); let hash = match source { Source::YouTubeVideo | Source::X @@ -601,31 +603,14 @@ mod tests { } } - #[test] - fn test_tweet_id_from_path() { - assert_eq!( - tweet_id_from_path("tweet:1234567890"), - Some("1234567890".to_string()) - ); - assert_eq!( - tweet_id_from_path("tweet:media:1234567890"), - Some("1234567890".to_string()) - ); - assert_eq!( - tweet_id_from_path("x:thread:1234567890"), - Some("1234567890".to_string()) - ); - assert_eq!(tweet_id_from_path("tweet:not-a-number"), None); - } - #[test] fn test_resolve_source_path() { assert_eq!( - resolve_source_path("tweet:media:1234567890", &Source::X), + expand_shorthand_to_url("tweet:media:1234567890", &Source::X), "https://x.com/i/status/1234567890" ); assert_eq!( - resolve_source_path("tweet:1234567890", &Source::Tweet), + expand_shorthand_to_url("tweet:1234567890", &Source::Tweet), "tweet:1234567890" ); }