diff options
| author | A Farzat <a@farzat.xyz> | 2026-06-05 15:59:57 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-06-05 16:05:56 +0300 |
| commit | 1ee2201833578111597dfb3c170eb517e7de10b9 (patch) | |
| tree | f00a863b7ea53ef8050d4eb6b96a6b736dce2d15 /src/renderer.rs | |
| parent | 7894f6954f3810fdc6e2b2ed1b507c232c168d59 (diff) | |
| download | repo2markdown-1ee2201833578111597dfb3c170eb517e7de10b9.tar.gz repo2markdown-1ee2201833578111597dfb3c170eb517e7de10b9.zip | |
Start migrating to Renderer struct
The struct should gradually replace the renderer function in a more
streamlined and configurable way. It writes directly to sink, and takes
filepaths instead of preloaded buffers.
Diffstat (limited to 'src/renderer.rs')
| -rw-r--r-- | src/renderer.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/renderer.rs b/src/renderer.rs index 76a87f0..23b5cf3 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,5 +1,6 @@ use std::{ fmt, + io::Write, path::{Path, PathBuf}, }; @@ -20,6 +21,20 @@ impl fmt::Display for RenderError { impl std::error::Error for RenderError {} +pub struct Renderer<W: Write> { + output: W, +} + +impl<W: Write> Renderer<W> { + pub fn new(output: W) -> Self { + Self { output } + } + + pub fn render_header(&mut self, project_name: &str) -> std::io::Result<()> { + writeln!(self.output, "# {}", project_name) + } +} + pub fn render(project_name: &str, files: &[(&Path, &[u8])]) -> Result<String, RenderError> { let mut output = format!("# {}\n", project_name); for (filename, bytes) in files { @@ -64,7 +79,7 @@ fn render_filename(path: &Path) -> String { mod tests { use std::{ffi::OsStr, os::unix::ffi::OsStrExt, path::Path}; - use super::{RenderError, render}; + use super::{RenderError, Renderer, render}; #[test] fn empty_project_renders_only_title() { @@ -159,4 +174,14 @@ mod tests { ```\n" ); } + + #[test] + fn renderer_writes_header() { + let mut output = Vec::new(); + let mut renderer = Renderer::new(&mut output); + + renderer.render_header("Project name").unwrap(); + + assert_eq!(String::from_utf8(output).unwrap(), "# Project name\n"); + } } |
