mdbook_preprocessor_utils/
copy_assets.rs

1use anyhow::{Error, Result};
2use std::fs;
3use std::io::ErrorKind;
4use std::path::Path;
5
6pub fn copy_assets(src_dir: impl AsRef<Path>, dst_dir: impl AsRef<Path>) -> Result<()> {
7  let src_dir = src_dir.as_ref();
8  let dst_dir = dst_dir.as_ref();
9
10  println!("cargo:rerun-if-changed={}", src_dir.display());
11  fs::create_dir_all(dst_dir)?;
12
13  let src_entries = match fs::read_dir(src_dir) {
14    Ok(src_entries) => src_entries,
15    Err(err) => match err.kind() {
16      ErrorKind::NotFound => return Ok(()),
17      _ => return Err(Error::new(err)),
18    },
19  };
20
21  for entry in src_entries {
22    let path = entry?.path();
23    fs::copy(&path, dst_dir.join(path.file_name().unwrap()))?;
24  }
25
26  Ok(())
27}