1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use super::Handler;
use crate::error;
use std::str;
pub struct PrintLn {
show_path: bool,
separator: String,
}
impl Default for PrintLn {
fn default() -> Self {
Self {
show_path: false,
separator: "\n".into(),
}
}
}
impl PrintLn {
pub fn new() -> Self {
Self::default()
}
pub fn set_show_path(mut self, show_path: bool) -> Self {
self.show_path = show_path;
self
}
pub fn set_separator<S>(mut self, separator: S) -> Self
where
S: ToString,
{
self.separator = separator.to_string();
self
}
}
impl Handler for PrintLn {
fn show_path(&self) -> bool {
self.show_path
}
fn separator(&self) -> &str {
&self.separator
}
fn handle(&mut self, path: &str, data: &[u8]) -> Result<(), error::Handler> {
let str_data = str::from_utf8(data).map_err(|err| error::Handler::new(err.to_string()))?;
if self.show_path() {
print!("{}: {}{}", path, str_data, self.separator());
} else {
print!("{}{}", str_data, self.separator());
}
Ok(())
}
}