diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-10-16 07:49:02 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-10-16 11:26:04 +0300 |
commit | 74751e82695b4b0dbed3466a2caf120a020b4229 (patch) | |
tree | adb80843c262de6066ca9219d14aa43fb3ab08d8 /src/chunk.rs | |
parent | 1b67cf9c535085541d653c6213cd7e7ab5090564 (diff) | |
download | obnam2-74751e82695b4b0dbed3466a2caf120a020b4229.tar.gz |
feat: obnam-restore restores generation to stdout
Diffstat (limited to 'src/chunk.rs')
-rw-r--r-- | src/chunk.rs | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/chunk.rs b/src/chunk.rs index 17159e0..e1358ee 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -1,4 +1,6 @@ -use serde::Serialize; +use crate::chunkid::ChunkId; +use serde::{Deserialize, Serialize}; +use std::default::Default; /// Store an arbitrary chunk of data. /// @@ -6,7 +8,7 @@ use serde::Serialize; /// /// A chunk also contains its associated metadata, except its /// identifier. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct DataChunk { data: Vec<u8>, } @@ -22,3 +24,31 @@ impl DataChunk { &self.data } } + +#[derive(Default, Debug, Serialize, Deserialize)] +pub struct GenerationChunk { + chunk_ids: Vec<ChunkId>, +} + +impl GenerationChunk { + pub fn new(chunk_ids: Vec<ChunkId>) -> Self { + Self { chunk_ids } + } + + pub fn is_empty(&self) -> bool { + self.chunk_ids.is_empty() + } + + pub fn len(&self) -> usize { + self.chunk_ids.len() + } + + pub fn chunk_ids(&self) -> impl Iterator<Item = &ChunkId> { + self.chunk_ids.iter() + } + + pub fn to_data_chunk(&self) -> anyhow::Result<DataChunk> { + let json = serde_json::to_string(self)?; + Ok(DataChunk::new(json.as_bytes().to_vec())) + } +} |