summaryrefslogtreecommitdiff
path: root/src/chunk.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/chunk.rs')
-rw-r--r--src/chunk.rs34
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()))
+ }
+}