Support MD5 hashes (#1556)

## Summary

We can add other hashes if necessary, but I don't know that they're
really used in practice.

Closes https://github.com/astral-sh/uv/issues/1547.
This commit is contained in:
Charlie Marsh 2024-02-16 19:25:16 -05:00 committed by GitHub
parent 9e0336c28a
commit c1eb6130e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 95 additions and 15 deletions

View file

@ -136,16 +136,21 @@ impl Default for Yanked {
#[archive(check_bytes)]
#[archive_attr(derive(Debug))]
pub struct Hashes {
pub md5: Option<String>,
pub sha256: Option<String>,
}
impl Hashes {
/// Format as `<algorithm>:<hash>`.
///
/// Currently limited to SHA256.
pub fn to_string(&self) -> Option<String> {
self.sha256
.as_ref()
.map(|sha256| format!("sha256:{sha256}"))
.or_else(|| self.md5.as_ref().map(|md5| format!("md5:{md5}")))
}
/// Return the hash digest.
pub fn as_str(&self) -> Option<&str> {
self.sha256.as_deref().or(self.md5.as_deref())
}
}