Skip to main content

yog_codegen/
write.rs

1//! Writing generated files.
2
3use std::io;
4use std::path::Path;
5
6/// Writes `code` to `path` (creating its directory) unless it's already
7/// there: unchanged files keep their mtime, so builds don't redo work.
8/// Whether it wrote.
9pub fn write_if_changed(path: &Path, code: impl AsRef<[u8]>) -> io::Result<bool> {
10    let code = code.as_ref();
11    if std::fs::read(path).ok().as_deref() == Some(code) {
12        return Ok(false);
13    }
14    if let Some(dir) = path.parent() {
15        std::fs::create_dir_all(dir)?;
16    }
17    std::fs::write(path, code)?;
18    Ok(true)
19}