]>
code.octet-stream.net Git - hashgood/blob - src/display.rs
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")?
;
53 fn print_hex_compare(print
: &str, against
: &str, mut stdout
: &mut StandardStream
) -> PrintResult
{
54 for (p
, a
) in print
.chars().zip(against
.chars()) {
56 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)).set_bold(true))?
;
58 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)).set_bold(true))?
;
60 write
!(&mut stdout
, "{}", p
)?
;
63 writeln
!(&mut stdout
)?
;
68 mut stdout
: &mut StandardStream
,
69 verify_source
: &VerificationSource
,
70 candidate_filename
: &Option
<String
>,
72 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)).set_bold(true))?
;
73 match &verify_source
{
74 VerificationSource
::CommandArgument
=> {
75 writeln
!(&mut stdout
, "command line argument")?
;
77 VerificationSource
::RawFile(raw_path
) => match raw_path
.as_str() {
79 writeln
!(&mut stdout
, "from standard input")?
;
82 writeln
!(&mut stdout
, "from file '{}' containing raw hash", path
)?
;
85 VerificationSource
::DigestsFile(digest_path
) => match digest_path
.as_str() {
89 "'{}' from digests on standard input",
90 candidate_filename
.as_ref().unwrap
()
96 "'{}' in digests file '{}'",
97 candidate_filename
.as_ref().unwrap
(),
109 verify_hash
: Option
<&CandidateHash
>,
110 verify_source
: Option
<&VerificationSource
>,
113 let mut stdout
= get_stdout(no_colour
);
115 write_filename(&mut stdout
, &hash
.filename
)?
;
116 write
!(&mut stdout
, " / ")?
;
117 write_algorithm(&mut stdout
, hash
.alg
)?
;
118 writeln
!(&mut stdout
)?
;
120 // Handle basic case first - nothing to compare it to
121 let hash_hex
= hex
::encode(&hash
.bytes
);
122 let verify_hash
= match verify_hash
{
124 write
!(&mut stdout
, "{}\n\n", hash_hex
)?
;
127 Some(verify_hash
) => verify_hash
,
129 let other_hex
= hex
::encode(&verify_hash
.bytes
);
131 // Do a top-to-bottom comparison
132 print_hex_compare(&hash_hex
, &other_hex
, &mut stdout
)?
;
133 print_hex_compare(&other_hex
, &hash_hex
, &mut stdout
)?
;
135 // Show the source of our hash
136 if let Some(source
) = verify_source
{
137 write_source(&mut stdout
, source
, &verify_hash
.filename
)?
;
140 writeln
!(&mut stdout
)?
;
144 pub fn print_messages(messages
: Vec
<(MessageLevel
, String
)>, no_colour
: bool
) -> PrintResult
{
145 let mut stdout
= get_stdout(no_colour
);
147 for (level
, msg
) in &messages
{
149 MessageLevel
::Error
=> {
150 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
151 write
!(&mut stdout
, "(error) ")?
;
153 MessageLevel
::Warning
=> {
154 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
155 write
!(&mut stdout
, "(warning) ")?
;
157 MessageLevel
::Note
=> {
158 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
159 write
!(&mut stdout
, "(note) ")?
;
163 writeln
!(&mut stdout
, "{}", msg
)?
;
165 if !messages
.is
_empty
() {
166 writeln
!(&mut stdout
)?
172 pub fn print_match_level(match_level
: MatchLevel
, no_colour
: bool
) -> PrintResult
{
173 let mut stdout
= get_stdout(no_colour
);
174 write
!(&mut stdout
, "Result: ")?
;
177 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)).set_bold(true))?
;
178 writeln
!(&mut stdout
, "OK")?
;
180 MatchLevel
::Maybe
=> {
181 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)).set_bold(true))?
;
182 writeln
!(&mut stdout
, "MAYBE")?
;
184 MatchLevel
::Fail
=> {
185 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)).set_bold(true))?
;
186 writeln
!(&mut stdout
, "FAIL")?
;