diff options
Diffstat (limited to 'crates/turtle/src/atuin_common')
| -rw-r--r-- | crates/turtle/src/atuin_common/record.rs | 74 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_common/utils.rs | 10 |
2 files changed, 18 insertions, 66 deletions
diff --git a/crates/turtle/src/atuin_common/record.rs b/crates/turtle/src/atuin_common/record.rs index 8a10804c..7b2a1a10 100644 --- a/crates/turtle/src/atuin_common/record.rs +++ b/crates/turtle/src/atuin_common/record.rs @@ -30,7 +30,7 @@ pub(crate) struct Host { impl Host { pub(crate) fn new(id: HostId) -> Self { - Host { + Self { id, name: String::new(), } @@ -117,8 +117,8 @@ impl RecordStatus { self.hosts.entry(host).or_default().insert(tag, tail_id); } - pub(crate) fn get(&self, host: HostId, tag: String) -> Option<RecordIdx> { - self.hosts.get(&host).and_then(|v| v.get(&tag)).copied() + pub(crate) fn get(&self, host: HostId, tag: &str) -> Option<RecordIdx> { + self.hosts.get(&host).and_then(|v| v.get(tag)).copied() } /// Diff this index with another, likely remote index. @@ -134,7 +134,7 @@ impl RecordStatus { // First, we check if other has everything that self has for (host, tag_map) in &self.hosts { for (tag, idx) in tag_map { - match other.get(*host, tag.clone()) { + match other.get(*host, tag) { // The other store is all up to date! No diff. Some(t) if t.eq(idx) => (), @@ -163,7 +163,7 @@ impl RecordStatus { // account for that! for (host, tag_map) in &other.hosts { for (tag, idx) in tag_map { - match self.get(*host, tag.clone()) { + match self.get(*host, tag) { // If we have this host/tag combo, the comparison and diff will have already happened above Some(_) => (), @@ -186,15 +186,19 @@ impl RecordStatus { pub(crate) trait Encryption { fn re_encrypt( data: EncryptedData, - ad: AdditionalData, + ad: AdditionalData<'_>, old_key: &[u8; 32], new_key: &[u8; 32], ) -> Result<EncryptedData> { let data = Self::decrypt(data, ad, old_key)?; Ok(Self::encrypt(data, ad, new_key)) } - fn encrypt(data: DecryptedData, ad: AdditionalData, key: &[u8; 32]) -> EncryptedData; - fn decrypt(data: EncryptedData, ad: AdditionalData, key: &[u8; 32]) -> Result<DecryptedData>; + fn encrypt(data: DecryptedData, ad: AdditionalData<'_>, key: &[u8; 32]) -> EncryptedData; + fn decrypt( + data: EncryptedData, + ad: AdditionalData<'_>, + key: &[u8; 32], + ) -> Result<DecryptedData>; } impl Record<DecryptedData> { @@ -266,8 +270,7 @@ impl Record<EncryptedData> { mod tests { use crate::atuin_common::record::{Host, HostId}; - use super::{DecryptedData, Record, RecordStatus}; - use pretty_assertions::assert_eq; + use super::{DecryptedData, Record}; fn test_record() -> Record<DecryptedData> { Record::builder() @@ -278,55 +281,4 @@ mod tests { .idx(0) .build() } - - #[test] - fn record_index_multi_diff() { - // A much more complex case, with a bunch more checks - let mut index1 = RecordStatus::new(); - let mut index2 = RecordStatus::new(); - - let store1record1 = test_record(); - let store1record2 = store1record1.append(vec![1, 2, 3]); - - let store2record1 = test_record(); - let store2record2 = store2record1.append(vec![1, 2, 3]); - - let store3record1 = test_record(); - - let store4record1 = test_record(); - - // index1 only knows about the first two entries of the first two stores - index1.set(store1record1); - index1.set(store2record1); - - // index2 is fully up to date with the first two stores, and knows of a third - index2.set(store1record2); - index2.set(store2record2); - index2.set(store3record1); - - // index1 knows of a 4th store - index1.set(store4record1); - - let diff1 = index1.diff(&index2); - let diff2 = index2.diff(&index1); - - // both diffs the same length - assert_eq!(4, diff1.len()); - assert_eq!(4, diff2.len()); - - dbg!(&diff1, &diff2); - - // both diffs should be ALMOST the same. They will agree on which hosts and tags - // require updating, but the "other" value will not be the same. - let smol_diff_1: Vec<(HostId, String)> = - diff1.iter().map(|v| (v.host, v.tag.clone())).collect(); - let smol_diff_2: Vec<(HostId, String)> = - diff1.iter().map(|v| (v.host, v.tag.clone())).collect(); - - assert_eq!(smol_diff_1, smol_diff_2); - - // diffing with yourself = no diff - assert_eq!(index1.diff(&index1).len(), 0); - assert_eq!(index2.diff(&index2).len(), 0); - } } diff --git a/crates/turtle/src/atuin_common/utils.rs b/crates/turtle/src/atuin_common/utils.rs index ba0c8eb7..c8c2776e 100644 --- a/crates/turtle/src/atuin_common/utils.rs +++ b/crates/turtle/src/atuin_common/utils.rs @@ -81,19 +81,19 @@ pub(crate) fn home_dir() -> PathBuf { pub(crate) fn config_dir() -> PathBuf { let config_dir = - std::env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from); + env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from); config_dir.join("atuin") } pub(crate) fn data_dir() -> PathBuf { - let data_dir = std::env::var("XDG_DATA_HOME") + let data_dir = env::var("XDG_DATA_HOME") .map_or_else(|_| home_dir().join(".local").join("share"), PathBuf::from); data_dir.join("atuin") } pub(crate) fn runtime_dir() -> PathBuf { - std::env::var("XDG_RUNTIME_DIR").map_or_else(|_| data_dir(), PathBuf::from) + env::var("XDG_RUNTIME_DIR").map_or_else(|_| data_dir(), PathBuf::from) } pub(crate) fn logs_dir() -> PathBuf { @@ -217,7 +217,7 @@ mod tests { fn in_git_repo_regular() { // regular git repo should resolve to the directory containing .git let tmp = std::env::temp_dir().join("atuin-test-regular-git"); - let _ = std::fs::remove_dir_all(&tmp); + drop(std::fs::remove_dir_all(&tmp)); let subdir = tmp.join("src").join("deep"); std::fs::create_dir_all(&subdir).unwrap(); std::fs::create_dir_all(tmp.join(".git")).unwrap(); @@ -234,7 +234,7 @@ mod tests { // worktree .git is a file pointing back to the main repo — // in_git_repo should follow it so all worktrees share a workspace let tmp = std::env::temp_dir().join("atuin-test-worktree-git"); - let _ = std::fs::remove_dir_all(&tmp); + drop(std::fs::remove_dir_all(&tmp)); // main repo at tmp/main with a real .git directory let main_repo = tmp.join("main"); |
