Skip to content

Commit 9cd7ea4

Browse files
committed
♻️ refactor verifying checksum code
1 parent 2754a86 commit 9cd7ea4

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/commands/install.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use flate2::read::GzDecoder;
1212
use indicatif::{ProgressBar, ProgressStyle};
1313
use once_cell::sync::Lazy;
1414
use progress_reader::ProgressReader;
15-
use sha2::{Digest, Sha256};
1615
use std::fs;
1716
use std::io::{BufReader, BufWriter, Read};
1817
use std::path::{Path, PathBuf};
@@ -57,8 +56,8 @@ pub enum Error {
5756
#[error(transparent)]
5857
FailedDownload(#[from] curl::Error),
5958

60-
#[error("Invalid checksum\nexptected: {expected}\ngot: {got}")]
61-
InvalidChecksum { expected: String, got: String },
59+
#[error(transparent)]
60+
InvalidChecksum(#[from] release::ChecksumError),
6261

6362
#[error(transparent)]
6463
FailedMake(#[from] make::Error),
@@ -171,37 +170,16 @@ fn download(url: &str, dir: impl AsRef<Path>) -> Result<PathBuf, Error> {
171170

172171
fn verify(filepath: impl AsRef<Path>, checksum: Option<&Hash>) -> Result<(), Error> {
173172
if let Some(checksum) = checksum {
173+
let hash_type = checksum.hash_type();
174174
let file = fs::File::open(filepath)?;
175175
let progress_bar = ProgressBar::new(file.metadata()?.len())
176176
.with_style(PROGRESS_STYLE.clone())
177-
.with_prefix("Verifying");
178-
let mut file_reader = ProgressReader::new(file, &progress_bar);
179-
180-
let (checksum, hash, hash_type) = match checksum {
181-
Hash::SHA256(checksum) => {
182-
progress_bar.set_message("SHA-256 checksum");
183-
let mut sha256 = Sha256::new();
184-
std::io::copy(&mut file_reader, &mut sha256)?;
185-
let hash = sha256.finalize();
186-
(checksum, format!("{:x}", hash), "SHA-256")
187-
}
188-
Hash::MD5(checksum) => {
189-
progress_bar.set_message("MD5 checksum");
190-
let mut md5 = md5::Context::new();
191-
std::io::copy(&mut file_reader, &mut md5)?;
192-
let hash = md5.compute();
193-
(checksum, format!("{:x}", hash), "MD5")
194-
}
195-
};
196-
if checksum == &hash {
197-
progress_bar.finish_and_clear();
198-
println!("{:>12} {} checksum", "Verified".green().bold(), hash_type);
199-
} else {
200-
return Err(Error::InvalidChecksum {
201-
expected: checksum.clone(),
202-
got: hash,
203-
});
204-
}
177+
.with_prefix("Verifying")
178+
.with_message(hash_type);
179+
180+
checksum.verify(ProgressReader::new(file, &progress_bar))?;
181+
progress_bar.finish_and_clear();
182+
println!("{:>12} {} checksum", "Verified".green().bold(), hash_type);
205183
} else {
206184
println!(
207185
"{:>12} {}: No checksum",

src/release.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,49 @@ pub enum Hash {
138138
MD5(String),
139139
}
140140

141+
#[derive(Error, Debug)]
142+
pub enum ChecksumError {
143+
#[error("Invalid checksum\nexptected: {expected}\ngot: {got}")]
144+
InvalidChecksum { expected: String, got: String },
145+
146+
#[error(transparent)]
147+
Io(#[from] std::io::Error),
148+
}
149+
150+
use sha2::Digest;
151+
impl Hash {
152+
pub fn hash_type(&self) -> &'static str {
153+
match self {
154+
Hash::SHA256(_) => "SHA-256",
155+
Hash::MD5(_) => "MD5",
156+
}
157+
}
158+
pub fn verify(&self, mut data: impl std::io::Read) -> Result<(), ChecksumError> {
159+
let (checksum, hash) = match self {
160+
Hash::SHA256(checksum) => {
161+
let mut sha256 = sha2::Sha256::new();
162+
std::io::copy(&mut data, &mut sha256)?;
163+
let hash = sha256.finalize();
164+
(checksum, format!("{:x}", hash))
165+
}
166+
Hash::MD5(checksum) => {
167+
let mut md5 = md5::Context::new();
168+
std::io::copy(&mut data, &mut md5)?;
169+
let hash = md5.compute();
170+
(checksum, format!("{:x}", hash))
171+
}
172+
};
173+
if checksum == &hash {
174+
Ok(())
175+
} else {
176+
Err(ChecksumError::InvalidChecksum {
177+
expected: checksum.clone(),
178+
got: hash,
179+
})
180+
}
181+
}
182+
}
183+
141184
#[derive(Debug, Clone, Copy, Display, PartialEq)]
142185
pub enum Support {
143186
#[display(fmt = "Active support")]

0 commit comments

Comments
 (0)