]>
code.octet-stream.net Git - hashgood/blob - src/display.rs
2cc940adca4d8883274ec1663c13d6a89d3b3b2a
   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
::Clipboard 
=> { 
  74             writeln
!(&mut stdout
, "pasted from clipboard")?
; 
  76         VerificationSource
::RawFile(raw_path
) => match raw_path
.as_str() { 
  78                 writeln
!(&mut stdout
, "from standard input")?
; 
  81                 writeln
!(&mut stdout
, "from file '{}' containing raw hash", path
)?
; 
  84         VerificationSource
::DigestsFile(digest_path
) => match digest_path
.as_str() { 
  88                     "'{}' from digests on standard input", 
  89                     candidate_filename
.as_ref().unwrap
() 
  95                     "'{}' in digests file '{}'", 
  96                     candidate_filename
.as_ref().unwrap
(), 
 108     verify_hash
: Option
<&CandidateHash
>, 
 109     verify_source
: Option
<&VerificationSource
>, 
 112     let mut stdout 
= get_stdout(no_colour
); 
 114     write_filename(&mut stdout
, &hash
.filename
)?
; 
 115     write
!(&mut stdout
, " / ")?
; 
 116     write_algorithm(&mut stdout
, hash
.alg
)?
; 
 117     writeln
!(&mut stdout
)?
; 
 119     // Handle basic case first - nothing to compare it to 
 120     let hash_hex 
= hex
::encode(&hash
.bytes
); 
 121     let verify_hash 
= match verify_hash 
{ 
 123             write
!(&mut stdout
, "{}\n\n", hash_hex
)?
; 
 126         Some(verify_hash
) => verify_hash
, 
 128     let other_hex 
= hex
::encode(&verify_hash
.bytes
); 
 130     // Do a top-to-bottom comparison 
 131     print_hex_compare(&hash_hex
, &other_hex
, &mut stdout
)?
; 
 132     print_hex_compare(&other_hex
, &hash_hex
, &mut stdout
)?
; 
 134     // Show the source of our hash 
 135     if let Some(source
) = verify_source 
{ 
 136         write_source(&mut stdout
, source
, &verify_hash
.filename
)?
; 
 139     writeln
!(&mut stdout
)?
; 
 143 pub fn print_messages(messages
: Vec
<(MessageLevel
, String
)>, no_colour
: bool
) -> PrintResult 
{ 
 144     let mut stdout 
= get_stdout(no_colour
); 
 146     for (level
, msg
) in &messages 
{ 
 148             MessageLevel
::Error 
=> { 
 149                 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
; 
 150                 write
!(&mut stdout
, "(error) ")?
; 
 152             MessageLevel
::Warning 
=> { 
 153                 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
; 
 154                 write
!(&mut stdout
, "(warning) ")?
; 
 156             MessageLevel
::Note 
=> { 
 157                 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
; 
 158                 write
!(&mut stdout
, "(note) ")?
; 
 162         writeln
!(&mut stdout
, "{}", msg
)?
; 
 164     if !messages
.is
_empty
() { 
 165         writeln
!(&mut stdout
)?
 
 171 pub fn print_match_level(match_level
: MatchLevel
, no_colour
: bool
) -> PrintResult 
{ 
 172     let mut stdout 
= get_stdout(no_colour
); 
 173     write
!(&mut stdout
, "Result: ")?
; 
 176             stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
; 
 177             writeln
!(&mut stdout
, "OK")?
; 
 179         MatchLevel
::Maybe 
=> { 
 180             stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
; 
 181             writeln
!(&mut stdout
, "MAYBE")?
; 
 183         MatchLevel
::Fail 
=> { 
 184             stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
; 
 185             writeln
!(&mut stdout
, "FAIL")?
;