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;
pub struct FsFormatter<'a> {
opts: FsFmtOpts,
root_node: &'a FsDirectoryNode,
}
impl<'a> FsFormatter<'a> {
pub fn new(opts: FsFmtOpts, root_node: &'a FsDirectoryNode) -> Self {
FsFormatter { opts, root_node }
}
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!()
}
}
}
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(())
}
}