forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

real_library.rs89 lines, 3.4 KiBHistoryRawDownload
1//! Where the real films are, on the machine that has them.
2//!
3//! A handful of tests are worth nothing against a fixture: a 4K HDR10 grade, a
4//! header-stripped BluRay remux, a disc's PGS tracks. None of those can live in
5//! this repository -- they are tens of gigabytes of somebody's library -- and
6//! their paths are nobody's business but that machine's, so they are not in the
7//! tree either. `real_library.toml` at the workspace root maps a logical key to
8//! an absolute path; it is gitignored, and `real_library.toml.example` shows the
9//! shape. Without it every test that asks here says so and passes.
10//!
11//! The format is the `key = "path"` subset of TOML, one per line, `#` comments:
12//! enough for a path table and no dependency to parse.
13
14use std::path::PathBuf;
15
16/// The manifest, beside the workspace `Cargo.toml`.
17const MANIFEST: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../real_library.toml");
18
19/// The file `key` names, if this machine is one that has it.
20///
21/// [`None`] -- with the reason on stderr, in the skip wording the rest of these
22/// tests use -- when the manifest is absent, when it does not name the key, or
23/// when what it names is not there. A test's whole gate is therefore one line:
24///
25/// ```no_run
26/// let Some(film) = engine::real_library::film("hevc_4k_hdr") else {
27/// return;
28/// };
29/// ```
30pub fn film(key: &str) -> Option<PathBuf> {
31 let Ok(text) = std::fs::read_to_string(MANIFEST) else {
32 eprintln!(
33 "skipped: no real_library.toml at the workspace root (copy real_library.toml.example)"
34 );
35 return None;
36 };
37 let Some(path) = text.lines().find_map(|line| entry(line, key)) else {
38 eprintln!("skipped: real_library.toml does not name `{key}`");
39 return None;
40 };
41 let path = PathBuf::from(path);
42 if !path.exists() {
43 eprintln!("skipped: `{key}` is not on this machine");
44 return None;
45 }
46 Some(path)
47}
48
49/// One `key = "value"` line, if it is this key's: comments and blank lines
50/// answer [`None`], and so does any line whose value is not quoted.
51fn entry(line: &str, key: &str) -> Option<String> {
52 let line = line.trim();
53 if line.starts_with('#') {
54 return None;
55 }
56 let (name, value) = line.split_once('=')?;
57 if name.trim() != key {
58 return None;
59 }
60 let path = value.trim().strip_prefix('"')?.strip_suffix('"')?;
61 Some(path.to_string())
62}
63
64#[cfg(test)]
65mod tests {
66 use super::entry;
67
68 /// The parser, against the lines a hand-written manifest really holds: the
69 /// key it was asked for and no other, quotes off, whitespace either side of
70 /// the `=`, and a commented-out line is not an entry however much it looks
71 /// like one.
72 #[test]
73 fn a_manifest_line_answers_for_its_own_key_only() {
74 assert_eq!(entry(r#"k = "/a/b.mkv""#, "k").as_deref(), Some("/a/b.mkv"));
75 assert_eq!(
76 entry(r#" k="/a/b.mkv" "#, "k").as_deref(),
77 Some("/a/b.mkv")
78 );
79 // A path with an `=` in it survives: only the first one splits.
80 assert_eq!(
81 entry(r#"k = "/a/b=c.mkv""#, "k").as_deref(),
82 Some("/a/b=c.mkv")
83 );
84 assert_eq!(entry(r#"other = "/a/b.mkv""#, "k"), None);
85 assert_eq!(entry(r#"# k = "/a/b.mkv""#, "k"), None);
86 assert_eq!(entry("k = /a/b.mkv", "k"), None);
87 assert_eq!(entry("", "k"), None);
88 }
89}