1use std::path::{Path, PathBuf};
5
6use crate::common::formats::find_format;
7use crate::common::stringtools::{space_camel_case, title_case};
8
9const KEPT_TOGETHER: [&str; 1] = ["PMFC"];
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub struct Directory {
16 pub name: &'static str,
18 pub title: &'static str,
20 pub holds: Holder,
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum Holder {
27 Composer,
29 Collection,
31}
32
33const fn composer(name: &'static str, title: &'static str) -> Directory {
34 Directory {
35 name,
36 title,
37 holds: Holder::Composer,
38 }
39}
40
41const fn collection(name: &'static str, title: &'static str) -> Directory {
42 Directory {
43 name,
44 title,
45 holds: Holder::Collection,
46 }
47}
48
49pub const CORE_DIRECTORIES: [Directory; 35] = [
52 collection("airdsAirs", "Aird's Airs"),
53 composer("bach", "Johann Sebastian Bach"),
54 composer("beach", "Amy Beach"),
55 composer("beethoven", "Ludwig van Beethoven"),
56 composer("chopin", "Frederic Chopin"),
57 composer("ciconia", "Johannes Ciconia"),
58 composer("corelli", "Arcangelo Corelli"),
59 composer("cpebach", "C.P.E. Bach"),
60 collection("demos", "Demonstration Files"),
61 collection("essenFolksong", "Essen Folksong Collection"),
62 composer("handel", "George Frideric Handel"),
63 composer("haydn", "Joseph Haydn"),
64 composer("joplin", "Scott Joplin"),
65 composer("johnson_j_r", "J. Rosamund Johnson"),
66 composer("josquin", "Josquin des Prez"),
67 collection("leadSheet", "Leadsheet demos"),
68 composer("liliuokalani", "Queen Liliʻuokalani"),
69 composer("luca", "D. Luca"),
70 composer("lusitano", "Vicente Lusitano"),
71 collection("miscFolk", "Miscellaneous Folk"),
72 composer("monteverdi", "Claudio Monteverdi"),
73 composer("mozart", "Wolfgang Amadeus Mozart"),
74 collection("nottingham-dataset", "Nottingham Music Database (partial)"),
75 collection("oneills1850", "Oneill's 1850 Collection"),
76 composer("palestrina", "Giovanni Palestrina"),
77 collection("ryansMammoth", "Ryan's Mammoth Collection"),
78 composer("schoenberg", "Arnold Schoenberg"),
79 composer("schubert", "Franz Schubert"),
80 composer("schumann_robert", "Robert Schumann"),
81 composer("schumann_clara", "Clara Schumann"),
82 collection("theoryExercises", "Theory Exercises"),
83 collection("trecento", "Fourteenth-Century Italian Music"),
84 composer("verdi", "Giuseppe Verdi"),
85 composer("weber", "Carl Maria von Weber"),
86 composer("webern", "Anton Webern"),
87];
88
89#[derive(Clone, Debug, PartialEq, Eq)]
91pub struct CorpusFile {
92 pub path: String,
94 pub title: String,
96 pub filename: String,
98 pub format: &'static str,
100 pub ext: String,
102}
103
104#[derive(Clone, Debug, PartialEq, Eq)]
106pub struct CorpusWork {
107 pub stub: String,
110 pub title: String,
112 pub files: Vec<CorpusFile>,
114}
115
116pub fn composer_paths(name: &str, paths: &[PathBuf]) -> Vec<PathBuf> {
128 let name = name.to_lowercase();
129 let mut found: Vec<PathBuf> = paths
130 .iter()
131 .filter(|path| names_composer(path, &name))
132 .cloned()
133 .collect();
134 found.sort();
135 found
136}
137
138fn names_composer(path: &Path, name: &str) -> bool {
139 path.components().any(|part| {
140 let part = part.as_os_str().to_string_lossy().to_lowercase();
141 if part == name {
142 return true;
143 }
144 match part.rsplit_once('.') {
145 Some((stem, _)) => stem == name,
146 None => false,
147 }
148 })
149}
150
151pub fn find_works(directory: &str, paths: &[PathBuf]) -> Vec<CorpusWork> {
170 let mut works: Vec<CorpusWork> = Vec::new();
171 for path in paths {
172 let text = path.to_string_lossy();
173 let Some((_, stub)) = text.split_once(directory) else {
174 continue;
175 };
176 let separators = ['/', std::path::MAIN_SEPARATOR];
178 let stub = stub.strip_prefix(separators).unwrap_or(stub);
179 let components: Vec<&str> = stub.split(separators).collect();
180 let filename = components.last().copied().unwrap_or_default();
181 let ext = suffix(filename);
182 let Some(format) = find_format(ext).0 else {
183 continue;
184 };
185
186 let work_stub = components[0].replace(ext, "");
188 let at = match works.iter().position(|work| work.stub == work_stub) {
189 Some(at) => at,
190 None => {
191 works.push(CorpusWork {
192 title: titled(&work_stub),
193 stub: work_stub,
194 files: Vec::new(),
195 });
196 works.len() - 1
197 }
198 };
199 works[at].files.push(CorpusFile {
200 path: format!("{directory}/{}", components.join("/")),
202 title: titled(&filename.replace(ext, "")),
203 filename: filename.to_string(),
204 format,
205 ext: ext.to_string(),
206 });
207 }
208 works
209}
210
211fn titled(name: &str) -> String {
213 title_case(&space_camel_case(name, true, &KEPT_TOGETHER))
214}
215
216fn suffix(name: &str) -> &str {
218 match name.rfind('.') {
219 Some(at) if at > 0 && at + 1 < name.len() => &name[at..],
220 _ => "",
221 }
222}
223
224#[cfg(test)]
225mod tests {
226 use super::*;
227
228 #[test]
229 fn a_file_named_like_the_composer_is_found() {
230 let paths = [
231 PathBuf::from("/c/demos/bach.krn"),
232 PathBuf::from("/c/demos/x.krn"),
233 ];
234 assert_eq!(
235 composer_paths("bach", &paths),
236 [PathBuf::from("/c/demos/bach.krn")]
237 );
238 }
239
240 #[test]
241 fn a_file_of_no_known_format_makes_no_work() {
242 let paths = [
243 PathBuf::from("/c/verdi/score.pdf"),
244 PathBuf::from("/c/verdi/x"),
245 ];
246 assert!(find_works("verdi", &paths).is_empty());
247 }
248
249 #[test]
250 fn a_file_at_the_top_is_a_work_of_its_own() {
251 let works = find_works("beach", &[PathBuf::from("/c/beach/prayer.mxl")]);
252 assert_eq!(works[0].stub, "prayer");
253 assert_eq!(works[0].files[0].ext, ".mxl");
254 }
255}