bim 0.1.0

A content-first static site generator for blogs, digital gardens, and knowledge bases
Documentation
use anyhow::Result;
use std::path::Path;
use walkdir::WalkDir;

pub fn copy_static_files(source_dir: &Path, output_dir: &Path) -> Result<()> {
    let static_dir = source_dir.join("static");

    if !static_dir.exists() {
        tracing::info!("No static directory found, skipping static file copy");
        return Ok(());
    }

    for entry in WalkDir::new(&static_dir).into_iter().filter_map(|e| e.ok()) {
        let path = entry.path();

        if path.is_file() {
            let relative = path.strip_prefix(&static_dir).unwrap_or(path);
            let output_path = output_dir.join(relative);

            // Create parent directory if needed
            if let Some(parent) = output_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            // Copy file
            std::fs::copy(path, &output_path)?;
            tracing::debug!("Copied static file: {:?} -> {:?}", path, output_path);
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_copy_static_no_dir() {
        let temp = TempDir::new().unwrap();
        let output = TempDir::new().unwrap();

        let result = copy_static_files(temp.path(), output.path());
        assert!(result.is_ok());
    }

    #[test]
    fn test_copy_static_with_files() {
        let temp = TempDir::new().unwrap();
        let output = TempDir::new().unwrap();

        // Create static directory with a file
        let static_dir = temp.path().join("static");
        std::fs::create_dir_all(&static_dir).unwrap();
        std::fs::write(static_dir.join("test.txt"), "hello").unwrap();

        let result = copy_static_files(temp.path(), output.path());
        assert!(result.is_ok());
        assert!(output.path().join("test.txt").exists());
    }
}