From 7391e26d18eb6833a6191e6359346f3c0c502607 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 19 Jan 2021 08:34:15 +0200 Subject: refactor: use ChunkId directly in errors, instead of String --- src/client.rs | 6 +++--- src/error.rs | 9 +++++---- src/index.rs | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index 23df540..515b8c9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -227,7 +227,7 @@ impl BackupClient { let headers = res.headers(); let meta = headers.get("chunk-meta"); if meta.is_none() { - let err = ObnamError::NoChunkMeta(chunk_id.to_string()); + let err = ObnamError::NoChunkMeta(chunk_id.clone()); error!("fetching chunk {} failed: {}", chunk_id, err); return Err(err.into()); } @@ -240,8 +240,8 @@ impl BackupClient { let body = body.to_vec(); let actual = sha256(&body); if actual != meta.sha256() { - let id = chunk_id.to_string(); - let err = ObnamError::WrongChecksum(id, actual, meta.sha256().to_string()); + let err = + ObnamError::WrongChecksum(chunk_id.clone(), actual, meta.sha256().to_string()); error!("fetching chunk {} failed: {}", chunk_id, err); return Err(err.into()); } diff --git a/src/error.rs b/src/error.rs index a35a99c..d368763 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,4 @@ +use crate::chunkid::ChunkId; use std::path::PathBuf; use thiserror::Error; @@ -11,14 +12,14 @@ pub enum ObnamError { TooManyFiles(PathBuf), #[error("Server response did not have a 'chunk-meta' header for chunk {0}")] - NoChunkMeta(String), + NoChunkMeta(ChunkId), #[error("Wrong checksum for chunk {0}, got {1}, expected {2}")] - WrongChecksum(String, String, String), + WrongChecksum(ChunkId, String, String), #[error("Chunk is missing: {0}")] - MissingChunk(String), + MissingChunk(ChunkId), #[error("Chunk is in store too many times: {0}")] - DuplicateChunk(String), + DuplicateChunk(ChunkId), } diff --git a/src/index.rs b/src/index.rs index fd38611..d527839 100644 --- a/src/index.rs +++ b/src/index.rs @@ -189,14 +189,14 @@ mod sql { eprintln!("lookup: meta={:?}", meta); metas.push(meta); } else { - let err = ObnamError::DuplicateChunk(id.to_string()); + let err = ObnamError::DuplicateChunk(id.clone()); error!("{}", err); return Err(err.into()); } } if metas.len() == 0 { eprintln!("lookup: no hits"); - return Err(ObnamError::MissingChunk(format!("{}", id)).into()); + return Err(ObnamError::MissingChunk(id.clone()).into()); } let r = metas[0].clone(); Ok(r) -- cgit v1.2.1