summaryrefslogtreecommitdiff
path: root/src/chunkmeta.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-06-04 06:35:14 +0000
committerLars Wirzenius <liw@liw.fi>2021-06-04 06:35:14 +0000
commitf2a274ee1291531c1154176bca5b9a47e9c234bd (patch)
tree6721b515739c6e5f9236ffd6f4ecc2dfecc471d2 /src/chunkmeta.rs
parentcb33088dbedf4b772013f83b8226047cc4355dd2 (diff)
parent9c2590d2428f0d3de882686ec2ec5832e7123c62 (diff)
downloadobnam2-f2a274ee1291531c1154176bca5b9a47e9c234bd.tar.gz
Merge branch 'aead' into 'main'
add encryption of individual chunks Closes #110 See merge request larswirzenius/obnam!146
Diffstat (limited to 'src/chunkmeta.rs')
-rw-r--r--src/chunkmeta.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/chunkmeta.rs b/src/chunkmeta.rs
index 37e2ed5..73d9007 100644
--- a/src/chunkmeta.rs
+++ b/src/chunkmeta.rs
@@ -80,10 +80,20 @@ impl ChunkMeta {
&self.sha256
}
+ /// Serialize from a textual JSON representation.
+ pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
+ serde_json::from_str(json)
+ }
+
/// Serialize as JSON.
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
+
+ /// Serialize as JSON, as a byte vector.
+ pub fn to_json_vec(&self) -> Vec<u8> {
+ self.to_json().as_bytes().to_vec()
+ }
}
impl FromStr for ChunkMeta {
@@ -135,10 +145,19 @@ mod test {
}
#[test]
- fn json_roundtrip() {
+ fn generation_json_roundtrip() {
let meta = ChunkMeta::new_generation("abcdef", "2020-09-17T08:17:13+03:00");
let json = serde_json::to_string(&meta).unwrap();
let meta2 = serde_json::from_str(&json).unwrap();
assert_eq!(meta, meta2);
}
+
+ #[test]
+ fn data_json_roundtrip() {
+ let meta = ChunkMeta::new("abcdef");
+ let json = meta.to_json_vec();
+ let meta2 = serde_json::from_slice(&json).unwrap();
+ assert_eq!(meta, meta2);
+ assert_eq!(meta.to_json_vec(), meta2.to_json_vec());
+ }
}