use crate::report::tone::Category;
use crate::synth::{text_key, SynthSpec, ToneFile};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LibraryTone {
pub synth: &'static str,
pub prefix: &'static str,
pub file: &'static str,
pub kind: &'static str,
pub name: &'static str,
pub body: &'static str,
}
impl LibraryTone {
pub fn path(&self) -> String {
format!("@{}/{}", self.prefix, self.file)
}
pub fn include_line(&self) -> String {
format!("#include \"{}\"", self.path())
}
pub fn text_key(&self) -> String {
text_key::library(self.synth, self.name)
}
pub fn category(&self) -> Option<Category> {
Category::from_name(self.kind)
}
}
pub fn tones(spec: &SynthSpec) -> Vec<LibraryTone> {
spec.tone_files
.iter()
.flat_map(|f| of_file(spec, f))
.collect()
}
pub fn find(spec: &SynthSpec, name: &str) -> Option<LibraryTone> {
tones(library_spec(spec)?)
.into_iter()
.find(|t| t.name == name)
}
pub fn tones_in(spec: &SynthSpec, name: &str) -> Option<Vec<LibraryTone>> {
fn walk(
spec: &SynthSpec,
f: &'static ToneFile,
seen: &mut Vec<&'static str>,
out: &mut Vec<LibraryTone>,
) {
if seen.contains(&f.name) {
return;
}
seen.push(f.name);
out.extend(of_file(spec, f));
for line in f.text.lines() {
let Some(rest) = line.trim().strip_prefix("#include") else {
continue;
};
let path = rest.trim().trim_matches('"');
let g = path
.strip_prefix('@')
.and_then(|p| p.split_once('/'))
.filter(|(synth, _)| spec.matches(synth))
.and_then(|(_, n)| spec.tone_file(n));
if let Some(g) = g {
walk(spec, g, seen, out);
}
}
}
let f = spec.tone_file(name)?;
let mut out = Vec::new();
walk(spec, f, &mut Vec::new(), &mut out);
Some(out)
}
pub fn file_text(spec: &SynthSpec, file: &ToneFile, lang: &str) -> String {
let mut n = 0;
let mut out = String::with_capacity(file.text.len());
for line in file.text.lines() {
if line.trim_start().starts_with(';') {
n += 1;
let t = spec.text(&text_key::tone_file_comment(spec.name, file.name, n), lang);
if !t.is_empty() {
out.push_str("; ");
out.push_str(t);
out.push('\n');
continue;
}
}
out.push_str(line);
out.push('\n');
}
out
}
pub fn file_comments(file: &ToneFile) -> impl Iterator<Item = &'static str> {
let text: &'static str = file.text;
text.lines()
.filter_map(|l| l.trim_start().strip_prefix(';').map(str::trim))
}
fn of_file(spec: &SynthSpec, file: &'static ToneFile) -> impl Iterator<Item = LibraryTone> {
let synth = spec.name;
let prefix = spec.library_prefix();
file.tones().map(move |(name, body)| LibraryTone {
synth,
prefix,
file: file.name,
kind: file.kind.unwrap_or(""),
name,
body,
})
}
pub fn includes_for(spec: &SynthSpec, text: &str) -> Vec<String> {
let is_name = |c: char| c.is_ascii_alphanumeric() || c == '_';
let defined: Vec<&str> = text
.lines()
.filter_map(|l| {
let rest = l.trim().strip_prefix('$')?;
let (name, after) = rest.split_at(rest.find(|c| !is_name(c)).unwrap_or(rest.len()));
after.trim_start().starts_with('=').then_some(name)
})
.collect();
let mut out: Vec<String> = Vec::new();
for (i, _) in text.match_indices('$') {
let rest = &text[i + 1..];
let name = &rest[..rest.find(|c| !is_name(c)).unwrap_or(rest.len())];
if name.is_empty() || defined.contains(&name) {
continue;
}
if let Some(t) = find(spec, name) {
let line = t.include_line();
if !out.contains(&line) {
out.push(line);
}
}
}
out
}
pub fn library_spec(spec: &SynthSpec) -> Option<&SynthSpec> {
if !spec.tone_files.is_empty() {
return Some(spec);
}
let same = |a: &SynthSpec, b: &SynthSpec| {
a.tones
.iter()
.map(|t| t.id)
.eq(b.tones.iter().map(|t| t.id))
&& a.params
.iter()
.map(|p| p.name)
.eq(b.params.iter().map(|p| p.name))
};
crate::synth::all()
.iter()
.copied()
.find(|s| !s.tone_files.is_empty() && same(s, spec))
}