about summary refs log tree commit diff
path: root/tools
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-06-19T02·35+0100
committertazjin <mail@tazj.in>2020-06-19T02·49+0000
commitd1524a5a6ff7e07469ec226376f51d99e3a01a96 (patch)
tree71b2a7ea2aaa5c872aaf005f6dc5b2ce8b2a5a21 /tools
parent9b5b88ae6c8e4700d96aac134a1d64af4a89f6d5 (diff)
refactor(cheddar): Pass references readers & writers into renderers r/1033
This paves the way for using other things than stdin/stdout as
sources/sinks, which is required for example for implementing a
syntect_server replacement based on cheddar.

Change-Id: I5779db8dbf7b7ced109c26b940f721d237d60785
Reviewed-on: https://cl.tvl.fyi/c/depot/+/491
Reviewed-by: glittershark <grfn@gws.fyi>
Reviewed-by: lukegb <lukegb@tvl.fyi>
Diffstat (limited to 'tools')
-rw-r--r--tools/cheddar/src/main.rs33
1 files changed, 18 insertions, 15 deletions
diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs
index 9e16b1de439b..5b16fc4aafa8 100644
--- a/tools/cheddar/src/main.rs
+++ b/tools/cheddar/src/main.rs
@@ -8,7 +8,7 @@ use std::collections::HashMap;
 use std::env;
 use std::ffi::OsStr;
 use std::io::BufRead;
-use std::io::Read;
+use std::io::Write;
 use std::io;
 use std::path::Path;
 use syntect::dumps::from_binary;
@@ -182,12 +182,11 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue {
     })
 }
 
-fn format_markdown() {
+fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) {
     let document = {
         let mut buffer = String::new();
-        let stdin = io::stdin();
-        let mut stdin = stdin.lock();
-        stdin.read_to_string(&mut buffer).expect("failed to read stdin");
+        reader.read_to_string(&mut buffer).expect("reading should work");
+        drop(reader);
         buffer
     };
 
@@ -228,17 +227,15 @@ fn format_markdown() {
         }
     });
 
-    format_html(root, &MD_OPTS, &mut io::stdout())
+    format_html(root, &MD_OPTS, writer)
         .expect("Markdown rendering failed");
 }
 
-fn format_code(args: &Args) {
-    let stdin = io::stdin();
-    let mut stdin = stdin.lock();
+fn format_code<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, args: &Args) {
     let mut linebuf = String::new();
 
     // Get the first line, we might need it for syntax identification.
-    let mut read_result = stdin.read_line(&mut linebuf);
+    let mut read_result = reader.read_line(&mut linebuf);
 
     // Set up the highlighter
     let theme = &THEMES.themes["InspiredGitHub"];
@@ -272,15 +269,15 @@ fn format_code(args: &Args) {
 
         // immediately output the current state to avoid keeping
         // things in memory
-        print!("{}", outbuf);
+        write!(writer, "{}", outbuf).expect("write should not fail");
 
         // merry go round again
         linebuf.clear();
         outbuf.clear();
-        read_result = stdin.read_line(&mut linebuf);
+        read_result = reader.read_line(&mut linebuf);
     }
 
-    println!("</pre>");
+    writeln!(writer, "</pre>").expect("write should not fail");
 }
 
 fn main() {
@@ -311,8 +308,14 @@ fn main() {
         .and_then(OsStr::to_str)
         .map(|s| s.to_string());
 
+    let stdin = io::stdin();
+    let mut in_handle = stdin.lock();
+
+    let stdout = io::stdout();
+    let mut out_handle = stdout.lock();
+
     match args.extension.as_ref().map(String::as_str) {
-        Some("md") if args.about_filter => format_markdown(),
-        _  => format_code(&args),
+        Some("md") if args.about_filter => format_markdown(&mut in_handle, &mut out_handle),
+        _  => format_code(&mut in_handle, &mut out_handle, &args),
     }
 }