i18n_build/
watch.rs

1//! Utility functions to use within a `build.rs` build script using
2//! this library.
3
4use crate::error::{PathError, PathType};
5use std::path::Path;
6
7use anyhow::{anyhow, Result};
8
9use walkdir::WalkDir;
10
11/// Tell `Cargo` to rerun the build script that calls this function
12/// (upon rebuild) if the specified file/directory changes.
13pub fn cargo_rerun_if_changed(path: &Path) -> Result<(), PathError> {
14    println!(
15        "cargo:rerun-if-changed={}",
16        path.to_str().ok_or_else(|| PathError::not_valid_utf8(
17            path,
18            "rerun build script if file changed",
19            PathType::Directory,
20        ))?
21    );
22    Ok(())
23}
24
25/// Tell `Cargo` to rerun the build script that calls this function
26/// (upon rebuild) if any of the files/directories within the
27/// specified directory changes.
28pub fn cargo_rerun_if_dir_changed(path: &Path) -> Result<()> {
29    cargo_rerun_if_changed(path)?;
30
31    for result in WalkDir::new(path) {
32        match result {
33            Ok(entry) => {
34                cargo_rerun_if_changed(entry.path())?;
35            }
36            Err(err) => return Err(anyhow!("error walking directory gui/: {}", err)),
37        }
38    }
39
40    Ok(())
41}