use crate::transcript::error::{Result, TranscriptError};
use crate::transcript::source::TranscriptSource;
use crate::transcript::sources::youtube::Youtube;
pub fn detect(url: &str) -> Result<Box<dyn TranscriptSource>> {
if Youtube::matches(url) {
return Ok(Box::new(Youtube::new()?));
}
Err(TranscriptError::InvalidLocator(format!(
"no transcript source recognises `{url}`"
)))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn detects_youtube_watch_url() {
let source = detect("https://www.youtube.com/watch?v=dQw4w9WgXcQ").unwrap();
assert_eq!(source.name(), "youtube");
}
#[test]
fn detects_youtube_short_url() {
let source = detect("https://youtu.be/dQw4w9WgXcQ").unwrap();
assert_eq!(source.name(), "youtube");
}
#[test]
fn detects_bare_youtube_id() {
let source = detect("dQw4w9WgXcQ").unwrap();
assert_eq!(source.name(), "youtube");
}
#[test]
fn unrecognised_locator_is_invalid_locator() {
assert!(matches!(
detect("https://vimeo.com/76979871"),
Err(TranscriptError::InvalidLocator(msg)) if msg.contains("vimeo.com")
));
}
}