Skip to content

Commit 442f4ab

Browse files
committed
Fix doc tests
1 parent 4c637cb commit 442f4ab

File tree

2 files changed

+190
-55
lines changed

2 files changed

+190
-55
lines changed

src/tprint.rs

+167-38
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::{fmt, io};
21
use std::cell::RefCell;
32
use std::rc::Rc;
3+
use std::{fmt, io};
44

5-
use crate::column::{TPrintColumn, TPrintAlign};
6-
use crate::output::{TPrintOutput, TPrintOutputStdout};
75
use crate::borders::{TPrintBorders, TPrintBordersASCII, TPrintBordersType};
6+
use crate::column::{TPrintAlign, TPrintColumn};
7+
use crate::output::{TPrintOutput, TPrintOutputStdout};
88
use crate::utils::get_string_width;
99

1010
type TPrintOutputRef = Rc<RefCell<dyn TPrintOutput>>;
@@ -24,7 +24,12 @@ pub struct TPrint {
2424

2525
impl TPrint {
2626
/// Creates a new TPrint object with the default ASCII borders and default output to stdout.
27-
pub fn new(show_borders: bool, show_headers: bool, spaces_left: usize, extra_spaces_between: usize) -> Self {
27+
pub fn new(
28+
show_borders: bool,
29+
show_headers: bool,
30+
spaces_left: usize,
31+
extra_spaces_between: usize,
32+
) -> Self {
2833
TPrint {
2934
output: Rc::new(RefCell::new(TPrintOutputStdout {})),
3035
borders: Rc::new(RefCell::new(TPrintBordersASCII {})),
@@ -37,7 +42,13 @@ impl TPrint {
3742
}
3843
}
3944
/// Creates a new TPrint object with the default ASCII borders and the specified output.
40-
pub fn new_with_output(output: TPrintOutputRef, show_borders: bool, show_headers: bool, spaces_left: usize, extra_spaces_between: usize) -> Self {
45+
pub fn new_with_output(
46+
output: TPrintOutputRef,
47+
show_borders: bool,
48+
show_headers: bool,
49+
spaces_left: usize,
50+
extra_spaces_between: usize,
51+
) -> Self {
4152
TPrint {
4253
output,
4354
borders: Rc::new(RefCell::new(TPrintBordersASCII {})),
@@ -51,7 +62,13 @@ impl TPrint {
5162
}
5263

5364
/// Creates a new TPrint object with the specified borders and default output to stdout.
54-
pub fn new_with_borders(borders: TPrintBordersRef, show_borders: bool, show_headers: bool, spaces_left: usize, extra_spaces_between: usize) -> Self {
65+
pub fn new_with_borders(
66+
borders: TPrintBordersRef,
67+
show_borders: bool,
68+
show_headers: bool,
69+
spaces_left: usize,
70+
extra_spaces_between: usize,
71+
) -> Self {
5572
TPrint {
5673
output: Rc::new(RefCell::new(TPrintOutputStdout {})),
5774
borders,
@@ -65,7 +82,14 @@ impl TPrint {
6582
}
6683

6784
/// Creates a new TPrint object with the specified borders and output.
68-
pub fn new_with_borders_output(borders: TPrintBordersRef, output: TPrintOutputRef, show_borders: bool, show_headers: bool, spaces_left: usize, extra_spaces_between: usize) -> Self {
85+
pub fn new_with_borders_output(
86+
borders: TPrintBordersRef,
87+
output: TPrintOutputRef,
88+
show_borders: bool,
89+
show_headers: bool,
90+
spaces_left: usize,
91+
extra_spaces_between: usize,
92+
) -> Self {
6993
TPrint {
7094
output,
7195
borders,
@@ -80,8 +104,14 @@ impl TPrint {
80104

81105
/// Adds a new column with the specified caption text, caption alignment and cells alignment.
82106
/// Columns must be defined before adding cell data to the table.
83-
pub fn column_add(&mut self, caption: &str, caption_align: TPrintAlign, cell_align: TPrintAlign) -> &mut Self {
84-
self.columns.push(TPrintColumn::new(caption, caption_align, cell_align));
107+
pub fn column_add(
108+
&mut self,
109+
caption: &str,
110+
caption_align: TPrintAlign,
111+
cell_align: TPrintAlign,
112+
) -> &mut Self {
113+
self.columns
114+
.push(TPrintColumn::new(caption, caption_align, cell_align));
85115
self
86116
}
87117

@@ -98,7 +128,8 @@ impl TPrint {
98128
/// tp.add_data("test");
99129
/// tp.add_data(42);
100130
/// tp.print()?;
101-
/// ```
131+
/// # Ok::<(), std::io::Error>(())
132+
///```
102133
///
103134
/// ## Chained Usage
104135
/// ```
@@ -108,6 +139,7 @@ impl TPrint {
108139
/// column_add("Count", TPrintAlign::Center, TPrintAlign::Left).
109140
/// add_data("test").add_data(42).
110141
/// print()?;
142+
/// # Ok::<(), std::io::Error>(())
111143
/// ```
112144
pub fn add_data<T: fmt::Display>(&mut self, value: T) -> &mut Self {
113145
let column_id = self.current_column_id;
@@ -123,30 +155,62 @@ impl TPrint {
123155
self
124156
}
125157

126-
fn print_horizonal_border(&self, left: &TPrintBordersType, right: &TPrintBordersType, middle: &TPrintBordersType, line: &TPrintBordersType) -> io::Result<()> {
158+
fn print_horizonal_border(
159+
&self,
160+
left: &TPrintBordersType,
161+
right: &TPrintBordersType,
162+
middle: &TPrintBordersType,
163+
line: &TPrintBordersType,
164+
) -> io::Result<()> {
127165
if !self.show_borders {
128-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(&TPrintBordersType::NewLine))?;
166+
self.output.borrow_mut().print_str(
167+
self.borders
168+
.borrow()
169+
.get_border(&TPrintBordersType::NewLine),
170+
)?;
129171
return Ok(());
130172
}
131173

132174
if self.spaces_left > 0 {
133-
self.output.borrow_mut().print_str(&self.borders.borrow().get_border(&TPrintBordersType::WhiteSpace).repeat(self.spaces_left))?;
175+
self.output.borrow_mut().print_str(
176+
&self
177+
.borders
178+
.borrow()
179+
.get_border(&TPrintBordersType::WhiteSpace)
180+
.repeat(self.spaces_left),
181+
)?;
134182
}
135183

136-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(left))?;
184+
self.output
185+
.borrow_mut()
186+
.print_str(self.borders.borrow().get_border(left))?;
137187

138188
for i in 0..self.columns.len() {
139189
let c = &self.columns[i];
140190

141-
self.output.borrow_mut().print_str(&self.borders.borrow().get_border(line).repeat(c.get_max_width() + 2 * self.spaces_between))?;
191+
self.output.borrow_mut().print_str(
192+
&self
193+
.borders
194+
.borrow()
195+
.get_border(line)
196+
.repeat(c.get_max_width() + 2 * self.spaces_between),
197+
)?;
142198

143199
if i < self.columns.len() - 1 {
144-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(middle))?;
200+
self.output
201+
.borrow_mut()
202+
.print_str(self.borders.borrow().get_border(middle))?;
145203
}
146204
}
147205

148-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(right))?;
149-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(&TPrintBordersType::NewLine))?;
206+
self.output
207+
.borrow_mut()
208+
.print_str(self.borders.borrow().get_border(right))?;
209+
self.output.borrow_mut().print_str(
210+
self.borders
211+
.borrow()
212+
.get_border(&TPrintBordersType::NewLine),
213+
)?;
150214

151215
Ok(())
152216
}
@@ -155,28 +219,44 @@ impl TPrint {
155219
if !self.show_borders {
156220
return Ok(());
157221
}
158-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(left_border))?;
222+
self.output
223+
.borrow_mut()
224+
.print_str(self.borders.borrow().get_border(left_border))?;
159225
Ok(())
160226
}
161227

162228
fn print_right_border(&self, right_border: &TPrintBordersType) -> io::Result<()> {
163229
if !self.show_borders {
164230
return Ok(());
165231
}
166-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(right_border))?;
167-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(&TPrintBordersType::NewLine))?;
232+
self.output
233+
.borrow_mut()
234+
.print_str(self.borders.borrow().get_border(right_border))?;
235+
self.output.borrow_mut().print_str(
236+
self.borders
237+
.borrow()
238+
.get_border(&TPrintBordersType::NewLine),
239+
)?;
168240
Ok(())
169241
}
170242

171243
fn print_internal_border(&self, v_border: &TPrintBordersType) -> io::Result<()> {
172244
if !self.show_borders {
173245
return Ok(());
174246
}
175-
self.output.borrow_mut().print_str(self.borders.borrow().get_border(v_border))?;
247+
self.output
248+
.borrow_mut()
249+
.print_str(self.borders.borrow().get_border(v_border))?;
176250
Ok(())
177251
}
178252

179-
fn print_cell(&self, value: &str, max_width: usize, align: &TPrintAlign, whitespace: &str) -> io::Result<()> {
253+
fn print_cell(
254+
&self,
255+
value: &str,
256+
max_width: usize,
257+
align: &TPrintAlign,
258+
whitespace: &str,
259+
) -> io::Result<()> {
180260
let left_spaces;
181261
let right_spaces;
182262

@@ -197,26 +277,44 @@ impl TPrint {
197277
}
198278
}
199279

200-
self.output.borrow_mut().print_str(&whitespace.repeat(left_spaces))?;
280+
self.output
281+
.borrow_mut()
282+
.print_str(&whitespace.repeat(left_spaces))?;
201283
self.output.borrow_mut().print_str(value)?;
202-
self.output.borrow_mut().print_str(&whitespace.repeat(right_spaces))?;
284+
self.output
285+
.borrow_mut()
286+
.print_str(&whitespace.repeat(right_spaces))?;
203287
Ok(())
204288
}
205289

206290
/// Prints the table to the output.
207291
pub fn print(&self) -> io::Result<()> {
208-
let total_rows: usize = self.columns.iter().map(|c| c.get_rows_count()).max().unwrap_or(0);
292+
let total_rows: usize = self
293+
.columns
294+
.iter()
295+
.map(|c| c.get_rows_count())
296+
.max()
297+
.unwrap_or(0);
209298

210299
let border = self.borders.borrow();
211300
let whitespace = border.get_border(&TPrintBordersType::WhiteSpace);
212301

213-
self.output.borrow_mut().print_str(border.get_intro(self.show_borders, self.show_headers))?;
302+
self.output
303+
.borrow_mut()
304+
.print_str(border.get_intro(self.show_borders, self.show_headers))?;
214305

215306
if self.show_headers {
216-
self.print_horizonal_border(&TPrintBordersType::HeaderTopLeft, &TPrintBordersType::HeaderTopRight, &TPrintBordersType::HeaderTopMiddle, &TPrintBordersType::HeaderHLine)?;
307+
self.print_horizonal_border(
308+
&TPrintBordersType::HeaderTopLeft,
309+
&TPrintBordersType::HeaderTopRight,
310+
&TPrintBordersType::HeaderTopMiddle,
311+
&TPrintBordersType::HeaderHLine,
312+
)?;
217313

218314
if self.spaces_left > 0 {
219-
self.output.borrow_mut().print_str(&whitespace.repeat(self.spaces_left))?;
315+
self.output
316+
.borrow_mut()
317+
.print_str(&whitespace.repeat(self.spaces_left))?;
220318
}
221319

222320
self.print_left_border(&TPrintBordersType::HeaderLeftVLine)?;
@@ -228,20 +326,36 @@ impl TPrint {
228326
self.print_internal_border(&TPrintBordersType::HeaderMiddleVLine)?;
229327
}
230328

231-
self.print_cell(c.get_caption(), c.get_max_width(), c.get_caption_align(), whitespace)?;
329+
self.print_cell(
330+
c.get_caption(),
331+
c.get_max_width(),
332+
c.get_caption_align(),
333+
whitespace,
334+
)?;
232335
}
233336

234337
self.print_right_border(&TPrintBordersType::HeaderRightVLine)?;
235338

236-
self.print_horizonal_border(&TPrintBordersType::HeaderBottomLeft, &TPrintBordersType::HeaderBottomRight, &TPrintBordersType::HeaderBottomMiddle, &TPrintBordersType::HeaderHLine)?;
237-
339+
self.print_horizonal_border(
340+
&TPrintBordersType::HeaderBottomLeft,
341+
&TPrintBordersType::HeaderBottomRight,
342+
&TPrintBordersType::HeaderBottomMiddle,
343+
&TPrintBordersType::HeaderHLine,
344+
)?;
238345
} else {
239-
self.print_horizonal_border(&TPrintBordersType::TopLeft, &TPrintBordersType::TopRight, &TPrintBordersType::TopMiddle, &TPrintBordersType::TopHLine)?;
346+
self.print_horizonal_border(
347+
&TPrintBordersType::TopLeft,
348+
&TPrintBordersType::TopRight,
349+
&TPrintBordersType::TopMiddle,
350+
&TPrintBordersType::TopHLine,
351+
)?;
240352
}
241353

242354
for r in 0..total_rows {
243355
if self.spaces_left > 0 {
244-
self.output.borrow_mut().print_str(&whitespace.repeat(self.spaces_left))?;
356+
self.output
357+
.borrow_mut()
358+
.print_str(&whitespace.repeat(self.spaces_left))?;
245359
}
246360

247361
self.print_left_border(&TPrintBordersType::MiddleLeftVLine)?;
@@ -252,17 +366,32 @@ impl TPrint {
252366
self.print_internal_border(&TPrintBordersType::MiddleMiddleVLine)?;
253367
}
254368

255-
self.print_cell(c.get_str(r), c.get_max_width(), c.get_cell_align(), whitespace)?;
369+
self.print_cell(
370+
c.get_str(r),
371+
c.get_max_width(),
372+
c.get_cell_align(),
373+
whitespace,
374+
)?;
256375
}
257376
self.print_right_border(&TPrintBordersType::MiddleRightVLine)?;
258377

259378
if r < total_rows - 1 {
260-
self.print_horizonal_border(&TPrintBordersType::MiddleLeft, &TPrintBordersType::MiddleRight, &TPrintBordersType::MiddleMiddle, &TPrintBordersType::MiddleHLine)?;
379+
self.print_horizonal_border(
380+
&TPrintBordersType::MiddleLeft,
381+
&TPrintBordersType::MiddleRight,
382+
&TPrintBordersType::MiddleMiddle,
383+
&TPrintBordersType::MiddleHLine,
384+
)?;
261385
}
262386
}
263387

264-
self.print_horizonal_border(&TPrintBordersType::BottomLeft, &TPrintBordersType::BottomRight, &TPrintBordersType::BottomMiddle, &TPrintBordersType::BottomHLine)?;
388+
self.print_horizonal_border(
389+
&TPrintBordersType::BottomLeft,
390+
&TPrintBordersType::BottomRight,
391+
&TPrintBordersType::BottomMiddle,
392+
&TPrintBordersType::BottomHLine,
393+
)?;
265394

266395
self.output.borrow_mut().print_str(border.get_closing())
267396
}
268-
}
397+
}

0 commit comments

Comments
 (0)