]>
code.octet-stream.net Git - hashgood/blob - src/display.rs
1b91581b44cdcc8e6394fb34a685757d669c599e
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
)).set_bold(true))?
;
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
)).set_bold(true))?
;
34 write
!(&mut stdout
, "MD5")?
;
37 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)).set_bold(true))?
;
38 write
!(&mut stdout
, "SHA-1")?
;
40 Algorithm
::Sha256
=> {
41 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)).set_bold(true))?
;
42 write
!(&mut stdout
, "SHA-256")?
;
44 Algorithm
::Sha512
=> {
45 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Blue
)).set_bold(true))?
;
46 write
!(&mut stdout
, "SHA-512")?
;
56 mut stdout
: &mut StandardStream
,
58 for (p
, m
) in print
.iter
().zip(matches
.iter
()) {
60 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)).set_bold(true))?
;
62 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)).set_bold(true))?
;
64 write
!(&mut stdout
, "{:02x}", p
)?
;
67 writeln
!(&mut stdout
)?
;
72 mut stdout
: &mut StandardStream
,
73 verify_source
: &VerificationSource
,
74 candidate_filename
: &Option
<String
>,
76 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)).set_bold(true))?
;
77 match &verify_source
{
78 VerificationSource
::CommandArgument
=> {
79 writeln
!(&mut stdout
, "command line argument")?
;
81 VerificationSource
::RawFile(raw_path
) => match raw_path
.as_str() {
83 writeln
!(&mut stdout
, "from standard input")?
;
86 writeln
!(&mut stdout
, "from file '{}' containing raw hash", path
)?
;
89 VerificationSource
::DigestsFile(digest_path
) => match digest_path
.as_str() {
93 "'{}' from digests on standard input",
94 candidate_filename
.as_ref().unwrap
()
100 "'{}' in digests file '{}'",
101 candidate_filename
.as_ref().unwrap
(),
111 fn calculate_match_indices(bytes1
: &[u8], bytes2
: &[u8]) -> Vec
<bool
> {
115 .map(|(b1
, b2
)| b1
== b2
)
121 verify_hash
: Option
<&CandidateHash
>,
122 verify_source
: Option
<&VerificationSource
>,
125 let mut stdout
= get_stdout(no_colour
);
127 write_filename(&mut stdout
, &hash
.filename
)?
;
128 write
!(&mut stdout
, " / ")?
;
129 write_algorithm(&mut stdout
, hash
.alg
)?
;
130 writeln
!(&mut stdout
)?
;
132 // Handle basic case first - nothing to compare it to
133 let verify_hash
= match verify_hash
{
135 write
!(&mut stdout
, "{}\n\n", hex
::encode(&hash
.bytes
))?
;
138 Some(verify_hash
) => verify_hash
,
141 // Do a top-to-bottom comparison
142 let matches
= calculate_match_indices(&hash
.bytes
, &verify_hash
.bytes
);
143 print_hex_compare(&hash
.bytes
, &matches
, &mut stdout
)?
;
144 print_hex_compare(&verify_hash
.bytes
, &matches
, &mut stdout
)?
;
146 // Show the source of our hash
147 if let Some(source
) = verify_source
{
148 write_source(&mut stdout
, source
, &verify_hash
.filename
)?
;
151 writeln
!(&mut stdout
)?
;
155 pub fn print_messages(messages
: Vec
<(MessageLevel
, String
)>, no_colour
: bool
) -> PrintResult
{
156 let mut stdout
= get_stdout(no_colour
);
158 for (level
, msg
) in &messages
{
160 MessageLevel
::Error
=> {
161 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
162 write
!(&mut stdout
, "(error) ")?
;
164 MessageLevel
::Warning
=> {
165 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
166 write
!(&mut stdout
, "(warning) ")?
;
168 MessageLevel
::Note
=> {
169 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
170 write
!(&mut stdout
, "(note) ")?
;
174 writeln
!(&mut stdout
, "{}", msg
)?
;
176 if !messages
.is
_empty
() {
177 writeln
!(&mut stdout
)?
183 pub fn print_match_level(match_level
: MatchLevel
, no_colour
: bool
) -> PrintResult
{
184 let mut stdout
= get_stdout(no_colour
);
185 write
!(&mut stdout
, "Result: ")?
;
188 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)).set_bold(true))?
;
189 writeln
!(&mut stdout
, "OK")?
;
191 MatchLevel
::Maybe
=> {
192 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)).set_bold(true))?
;
193 writeln
!(&mut stdout
, "MAYBE")?
;
195 MatchLevel
::Fail
=> {
196 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)).set_bold(true))?
;
197 writeln
!(&mut stdout
, "FAIL")?
;