diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-03-27 09:28:43 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-03-27 09:28:43 +0000 |
commit | d546ea558c52b9efef66b81419acd8355befa3d0 (patch) | |
tree | b7fdafe1d928a2f8a7fdd43596ab0ea8b3e091fa | |
parent | 4410c3234e0e1f8a17e217a21db7e8d82cbeb280 (diff) | |
parent | dfab3cbb3f5200ab8675ecbff0c1554baf09b3e3 (diff) | |
download | obnam2-d546ea558c52b9efef66b81419acd8355befa3d0.tar.gz |
Merge branch 'cleanups' into 'main'
clean up clippy complaints
See merge request larswirzenius/obnam!129
-rwxr-xr-x | check | 4 | ||||
-rw-r--r-- | src/backup_reason.rs | 2 | ||||
-rw-r--r-- | src/backup_run.rs | 30 | ||||
-rw-r--r-- | src/benchmark.rs | 2 | ||||
-rw-r--r-- | src/bin/benchmark-null.rs | 4 | ||||
-rw-r--r-- | src/chunkid.rs | 7 | ||||
-rw-r--r-- | src/chunkmeta.rs | 5 | ||||
-rw-r--r-- | src/client.rs | 17 | ||||
-rw-r--r-- | src/cmd/restore.rs | 3 | ||||
-rw-r--r-- | src/fsentry.rs | 2 | ||||
-rw-r--r-- | src/generation.rs | 6 | ||||
-rw-r--r-- | src/genlist.rs | 4 | ||||
-rw-r--r-- | src/index.rs | 12 | ||||
-rw-r--r-- | src/indexedstore.rs | 2 | ||||
-rw-r--r-- | src/policy.rs | 2 | ||||
-rw-r--r-- | src/server.rs | 6 |
16 files changed, 58 insertions, 50 deletions
@@ -17,11 +17,11 @@ fi got_cargo_cmd() { - cargo --list | grep " $1 " > /dev/null + cargo "$1" --help > /dev/null } +got_cargo_cmd clippy && cargo clippy -q --all-targets $hideok cargo build --all-targets -got_cargo_cmd clippy && $hideok cargo clippy got_cargo_cmd fmt && $hideok cargo fmt -- --check $hideok cargo test diff --git a/src/backup_reason.rs b/src/backup_reason.rs index f785dea..0a51556 100644 --- a/src/backup_reason.rs +++ b/src/backup_reason.rs @@ -14,7 +14,7 @@ pub enum Reason { } impl Reason { - pub fn from_str(text: &str) -> Reason { + pub fn from(text: &str) -> Reason { match text { "skipped" => Reason::Skipped, "new" => Reason::IsNew, diff --git a/src/backup_run.rs b/src/backup_run.rs index ea5888a..c5d5382 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -48,7 +48,7 @@ impl<'a> InitialBackup<'a> { } pub fn drop(&self) { - &self.progress.finish(); + self.progress.finish(); } pub fn backup( @@ -65,7 +65,13 @@ impl<'a> InitialBackup<'a> { let path = &entry.pathbuf(); info!("backup: {}", path.display()); self.progress.found_live_file(path); - backup_file(&self.client, &entry, &path, self.buffer_size, Reason::IsNew) + Ok(backup_file( + &self.client, + &entry, + &path, + self.buffer_size, + Reason::IsNew, + )) } } } @@ -73,7 +79,7 @@ impl<'a> InitialBackup<'a> { impl<'a> IncrementalBackup<'a> { pub fn new(config: &ClientConfig, client: &'a BackupClient) -> BackupResult<Self> { - let policy = BackupPolicy::new(); + let policy = BackupPolicy::default(); Ok(Self { client, policy, @@ -130,9 +136,13 @@ impl<'a> IncrementalBackup<'a> { Reason::IsNew | Reason::Changed | Reason::GenerationLookupError - | Reason::Unknown => { - backup_file(&self.client, &entry, &path, self.buffer_size, reason) - } + | Reason::Unknown => Ok(backup_file( + &self.client, + &entry, + &path, + self.buffer_size, + reason, + )), Reason::Unchanged | Reason::Skipped | Reason::FileError => { let fileno = old.get_fileno(&entry.pathbuf())?; let ids = if let Some(fileno) = fileno { @@ -140,7 +150,7 @@ impl<'a> IncrementalBackup<'a> { } else { vec![] }; - Ok((entry.clone(), ids, reason)) + Ok((entry, ids, reason)) } } } @@ -166,13 +176,13 @@ fn backup_file( path: &Path, chunk_size: usize, reason: Reason, -) -> BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)> { +) -> (FilesystemEntry, Vec<ChunkId>, Reason) { let ids = client.upload_filesystem_entry(&entry, chunk_size); match ids { Err(err) => { warn!("error backing up {}, skipping it: {}", path.display(), err); - Ok((entry.clone(), vec![], Reason::FileError)) + (entry.clone(), vec![], Reason::FileError) } - Ok(ids) => Ok((entry.clone(), ids, reason)), + Ok(ids) => (entry.clone(), ids, reason), } } diff --git a/src/benchmark.rs b/src/benchmark.rs index b484aa1..d214939 100644 --- a/src/benchmark.rs +++ b/src/benchmark.rs @@ -21,7 +21,7 @@ impl Iterator for ChunkGenerator { if self.next >= self.goal { None } else { - let id = ChunkId::from_str(&format!("{}", self.next)); + let id = ChunkId::recreate(&format!("{}", self.next)); let checksum = id.sha256(); let meta = ChunkMeta::new(&checksum); let chunk = DataChunk::new(vec![]); diff --git a/src/bin/benchmark-null.rs b/src/bin/benchmark-null.rs index 6df8ca1..259a837 100644 --- a/src/bin/benchmark-null.rs +++ b/src/bin/benchmark-null.rs @@ -17,13 +17,11 @@ struct Opt { num: u32, } -fn main() -> anyhow::Result<()> { +fn main() { pretty_env_logger::init(); let opt = Opt::from_args(); let gen = ChunkGenerator::new(opt.num); for (_, _, _, _) in gen {} - - Ok(()) } diff --git a/src/chunkid.rs b/src/chunkid.rs index 3933d4b..2f67d79 100644 --- a/src/chunkid.rs +++ b/src/chunkid.rs @@ -37,7 +37,8 @@ impl ChunkId { } } - pub fn from_str(s: &str) -> Self { + /// Re-construct an identifier from a previous values. + pub fn recreate(s: &str) -> Self { ChunkId { id: s.to_string() } } @@ -85,7 +86,7 @@ impl FromStr for ChunkId { type Err = (); fn from_str(s: &str) -> Result<Self, Self::Err> { - Ok(ChunkId::from_str(s)) + Ok(ChunkId::recreate(s)) } } @@ -117,6 +118,6 @@ mod test { fn survives_round_trip() { let id = ChunkId::new(); let id_str = id.to_string(); - assert_eq!(id, ChunkId::from_str(&id_str)) + assert_eq!(id, ChunkId::recreate(&id_str)) } } diff --git a/src/chunkmeta.rs b/src/chunkmeta.rs index fc5ffff..37e2ed5 100644 --- a/src/chunkmeta.rs +++ b/src/chunkmeta.rs @@ -67,10 +67,7 @@ impl ChunkMeta { /// Is this a generation chunk? pub fn is_generation(&self) -> bool { - match self.generation { - Some(true) => true, - _ => false, - } + matches!(self.generation, Some(true)) } /// When did this generation end? diff --git a/src/client.rs b/src/client.rs index f74e184..d513011 100644 --- a/src/client.rs +++ b/src/client.rs @@ -71,7 +71,10 @@ impl ClientConfig { roots: tentative.roots, verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(), chunk_size: tentative.chunk_size.or(Some(DEFAULT_CHUNK_SIZE)).unwrap(), - log: tentative.log.or(Some(PathBuf::from(DEVNULL))).unwrap(), + log: tentative + .log + .or_else(|| Some(PathBuf::from(DEVNULL))) + .unwrap(), }; config.check()?; @@ -242,7 +245,7 @@ impl BackupClient { debug!("upload_chunk: id={}", chunk_id); chunk_id.parse().unwrap() } else { - return Err(ClientError::NoCreatedChunkId.into()); + return Err(ClientError::NoCreatedChunkId); }; info!("uploaded_chunk {} meta {:?}", chunk_id, meta); Ok(chunk_id) @@ -261,7 +264,7 @@ impl BackupClient { debug!("upload_chunk: id={}", chunk_id); chunk_id.parse().unwrap() } else { - return Err(ClientError::NoCreatedChunkId.into()); + return Err(ClientError::NoCreatedChunkId); }; info!("uploaded_generation chunk {}", chunk_id); Ok(chunk_id) @@ -310,7 +313,7 @@ impl BackupClient { if res.status() != 200 { let err = ClientError::ChunkNotFound(chunk_id.to_string()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let headers = res.headers(); @@ -318,7 +321,7 @@ impl BackupClient { if meta.is_none() { let err = ClientError::NoChunkMeta(chunk_id.clone()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let meta = meta.unwrap().to_str()?; debug!("fetching chunk {}: meta={:?}", chunk_id, meta); @@ -332,7 +335,7 @@ impl BackupClient { let err = ClientError::WrongChecksum(chunk_id.clone(), actual, meta.sha256().to_string()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let chunk: DataChunk = DataChunk::new(body); @@ -341,7 +344,7 @@ impl BackupClient { } fn fetch_generation_chunk(&self, gen_id: &str) -> ClientResult<GenerationChunk> { - let chunk_id = ChunkId::from_str(gen_id); + let chunk_id = ChunkId::recreate(gen_id); let chunk = self.fetch_chunk(&chunk_id)?; let gen = GenerationChunk::from_data_chunk(&chunk)?; Ok(gen) diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 147c422..183f207 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -234,8 +234,7 @@ fn restore_metadata(path: &Path, entry: &FilesystemEntry) -> RestoreResult<()> { fn path_to_cstring(path: &Path) -> CString { let path = path.as_os_str(); let path = path.as_bytes(); - let path = CString::new(path).unwrap(); - path + CString::new(path).unwrap() } fn create_progress_bar(file_count: i64, verbose: bool) -> ProgressBar { diff --git a/src/fsentry.rs b/src/fsentry.rs index 28f7c61..35931ab 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -196,7 +196,7 @@ impl FilesystemKind { 2 => Ok(FilesystemKind::Symlink), 3 => Ok(FilesystemKind::Socket), 4 => Ok(FilesystemKind::Fifo), - _ => Err(FsEntryError::UnknownFileKindCode(code).into()), + _ => Err(FsEntryError::UnknownFileKindCode(code)), } } } diff --git a/src/generation.rs b/src/generation.rs index 240f46a..72d4468 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -62,7 +62,7 @@ impl NascentGeneration { Ok(()) } - pub fn insert_iter<'a>( + pub fn insert_iter( &mut self, entries: impl Iterator<Item = BackupResult<(FilesystemEntry, Vec<ChunkId>, Reason)>>, ) -> NascentResult<Vec<BackupError>> { @@ -162,7 +162,7 @@ pub struct BackedUpFile { impl BackedUpFile { pub fn new(fileno: FileId, entry: FilesystemEntry, reason: &str) -> Self { - let reason = Reason::from_str(reason); + let reason = Reason::from(reason); Self { fileno, entry, @@ -344,7 +344,7 @@ mod sql { if iter.next() == None { Ok(Some((fileno, entry, reason))) } else { - Err(LocalGenerationError::TooManyFiles(filename.to_path_buf()).into()) + Err(LocalGenerationError::TooManyFiles(filename.to_path_buf())) } } } diff --git a/src/genlist.rs b/src/genlist.rs index 5eec248..9d7e39d 100644 --- a/src/genlist.rs +++ b/src/genlist.rs @@ -13,7 +13,7 @@ pub enum GenerationListError { impl GenerationList { pub fn new(gens: Vec<FinishedGeneration>) -> Self { - let mut list = gens.clone(); + let mut list = gens; list.sort_by_cached_key(|gen| gen.ended().to_string()); Self { list } } @@ -33,7 +33,7 @@ impl GenerationList { let hits: Vec<FinishedGeneration> = self .iter() .filter(|gen| gen.id() == genref) - .map(|gen| gen.clone()) + .cloned() .collect(); if hits.len() == 1 { Some(hits[0].clone()) diff --git a/src/index.rs b/src/index.rs index 9386e73..887238c 100644 --- a/src/index.rs +++ b/src/index.rs @@ -138,7 +138,7 @@ mod test { let meta = ChunkMeta::new_generation("abc", "timestamp"); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); - idx.insert_meta(id.clone(), meta.clone()).unwrap(); + idx.insert_meta(id.clone(), meta).unwrap(); assert_eq!(idx.find_generations().unwrap(), vec![id]); } @@ -148,7 +148,7 @@ mod test { let meta = ChunkMeta::new_generation("abc", "timestamp"); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); - idx.insert_meta(id.clone(), meta.clone()).unwrap(); + idx.insert_meta(id.clone(), meta).unwrap(); idx.remove_meta(&id).unwrap(); assert_eq!(idx.find_generations().unwrap(), vec![]); } @@ -214,12 +214,12 @@ mod sql { } else { let err = IndexError::DuplicateChunk(id.clone()); error!("{}", err); - return Err(err.into()); + return Err(err); } } - if metas.len() == 0 { + if metas.is_empty() { eprintln!("lookup: no hits"); - return Err(IndexError::MissingChunk(id.clone()).into()); + return Err(IndexError::MissingChunk(id.clone())); } let r = metas[0].clone(); Ok(r) @@ -272,6 +272,6 @@ mod sql { fn row_to_id(row: &Row) -> rusqlite::Result<ChunkId> { let id: String = row.get(row.column_index("id")?)?; - Ok(ChunkId::from_str(&id)) + Ok(ChunkId::recreate(&id)) } } diff --git a/src/indexedstore.rs b/src/indexedstore.rs index f2d1831..7f67a1f 100644 --- a/src/indexedstore.rs +++ b/src/indexedstore.rs @@ -84,7 +84,7 @@ impl IndexedStore { let file_chunks = all_chunk_ids .iter() .filter(|id| !sql_chunks.contains(id)) - .map(|id| id.clone()) + .cloned() .collect(); Ok(file_chunks) diff --git a/src/policy.rs b/src/policy.rs index 8a65e09..39c73fc 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -9,7 +9,7 @@ pub struct BackupPolicy { } impl BackupPolicy { - pub fn new() -> Self { + pub fn default() -> Self { Self { new: true, old_if_changed: true, diff --git a/src/server.rs b/src/server.rs index abf3f1e..6ea8ac4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -49,13 +49,13 @@ impl ServerConfig { pub fn check(&self) -> Result<(), ServerConfigError> { if !self.chunks.exists() { - return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone()).into()); + return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone())); } if !self.tls_cert.exists() { - return Err(ServerConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); + return Err(ServerConfigError::TlsCertNotFound(self.tls_cert.clone())); } if !self.tls_key.exists() { - return Err(ServerConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); + return Err(ServerConfigError::TlsKeyNotFound(self.tls_key.clone())); } Ok(()) } |