diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-09 08:15:03 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-16 09:04:12 +0300 |
commit | 9f6ff22ff9a1b0a5a28d037846b1cecee4f2945c (patch) | |
tree | b5c56855f74c088775134fffbc88036176240968 /src/checksummer.rs | |
parent | 8da3f80d296dc1891159fe4fdc1787cecd9730d0 (diff) | |
download | obnam2-9f6ff22ff9a1b0a5a28d037846b1cecee4f2945c.tar.gz |
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
Diffstat (limited to 'src/checksummer.rs')
-rw-r--r-- | src/checksummer.rs | 16 |
1 files changed, 12 insertions, 4 deletions
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), + } } } |