From 7a2e7645bf8d865483a3263de00684067513f5a7 Mon Sep 17 00:00:00 2001 From: elwrcl Date: Sat, 3 Oct 2026 02:47:00 +0300 Subject: [PATCH] fix: rewrite cached sounds that differ from this build's --- src/sound/mod.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/sound/mod.rs b/src/sound/mod.rs index da5e552..2658f65 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -17,13 +17,16 @@ fn runtime_dir() -> PathBuf { .unwrap_or_else(std::env::temp_dir) } -/// The WAV for shot `n`, written into `dir` the first time. +/// The WAV for shot `n` in `dir`, (re)written whenever it differs from +/// what this build synthesises: a few milliseconds, and a tuned synth or a +/// broken file never lingers. fn cached(dir: &Path, n: u8) -> Result { let path = dir.join(format!("{n}.wav")); - if !path.exists() { + let wav = synth::wav(&synth::sound(n)); + if std::fs::read(&path).ok().as_deref() != Some(wav.as_slice()) { std::fs::create_dir_all(dir) .with_context(|| format!("could not create {}", dir.display()))?; - crate::output::write_atomic(&path, &synth::wav(&synth::sound(n)))?; + crate::output::write_atomic(&path, &wav)?; } Ok(path) } @@ -79,6 +82,14 @@ pub fn demo(config: &config::Sound) -> Result<()> { mod tests { use super::*; + #[test] + fn a_stale_or_broken_file_is_rewritten() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("3.wav"), b"an older synth").unwrap(); + let path = cached(dir.path(), 3).unwrap(); + assert_eq!(std::fs::read(path).unwrap(), synth::wav(&synth::sound(3))); + } + #[test] fn the_cache_holds_seven_wavs() { let dir = tempfile::tempdir().unwrap();