dprint_cli_core/
checksums.rs

1use anyhow::bail;
2use anyhow::Result;
3
4pub fn get_sha256_checksum(bytes: &[u8]) -> String {
5  use sha2::Digest;
6  use sha2::Sha256;
7  let mut hasher = Sha256::new();
8  hasher.update(bytes);
9  format!("{:x}", hasher.finalize())
10}
11
12pub fn verify_sha256_checksum(bytes: &[u8], checksum: &str) -> Result<()> {
13  let bytes_checksum = get_sha256_checksum(bytes);
14  if bytes_checksum != checksum {
15    bail!(
16      "The checksum did not match the expected checksum.\n\nActual: {}\nExpected: {}",
17      bytes_checksum,
18      checksum
19    )
20  } else {
21    Ok(())
22  }
23}
24
25#[derive(Clone)]
26pub struct ChecksumPathOrUrl {
27  pub path_or_url: String,
28  pub checksum: Option<String>,
29}
30
31pub fn parse_checksum_path_or_url(text: &str) -> ChecksumPathOrUrl {
32  // todo: this should tell whether what follows the '@' symbol is a checksum
33  match text.rfind('@') {
34    Some(index) => ChecksumPathOrUrl {
35      path_or_url: text[..index].to_string(),
36      checksum: Some(text[index + 1..].to_string()),
37    },
38    None => ChecksumPathOrUrl {
39      path_or_url: text.to_string(),
40      checksum: None,
41    },
42  }
43}