dumpfs 0.1.0

A tool for dumping codebase information for LLMs efficiently and effectively
Documentation
//! Filesystem Formatter Module
//!
//! Provides flexible formatting of filesystem nodes (crate::types::Node) into various output formats.
mod error;
mod opts;
mod text;
use std::{fmt::Write, io::BufWriter, path::Path};

use super::FsDirectoryNode;
use askama::Template;
use error::FsFmtResult;

pub use error::FsFmtError;
pub use opts::*;
use text::FsTextFmt;

/// Factory and orchestrator for formatting FsNode trees.
pub struct FsFormatter<'a> {
    opts: FsFmtOpts,
    root_node: &'a FsDirectoryNode,
}

impl<'a> FsFormatter<'a> {
    /// Creates a new formatter for the given options and root node.
    pub fn new(opts: FsFmtOpts, root_node: &'a FsDirectoryNode) -> Self {
        FsFormatter { opts, root_node }
    }

    /// Writes the formatted output to any `io::Write` implementation.
    pub fn write<W: Write>(&self, w: &mut W) -> FsFmtResult {
        match self.opts.format {
            FsWriteFormat::Md => {
                let render = FsTextFmt::new(self.root_node, &self.opts);
                render.render_into(w)?;
                Ok(())
            }
            FsWriteFormat::Xml => {
                todo!()
            }
            FsWriteFormat::Json => {
                todo!()
            }
        }
    }

    /// Helper method to save the formatted output directly to a file.
    pub fn save<P: AsRef<Path>>(&self, file_path: P) -> FsFmtResult {
        let f = std::fs::File::create(file_path)?;
        let mut w = BufWriter::new(f);
        let mut b = String::new();
        self.write(&mut b)?;
        std::io::Write::write_all(&mut w, b.as_bytes())?;
        Ok(())
    }
}