From 59c6c5465b90d9444b8bcc840b24b83b177633dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sun, 8 Nov 2020 19:20:55 +0000 Subject: [PATCH] Make sure version increases on save --- src/index/io.rs | 23 ++++++++++++++++++++++- src/index/item.rs | 4 ++-- src/version.rs | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/index/io.rs b/src/index/io.rs index 044539b..a97fdf5 100644 --- a/src/index/io.rs +++ b/src/index/io.rs @@ -39,7 +39,7 @@ impl Index { let index = Index::load_from_file(&Index::index_file_path_for_repository_path(&self.index_directory()?))?; self.merge_items_by_file_id(index.items_by_file_id); self.merge_newest_items(index.newest_items_by_source_path); - self.version = max(self.version.clone(), index.version); + self.version = max(self.version, index.version); } self.version = self.version.next(); self.write_index_to_file(self.index_file_path())?; @@ -112,3 +112,24 @@ impl Index { .to_path_buf()) } } + +#[cfg(test)] +mod must { + use crate::index::Index; + use anyhow::Result; + + #[test] + fn have_version_increased_when_saved() -> Result<()> { + let temp_dir = tempfile::tempdir()?; + let mut index = Index::new(&temp_dir.into_path()); + let old_version = index.version; + + index.save()?; + + let new_version = index.version; + + assert!(new_version > old_version); + + Ok(()) + } +} diff --git a/src/index/item.rs b/src/index/item.rs index 605ac81..83d221d 100644 --- a/src/index/item.rs +++ b/src/index/item.rs @@ -31,7 +31,7 @@ impl IndexItem { } pub fn version(&self) -> Version { - self.version.clone() + self.version } pub fn id(&self) -> ItemId { @@ -53,7 +53,7 @@ impl From for IndexItem { relative_path: i.relative_path().to_string_lossy().to_string(), original_source_path: i.original_source_path().to_string_lossy().to_string(), id: i.id().clone(), - version: i.version().clone(), + version: *i.version(), } } } diff --git a/src/version.rs b/src/version.rs index 15d3699..1d3dfd3 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::fmt::Display; -#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize, Hash)] +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize, Hash)] pub struct Version(u128); impl Version {