diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-05-29 11:38:06 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-05-29 15:20:23 +0300 |
commit | cc62aac1a3ebdb1bf48a6520d430ad80948bcd51 (patch) | |
tree | ceb26e1a122e29e6e93986884a883fd628d4d58c /src/chunkmeta.rs | |
parent | 08b71890de3407acc323f09330ebe8a8ee2782ec (diff) | |
download | obnam2-cc62aac1a3ebdb1bf48a6520d430ad80948bcd51.tar.gz |
refactor: make metadata part of a chunk
This makes is harder to accidentally use the wrong metadata for a
chunk.
Diffstat (limited to 'src/chunkmeta.rs')
-rw-r--r-- | src/chunkmeta.rs | 21 |
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()); + } } |