summaryrefslogtreecommitdiff
path: root/src/label.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-04-09 09:40:59 +0300
committerLars Wirzenius <liw@liw.fi>2022-04-16 09:06:05 +0300
commit82ff782fe85c84c10f1f18c9bd5c2b017bc2f240 (patch)
treedee4767299a179067b9b54d557e96f95af88801c /src/label.rs
parentd9b72ffa5485f3c253da22f09ff0a7090de7aa37 (diff)
downloadobnam2-82ff782fe85c84c10f1f18c9bd5c2b017bc2f240.tar.gz
feat! change how chunk labels are serialized
Serialized labels now start with a type prefix: a character that says what type of label it is. This isn't strictly required: we _can_ just decide to always use a single type of checksum for all chunks in one backup, for one client, or in the whole repository. However, if it's ever possible to have more than one type, it helps debugging if every checksum, when serialized, is explicit about its type. Change things to use the new serialize method instead of the Display trait for Label. We're primarily serializing labels so they can be stored in a database, and used in URLs, only secondarily showing them to users. Sponsored-by: author
Diffstat (limited to 'src/label.rs')
-rw-r--r--src/label.rs59
1 files changed, 48 insertions, 11 deletions
diff --git a/src/label.rs b/src/label.rs
index 7ee55d1..64be341 100644
--- a/src/label.rs
+++ b/src/label.rs
@@ -6,7 +6,9 @@
//! small number of carefully chosen algorithms are supported here.
use sha2::{Digest, Sha256};
-use std::fmt;
+
+const LITERAL: char = '0';
+const SHA256: char = '1';
/// A checksum of some data.
#[derive(Debug, Clone)]
@@ -32,18 +34,53 @@ impl Label {
Self::Sha256(format!("{:x}", hash))
}
- /// Create a `Checksum` from a known, previously computed hash.
- pub fn sha256_from_str_unchecked(hash: &str) -> Self {
- Self::Sha256(hash.to_string())
+ /// Serialize a label into a string representation.
+ pub fn serialize(&self) -> String {
+ match self {
+ Self::Literal(s) => format!("{}{}", LITERAL, s),
+ Self::Sha256(hash) => format!("{}{}", SHA256, hash),
+ }
}
-}
-impl fmt::Display for Label {
- /// Format a checksum for display.
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Self::Literal(s) => write!(f, "{}", s),
- Self::Sha256(hash) => write!(f, "{}", hash),
+ /// De-serialize a label from its string representation.
+ pub fn deserialize(s: &str) -> Result<Self, LabelError> {
+ if s.starts_with(LITERAL) {
+ Ok(Self::Literal(s[1..].to_string()))
+ } else if s.starts_with(SHA256) {
+ Ok(Self::Sha256(s[1..].to_string()))
+ } else {
+ Err(LabelError::UnknownType(s.to_string()))
}
}
}
+
+/// Possible errors from dealing with chunk labels.
+#[derive(Debug, thiserror::Error)]
+pub enum LabelError {
+ /// Serialized label didn't start with a known type prefix.
+ #[error("Unknown label: {0:?}")]
+ UnknownType(String),
+}
+
+#[cfg(test)]
+mod test {
+ use super::Label;
+
+ #[test]
+ fn roundtrip_literal() {
+ let label = Label::literal("dummy data");
+ let serialized = label.serialize();
+ let de = Label::deserialize(&serialized).unwrap();
+ let seri2 = de.serialize();
+ assert_eq!(serialized, seri2);
+ }
+
+ #[test]
+ fn roundtrip_sha256() {
+ let label = Label::sha256(b"dummy data");
+ let serialized = label.serialize();
+ let de = Label::deserialize(&serialized).unwrap();
+ let seri2 = de.serialize();
+ assert_eq!(serialized, seri2);
+ }
+}