summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-12-23 19:35:23 +0200
committerLars Wirzenius <liw@liw.fi>2020-12-23 19:35:23 +0200
commit345c5d822776ef78e594c2365d8bfd39202952e0 (patch)
tree37fbdb673eb03c63c8a909540a885d82d0225357
parent4128b937a6bd1ea3944a6a7f00930d40f27f2d2a (diff)
downloadobnam2-345c5d822776ef78e594c2365d8bfd39202952e0.tar.gz
refactor: use a struct instead of a tuple
It seems this is more idiomatic in Rust.
-rw-r--r--src/bin/obnam-server.rs6
-rw-r--r--src/chunk.rs2
-rw-r--r--src/indexedstore.rs8
-rw-r--r--src/store.rs24
4 files changed, 31 insertions, 9 deletions
diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs
index e695870..dc8aa82 100644
--- a/src/bin/obnam-server.rs
+++ b/src/bin/obnam-server.rs
@@ -163,9 +163,11 @@ pub async fn fetch_chunk(
let store = store.lock().await;
let id: ChunkId = id.parse().unwrap();
match store.load(&id) {
- Ok((meta, chunk)) => {
+ Ok(loaded) => {
+ let meta = loaded.meta().clone();
+ let data = loaded.data().clone();
info!("found chunk {}: {:?}", id, meta);
- Ok(ChunkResult::Fetched(meta, chunk))
+ Ok(ChunkResult::Fetched(meta, data))
}
Err(e) => {
error!("chunk not found: {}: {:?}", id, e);
diff --git a/src/chunk.rs b/src/chunk.rs
index e1358ee..7fdeccb 100644
--- a/src/chunk.rs
+++ b/src/chunk.rs
@@ -8,7 +8,7 @@ use std::default::Default;
///
/// A chunk also contains its associated metadata, except its
/// identifier.
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataChunk {
data: Vec<u8>,
}
diff --git a/src/indexedstore.rs b/src/indexedstore.rs
index 4cc90cc..5a41406 100644
--- a/src/indexedstore.rs
+++ b/src/indexedstore.rs
@@ -2,7 +2,7 @@ use crate::chunk::DataChunk;
use crate::chunkid::ChunkId;
use crate::chunkmeta::ChunkMeta;
use crate::index::Index;
-use crate::store::Store;
+use crate::store::{LoadedChunk, Store};
use std::path::Path;
/// A store for chunks and their metadata.
@@ -31,7 +31,7 @@ impl IndexedStore {
Ok(id)
}
- pub fn load(&self, id: &ChunkId) -> anyhow::Result<(ChunkMeta, DataChunk)> {
+ pub fn load(&self, id: &ChunkId) -> anyhow::Result<LoadedChunk> {
self.store.load(id)
}
@@ -48,8 +48,8 @@ impl IndexedStore {
}
pub fn remove(&mut self, id: &ChunkId) -> anyhow::Result<()> {
- let (meta, _) = self.store.load(id)?;
- self.index.remove("sha256", meta.sha256());
+ let loaded = self.store.load(id)?;
+ self.index.remove("sha256", loaded.meta().sha256());
self.index.remove_generation(id);
self.store.delete(id)?;
Ok(())
diff --git a/src/store.rs b/src/store.rs
index a415d7a..123b9fa 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -61,11 +61,12 @@ impl Store {
}
/// Load a chunk from a store.
- pub fn load(&self, id: &ChunkId) -> anyhow::Result<(ChunkMeta, DataChunk)> {
+ pub fn load(&self, id: &ChunkId) -> anyhow::Result<LoadedChunk> {
let (_, _, dataname) = &self.filenames(id);
let meta = self.load_meta(id)?;
let data = std::fs::read(&dataname)?;
- Ok((meta, DataChunk::new(data)))
+ let data = DataChunk::new(data);
+ Ok(LoadedChunk { meta, data })
}
/// Delete a chunk from a store.
@@ -76,3 +77,22 @@ impl Store {
Ok(())
}
}
+
+pub struct LoadedChunk {
+ meta: ChunkMeta,
+ data: DataChunk,
+}
+
+impl LoadedChunk {
+ pub fn new(meta: ChunkMeta, data: DataChunk) -> Self {
+ Self { meta, data }
+ }
+
+ pub fn meta(&self) -> &ChunkMeta {
+ &self.meta
+ }
+
+ pub fn data(&self) -> &DataChunk {
+ &self.data
+ }
+}