From 345c5d822776ef78e594c2365d8bfd39202952e0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 23 Dec 2020 19:35:23 +0200 Subject: refactor: use a struct instead of a tuple It seems this is more idiomatic in Rust. --- src/bin/obnam-server.rs | 6 ++++-- src/chunk.rs | 2 +- src/indexedstore.rs | 8 ++++---- src/store.rs | 24 ++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') 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, } 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 { 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 { 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 + } +} -- cgit v1.2.1