1use crate::api_types::{compile, download, execute, fmt, share};
2use std::io::{self, Write};
3
4const DEFAULT_ROW_LEN: usize = 90;
16const EXECUTION_TITLE: &str = "Execution";
17const FORMAT_TITLE: &str = "Format";
18const SHARE_TITLE: &str = "Share";
19const STDOUT_TITLE: &str = "Standard Output";
20const STDERR_TITLE: &str = "Standard Error";
21const ERROR_TITLE: &str = "Error";
22const DL_TITLE: &str = "Download";
23
24pub struct Printer {
25 writer: io::BufWriter<io::Stdout>,
27 width: usize,
28}
29
30impl Printer {
31 pub fn new() -> Printer {
32 let stdout = io::stdout();
33 let mut writer = io::BufWriter::new(stdout);
34
35 Printer {
36 writer: writer,
37 width: DEFAULT_ROW_LEN,
38 }
39 }
40
41 pub fn print_run(&mut self, res: execute::Response) -> Result<(), Box<dyn std::error::Error>> {
46 if !res.is_error() {
47 self.print_horizontal_line()?;
48 self.print_header(EXECUTION_TITLE.to_string())?;
49 self.print_horizontal_line()?;
50 self.print_header(STDERR_TITLE.to_string())?;
51 self.print_horizontal_line()?;
52 self.print_body(res.stderr)?;
53 self.print_horizontal_line()?;
54 self.print_header(STDOUT_TITLE.to_string())?;
55 self.print_horizontal_line()?;
56 self.print_body(res.stdout)?;
57 self.print_horizontal_line()?;
58 } else {
59 self.print_error(res.error)?;
60 }
61
62 Ok(())
63 }
64
65 pub fn print_fmt(&mut self, res: fmt::Response) -> Result<(), Box<dyn std::error::Error>> {
66 if !res.is_error() {
67 self.print_horizontal_line()?;
68 self.print_header(EXECUTION_TITLE.to_string())?;
69 self.print_horizontal_line()?;
70 self.print_body(res.code)?;
71 self.print_horizontal_line()?;
72 } else {
73 self.print_error(res.code)?;
75 }
76
77 Ok(())
78 }
79
80 pub fn print_share(&mut self, res: share::Response) -> Result<(), Box<dyn std::error::Error>> {
81 self.print_horizontal_line()?;
82 self.print_header(SHARE_TITLE.to_string())?;
83 self.print_horizontal_line()?;
84 self.print_header("Permalink to the playground".to_string())?;
85 self.print_horizontal_line()?;
86 self.print_body(res.playground_url())?;
87 self.print_horizontal_line()?;
88 self.print_header("Direct link to the gist".to_string())?;
89 self.print_horizontal_line()?;
90 self.print_body(res.gist_url())?;
91 self.print_horizontal_line()?;
92
93 Ok(())
94 }
95
96 pub fn print_download(
97 &mut self,
98 res: download::Response,
99 ) -> Result<(), Box<dyn std::error::Error>> {
100 self.print_horizontal_line()?;
101 self.print_header(DL_TITLE.to_string())?;
102 self.print_horizontal_line()?;
103 self.print_body(res.code)?;
104 self.print_horizontal_line()?;
105
106 Ok(())
107 }
108
109 fn print_error(&mut self, message: String) -> Result<(), Box<dyn std::error::Error>> {
110 self.print_horizontal_line()?;
111 self.print_header(ERROR_TITLE.to_string())?;
112 self.print_horizontal_line()?;
113 self.print_body(message)?;
114 self.print_horizontal_line()?;
115
116 Ok(())
117 }
118
119 fn print_header(&mut self, title: String) -> Result<(), Box<dyn std::error::Error>> {
120 let width = (DEFAULT_ROW_LEN - title.chars().count()) / 2;
121
122 writeln!(
123 self.writer,
124 "{}{}{}",
125 " ".repeat(width),
126 title,
127 " ".repeat(width),
128 )?;
129
130 Ok(())
131 }
132
133 fn print_horizontal_line(&mut self) -> Result<(), Box<dyn std::error::Error>> {
134 writeln!(self.writer, "{}", "-".repeat(self.width))?;
135
136 Ok(())
137 }
138
139 fn print_new_line(&mut self) -> Result<(), Box<dyn std::error::Error>> {
140 writeln!(self.writer, "");
141
142 Ok(())
143 }
144
145 fn print_body(&mut self, body: String) -> Result<(), Box<dyn std::error::Error>> {
146 writeln!(self.writer, "{}", body)?;
147
148 Ok(())
149 }
150}