From 9f6ff22ff9a1b0a5a28d037846b1cecee4f2945c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 9 Apr 2022 08:15:03 +0300 Subject: refactor: add a Literal variant to Checksum We've had fake checksums that are just arbitrary literal strings, and this change makes this more explicit. It's a preliminary change for later support for additional checksum algorithms. Sponsored-by: author --- src/checksummer.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/checksummer.rs') diff --git a/src/checksummer.rs b/src/checksummer.rs index 50bce04..6d7303b 100644 --- a/src/checksummer.rs +++ b/src/checksummer.rs @@ -11,11 +11,19 @@ use std::fmt; /// A checksum of some data. #[derive(Debug, Clone)] pub enum Checksum { + /// An arbitrary, literal string. + Literal(String), + /// A SHA256 checksum. Sha256(String), } impl Checksum { + /// Construct a literal string. + pub fn literal(s: &str) -> Self { + Self::Literal(s.to_string()) + } + /// Compute a SHA256 checksum for a block of data. pub fn sha256(data: &[u8]) -> Self { let mut hasher = Sha256::new(); @@ -33,9 +41,9 @@ impl Checksum { impl fmt::Display for Checksum { /// Format a checksum for display. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let hash = match self { - Self::Sha256(hash) => hash, - }; - write!(f, "{}", hash) + match self { + Self::Literal(s) => write!(f, "{}", s), + Self::Sha256(hash) => write!(f, "{}", hash), + } } } -- cgit v1.2.1