Cotton code
80 lines · 5.1 KB · RustRaw
use super::*;fn film(title: &str, year: Option<&str>) -> Option<FilmPath> {    Some(FilmPath::Film { title: title.into(), year: year.map(str::to_string) })}fn episode(series: &str, season: u32, episode: u32, title: Option<&str>) -> Option<FilmPath> {    Some(FilmPath::Episode { series: series.into(), season, episode, title: title.map(str::to_string) })}/// RULE: a path is read right to left, as the definition's two layouts say:/// `Series Name/Season 02/S02E05 - Title.mkv` an episode, `Film (2019).mkv`/// a film; release words never reach a title, and a sample is nothing.#[test]fn a_path_says_its_film_or_its_episode() {    assert_eq!(from_path("Films/Night Ferry (2019).mkv"), film("Night Ferry", Some("2019")));    assert_eq!(from_path("Night.Ferry.2019.1080p.BluRay.x264.mkv"), film("Night Ferry", Some("2019")));    assert_eq!(from_path("Mr. Invented (2021).mp4"), film("Mr. Invented", Some("2021")));    assert_eq!(from_path("Paper Comets.mov"), film("Paper Comets", None));    assert_eq!(from_path("2049 Rooms (2017).mkv"), film("2049 Rooms", Some("2017")), "a title may start with a year-like word");    assert_eq!(from_path("Lowmarsh/Season 02/S02E05 - The Long Night.mkv"), episode("Lowmarsh", 2, 5, Some("The Long Night")));    assert_eq!(from_path("Lowmarsh/Season 2/05 - The Long Night.mkv"), episode("Lowmarsh", 2, 5, Some("The Long Night")));    assert_eq!(from_path("Lowmarsh/S01/Lowmarsh.S01E03.720p.WEB-DL.mkv"), episode("Lowmarsh", 1, 3, None));    assert_eq!(from_path("TV/Lowmarsh - 1x04 - Cicadas.avi"), episode("Lowmarsh", 1, 4, Some("Cicadas")));    assert_eq!(from_path("Lowmarsh/Lowmarsh s03e10.mp4"), episode("Lowmarsh", 3, 10, None));    assert_eq!(from_path("Lowmarsh/Season 02/Sample/S02E05.mkv"), None, "a release's sample");    assert_eq!(from_path("Night Ferry/night.ferry.sample.mkv"), None);    assert_eq!(from_path("Night Ferry/Extras/Making of.mp4"), None);    assert_eq!(from_path("Night Ferry (2019).srt"), None, "no video");}/// RULE: in Cotton a video is a film at twenty minutes or more, or in a/// container no camera writes; the phone sends whole only what it plays as/// it is; of two files of one film, the taller then the denser is kept.#[test]fn a_film_is_judged_by_its_length_its_container_and_its_codecs() {    assert!(is_film("mkv", None) && is_film("AVI", Some(3.0)));    assert!(is_film("mp4", Some(20.0)) && !is_film("mp4", Some(19.9)) && !is_film("mov", None));    assert!(plays_whole("mp4", Some("avc1"), Some("mp4a")));    assert!(plays_whole("mp4", Some("video/avc"), Some("audio/mp4a-latm")), "Android's media types read as AVFoundation's codes");    assert!(plays_whole("mov", Some("video/hevc"), None));    assert!(plays_whole("MOV", Some("hevc"), Some("aac")));    assert!(plays_whole("m4v", Some("h264"), None), "a silent film plays");    assert!(!plays_whole("mp4", Some("avc1"), Some("ac-3")), "AC-3 audio waits for the Mac");    assert!(!plays_whole("mkv", Some("h264"), Some("aac")), "the container waits for the Mac");    assert!(!plays_whole("mp4", None, Some("aac")), "a video nobody read waits for the Mac");    let hd = Quality { height: Some(1080), bitrate: Some(4000) };    let sd = Quality { height: Some(576), bitrate: Some(9000) };    assert!(better(hd, sd) && !better(sd, hd));    assert!(better(Quality { height: Some(1080), bitrate: Some(8000) }, hd));    assert!(!better(hd, hd), "a tie keeps the one held");    assert_eq!(same_key("Night Ferry!", Some("2019")), same_key("night  ferry", Some("2019")));    assert_ne!(same_key("Night Ferry", Some("2019")), same_key("Night Ferry", Some("2020")));}/// RULE: the phone and the Mac read a path alike: every case of the shared/// fixture (`tests/fixtures/film_names.json`, also read by/// the film-names test) answers here as written.#[test]fn the_shared_fixture_reads_alike() {    let fixture = cotton_store::manifest_dir().join("tests/fixtures/film_names.json");    let cases: Vec<serde_json::Value> = serde_json::from_str(&std::fs::read_to_string(&fixture).expect("the shared fixture")).unwrap();    assert!(cases.len() >= 10, "the fixture holds its cases");    for case in cases {        let path = case["path"].as_str().unwrap();        let said = from_path(path).map(|said| serde_json::to_value(said).unwrap()).unwrap_or(serde_json::Value::Null);        assert_eq!(said, case["expect"], "{path}");    }}// RULE (Android Cinemas, 12 Nov 2026: a phone mirrors no path rule): an episode in a season's folder has its series' folder above it; a film, an episode in no season's folder and a sample have none.#[test]fn an_episode_in_a_season_names_its_series_folder() {    assert_eq!(series_folder("Picked/Night Ferry/Season 02/S02E05 - The Long Night.mkv").as_deref(), Some("Picked/Night Ferry"));    assert_eq!(series_folder("Night Ferry/Season 1/05 - Harbour.mp4").as_deref(), Some("Night Ferry"));    assert_eq!(series_folder("Night Ferry/S02E05.mkv"), None, "no season's folder");    assert_eq!(series_folder("Films/The Fog Bell (2021).mkv"), None, "a film");    assert_eq!(series_folder("Season 02/S02E05.mkv"), None, "nothing above the season");    assert_eq!(series_folder("Night Ferry/Season 02/notes.txt"), None, "no video");}