cartulary 0.3.0-alpha.1

The knowledge layer of your project — decisions, issues, docs, all in one place.
Documentation
//! Production adapter for [`MigrationCorpus`]. Walks the corpus with
//! `walkdir`, reads/writes via `std::fs`, renames directories via
//! `std::fs::rename`. No state — every call goes straight to the OS.

use std::fs;
use std::path::{Path, PathBuf};

use crate::domain::usecases::migrate::MigrationCorpus;

pub struct FsMigrationCorpus;

impl MigrationCorpus for FsMigrationCorpus {
    fn collect_index_paths(&self, dirs: &[PathBuf]) -> Vec<PathBuf> {
        let mut out = Vec::new();
        for dir in dirs {
            if !dir.exists() {
                continue;
            }
            for entry in walkdir::WalkDir::new(dir)
                .into_iter()
                .filter_map(Result::ok)
                .filter(|e| e.file_type().is_file() && e.file_name() == "index.md")
            {
                out.push(entry.path().to_path_buf());
            }
        }
        out
    }

    fn read(&self, path: &Path) -> anyhow::Result<String> {
        Ok(fs::read_to_string(path)?)
    }

    fn write(&self, path: &Path, content: &str) -> anyhow::Result<()> {
        fs::write(path, content)?;
        Ok(())
    }

    fn rename(&self, from: &Path, to: &Path) -> anyhow::Result<()> {
        fs::rename(from, to)?;
        Ok(())
    }

    fn exists(&self, path: &Path) -> bool {
        path.exists()
    }
}