]>
code.octet-stream.net Git - hashgood/blob - src/display.rs
b886a111f909f5705140226cce010e4330ee2ba2
1 use super::{Algorithm
, CandidateHash
, Hash
, MatchLevel
, MessageLevel
, VerificationSource
};
4 use termcolor
::{Color
, ColorChoice
, ColorSpec
, StandardStream
, WriteColor
};
6 pub type PrintResult
= Result
<(), Box
<dyn Error
>>;
8 fn filename_display(filename
: &str) -> &str {
10 return "standard input";
15 fn get_stdout(no_colour
: bool
) -> StandardStream
{
17 StandardStream
::stdout(ColorChoice
::Never
)
19 StandardStream
::stdout(ColorChoice
::Always
)
23 fn write_filename(mut stdout
: &mut StandardStream
, filename
: &str) -> PrintResult
{
24 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
25 write
!(&mut stdout
, "{}", filename_display(filename
))?
;
30 fn write_algorithm(mut stdout
: &mut StandardStream
, alg
: Algorithm
) -> PrintResult
{
33 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Magenta
)))?
;
34 write
!(&mut stdout
, "MD5")?
;
37 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
38 write
!(&mut stdout
, "SHA-1")?
;
40 Algorithm
::Sha256
=> {
41 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
42 write
!(&mut stdout
, "SHA-256")?
;
49 fn print_hex_compare(print
: &str, against
: &str, mut stdout
: &mut StandardStream
) -> PrintResult
{
50 for (p
, a
) in print
.chars().zip(against
.chars()) {
52 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
54 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
56 write
!(&mut stdout
, "{}", p
)?
;
59 writeln
!(&mut stdout
)?
;
64 mut stdout
: &mut StandardStream
,
65 verify_source
: &VerificationSource
,
66 candidate_filename
: &Option
<String
>,
68 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
69 match &verify_source
{
70 VerificationSource
::CommandArgument
=> {
71 writeln
!(&mut stdout
, "command line argument")?
;
73 VerificationSource
::RawFile(raw_path
) => match raw_path
.as_str() {
75 writeln
!(&mut stdout
, "from standard input")?
;
78 writeln
!(&mut stdout
, "from file '{}' containing raw hash", path
)?
;
81 VerificationSource
::DigestsFile(digest_path
) => match digest_path
.as_str() {
85 "'{}' from digests on standard input",
86 candidate_filename
.as_ref().unwrap
()
92 "'{}' in digests file '{}'",
93 candidate_filename
.as_ref().unwrap
(),
105 verify_hash
: Option
<&CandidateHash
>,
106 verify_source
: Option
<&VerificationSource
>,
109 let mut stdout
= get_stdout(no_colour
);
111 write_filename(&mut stdout
, &hash
.filename
)?
;
112 write
!(&mut stdout
, " / ")?
;
113 write_algorithm(&mut stdout
, hash
.alg
)?
;
114 writeln
!(&mut stdout
)?
;
116 // Handle basic case first - nothing to compare it to
117 let hash_hex
= hex
::encode(&hash
.bytes
);
118 let verify_hash
= match verify_hash
{
120 write
!(&mut stdout
, "{}\n\n", hash_hex
)?
;
123 Some(verify_hash
) => verify_hash
,
125 let other_hex
= hex
::encode(&verify_hash
.bytes
);
127 // Do a top-to-bottom comparison
128 print_hex_compare(&hash_hex
, &other_hex
, &mut stdout
)?
;
129 print_hex_compare(&other_hex
, &hash_hex
, &mut stdout
)?
;
131 // Show the source of our hash
132 if let Some(source
) = verify_source
{
133 write_source(&mut stdout
, source
, &verify_hash
.filename
)?
;
136 writeln
!(&mut stdout
)?
;
140 pub fn print_messages(messages
: Vec
<(MessageLevel
, String
)>, no_colour
: bool
) -> PrintResult
{
141 let mut stdout
= get_stdout(no_colour
);
143 for (level
, msg
) in &messages
{
145 MessageLevel
::Error
=> {
146 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
147 write
!(&mut stdout
, "(error) ")?
;
149 MessageLevel
::Warning
=> {
150 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
151 write
!(&mut stdout
, "(warning) ")?
;
153 MessageLevel
::Note
=> {
154 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
155 write
!(&mut stdout
, "(note) ")?
;
159 writeln
!(&mut stdout
, "{}", msg
)?
;
161 if !messages
.is
_empty
() {
162 writeln
!(&mut stdout
)?
168 pub fn print_match_level(match_level
: MatchLevel
, no_colour
: bool
) -> PrintResult
{
169 let mut stdout
= get_stdout(no_colour
);
170 write
!(&mut stdout
, "Result: ")?
;
173 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
174 writeln
!(&mut stdout
, "OK")?
;
176 MatchLevel
::Maybe
=> {
177 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
178 writeln
!(&mut stdout
, "MAYBE")?
;
180 MatchLevel
::Fail
=> {
181 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
182 writeln
!(&mut stdout
, "FAIL")?
;