diff --git a/taskchampion/taskchampion/src/server/cloud/gcp.rs b/taskchampion/taskchampion/src/server/cloud/gcp.rs index 33f409754..5fbffc13c 100644 --- a/taskchampion/taskchampion/src/server/cloud/gcp.rs +++ b/taskchampion/taskchampion/src/server/cloud/gcp.rs @@ -1,5 +1,5 @@ #![allow(unused_variables, dead_code)] -use super::service::{Service, ObjectInfo}; +use super::service::{ObjectInfo, Service}; use crate::errors::Result; use google_cloud_storage::client::{Client, ClientConfig}; use google_cloud_storage::http::error::ErrorResponse; @@ -28,9 +28,7 @@ fn is_http_error(query: u16, res: &std::result::Result) -> bo impl GcpService { pub(in crate::server) fn new(bucket: String) -> Result { let rt = Runtime::new()?; - let config = rt.block_on( - ClientConfig::default().with_auth(), - )?; + let config = rt.block_on(ClientConfig::default().with_auth())?; Ok(Self { client: Client::new(config), rt, @@ -328,7 +326,7 @@ mod tests { }; assert!(svc - .compare_and_swap(&pfx("testy"), None, b"bar".into()) + .compare_and_swap(&pfx("testy"), None, b"bar".to_vec()) .unwrap()); let got = svc.get(&pfx("testy")).unwrap(); assert_eq!(got, Some(b"bar".to_vec())); @@ -347,7 +345,7 @@ mod tests { svc.put(&pfx("testy"), b"foo1").unwrap(); svc.put(&pfx("testy"), b"foo2").unwrap(); assert!(svc - .compare_and_swap(&pfx("testy"), Some(b"foo2".into()), b"bar".into()) + .compare_and_swap(&pfx("testy"), Some(b"foo2".to_vec()), b"bar".to_vec()) .unwrap()); let got = svc.get(&pfx("testy")).unwrap(); assert_eq!(got, Some(b"bar".to_vec())); @@ -365,7 +363,7 @@ mod tests { // Create the existing file, with two generations. svc.put(&pfx("testy"), b"foo1").unwrap(); assert!(!svc - .compare_and_swap(&pfx("testy"), None, b"bar".into()) + .compare_and_swap(&pfx("testy"), None, b"bar".to_vec()) .unwrap()); let got = svc.get(&pfx("testy")).unwrap(); assert_eq!(got, Some(b"foo1".to_vec())); @@ -384,7 +382,7 @@ mod tests { svc.put(&pfx("testy"), b"foo1").unwrap(); svc.put(&pfx("testy"), b"foo2").unwrap(); assert!(!svc - .compare_and_swap(&pfx("testy"), Some(b"foo1".into()), b"bar".into()) + .compare_and_swap(&pfx("testy"), Some(b"foo1".to_vec()), b"bar".to_vec()) .unwrap()); let got = svc.get(&pfx("testy")).unwrap(); assert_eq!(got, Some(b"foo2".to_vec())); diff --git a/taskchampion/taskchampion/src/server/cloud/server.rs b/taskchampion/taskchampion/src/server/cloud/server.rs index 2d395de7b..3a33fee9f 100644 --- a/taskchampion/taskchampion/src/server/cloud/server.rs +++ b/taskchampion/taskchampion/src/server/cloud/server.rs @@ -1,6 +1,4 @@ -use super::service::{Service, ObjectInfo}; -#[cfg(not(test))] -use std::time::{SystemTime, UNIX_EPOCH}; +use super::service::{ObjectInfo, Service}; use crate::errors::{Error, Result}; use crate::server::crypto::{Cryptor, Sealed, Unsealed}; use crate::server::{ @@ -9,6 +7,8 @@ use crate::server::{ }; use ring::rand; use std::collections::{HashMap, HashSet}; +#[cfg(not(test))] +use std::time::{SystemTime, UNIX_EPOCH}; use uuid::Uuid; /// Implement the Server trait for a cloud service implemented by [`Service`]. @@ -140,7 +140,7 @@ impl CloudServer { rand::SystemRandom::new() .fill(&mut randint) .map_err(|_| Error::Server("Random number generator failure".into()))?; - Ok(randint[0]) + Ok(randint[0]) } /// Get the version from "latest", or None if the object does not exist. This always fetches a fresh @@ -249,7 +249,10 @@ impl CloudServer { // Collect all versions older than MAX_VERSION_AGE_SECS #[cfg(not(test))] let age_threshold = { - let now = SystemTime::now().duration_since(UNIX_EPOCH).map(|t| t.as_secs()).unwrap_or(0); + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|t| t.as_secs()) + .unwrap_or(0); now.saturating_sub(MAX_VERSION_AGE_SECS) }; @@ -257,13 +260,16 @@ impl CloudServer { #[cfg(test)] let age_threshold = 1000; - let old_versions: HashSet = versions.iter().filter_map(|(c, _, creation)| { - if *creation < age_threshold { - Some(*c) - } else { - None - } - }).collect(); + let old_versions: HashSet = versions + .iter() + .filter_map(|(c, _, creation)| { + if *creation < age_threshold { + Some(*c) + } else { + None + } + }) + .collect(); // Now, any pair not present in that chain can be deleted. However, another replica // may be in the state where it has uploaded a version but not changed "latest" yet, @@ -275,16 +281,20 @@ impl CloudServer { } // Collect a set of all snapshots. - let snapshots = self.service.list(b"s-").filter_map(|res| match res { - Ok(ObjectInfo { name , .. }) => { - if let Some(v) = Self::parse_snapshot_name(&name) { - Some(Ok(v)) - } else { - None + let snapshots = self + .service + .list(b"s-") + .filter_map(|res| match res { + Ok(ObjectInfo { name, .. }) => { + if let Some(v) = Self::parse_snapshot_name(&name) { + Some(Ok(v)) + } else { + None + } } - } - Err(e) => Some(Err(e)), - }).collect::>>()?; + Err(e) => Some(Err(e)), + }) + .collect::>>()?; // Find the latest snapshot by iterating back from "latest". Note that this iteration is // guaranteed not to be cyclical, as that was checked above. @@ -304,7 +314,7 @@ impl CloudServer { } // If there's a latest snapshot, delete all other snapshots. - let Some(latest_snapshot) = latest_snapshot else { + let Some(latest_snapshot) = latest_snapshot else { // If there's no snapshot, no further cleanup is possible. return Ok(()); }; @@ -482,7 +492,8 @@ mod tests { impl Service for MockService { fn put(&mut self, name: &[u8], value: &[u8]) -> Result<()> { - self.0.insert(name.to_vec(), (INSERTION_TIME, value.to_vec())); + self.0 + .insert(name.to_vec(), (INSERTION_TIME, value.to_vec())); Ok(()) } @@ -508,16 +519,21 @@ mod tests { Ok(false) } - fn list<'a>(&'a mut self, prefix: &[u8]) -> Box> + 'a> { + fn list<'a>( + &'a mut self, + prefix: &[u8], + ) -> Box> + 'a> { let prefix = prefix.to_vec(); Box::new( self.0 .iter() .filter(move |(k, _)| k.starts_with(&prefix)) - .map(|(k, (t, _))| Ok(ObjectInfo { - name: k.to_vec(), - creation: *t, - })), + .map(|(k, (t, _))| { + Ok(ObjectInfo { + name: k.to_vec(), + creation: *t, + }) + }), ) } } @@ -536,7 +552,13 @@ mod tests { // Add some testing utilities to CloudServer. impl CloudServer { - fn mock_add_version(&mut self, parent: VersionId, child: VersionId, creation: u64, data: &[u8]) { + fn mock_add_version( + &mut self, + parent: VersionId, + child: VersionId, + creation: u64, + data: &[u8], + ) { let name = Self::version_name(&parent, &child); let sealed = self .cryptor @@ -562,7 +584,9 @@ mod tests { fn mock_set_latest(&mut self, latest: VersionId) { let latest = version_to_bytes(latest); - self.service.0.insert(LATEST.to_vec(), (INSERTION_TIME, latest)); + self.service + .0 + .insert(LATEST.to_vec(), (INSERTION_TIME, latest)); } /// Create a copy of this server without any data; used for creating a MockService @@ -781,7 +805,7 @@ mod tests { fn add_version_empty() { let mut server = make_server(); let parent = Uuid::new_v4(); - let (res, _) = server.add_version(parent, b"history".into()).unwrap(); + let (res, _) = server.add_version(parent, b"history".to_vec()).unwrap(); assert!(matches!(res, AddVersionResult::Ok(_))); } @@ -791,7 +815,7 @@ mod tests { let (v1, v2) = (Uuid::new_v4(), Uuid::new_v4()); server.mock_add_version(v1, v2, 1000, b"first"); server.mock_set_latest(v2); - let (res, _) = server.add_version(v2, b"history".into()).unwrap(); + let (res, _) = server.add_version(v2, b"history".to_vec()).unwrap(); let AddVersionResult::Ok(new_version) = res else { panic!("expected OK"); }; @@ -808,7 +832,7 @@ mod tests { let (v1, v2) = (Uuid::new_v4(), Uuid::new_v4()); server.mock_add_version(v1, v2, 1000, b"first"); server.mock_set_latest(v2); - let (res, _) = server.add_version(v1, b"history".into()).unwrap(); + let (res, _) = server.add_version(v1, b"history".to_vec()).unwrap(); assert_eq!(res, AddVersionResult::ExpectedParentVersion(v2)); let mut expected = server.empty_copy(); expected.mock_add_version(v1, v2, 1000, b"first"); @@ -831,7 +855,7 @@ mod tests { server.add_version_intercept = Some(|service| { service.put(&LATEST, &version_to_bytes(V3)).unwrap(); }); - let (res, _) = server.add_version(v2, b"history".into()).unwrap(); + let (res, _) = server.add_version(v2, b"history".to_vec()).unwrap(); assert_eq!(res, AddVersionResult::ExpectedParentVersion(V3)); let mut expected = server.empty_copy(); expected.mock_add_version(v1, v2, 1000, b"first"); @@ -847,7 +871,7 @@ mod tests { server.mock_add_version(v1, v2, 1000, b"first"); server.mock_set_latest(v2); let (res, _) = server - .add_version(Uuid::new_v4(), b"history".into()) + .add_version(Uuid::new_v4(), b"history".to_vec()) .unwrap(); assert_eq!(res, AddVersionResult::ExpectedParentVersion(v2)); let mut expected = server.empty_copy(); @@ -1100,7 +1124,7 @@ mod tests { fn add_snapshot() { let mut server = make_server(); let v = Uuid::new_v4(); - server.add_snapshot(v, b"SNAP".into()).unwrap(); + server.add_snapshot(v, b"SNAP".to_vec()).unwrap(); let mut expected = server.empty_copy(); expected.mock_add_snapshot(v, INSERTION_TIME, b"SNAP"); assert_eq!(server.unencrypted(), expected.unencrypted()); @@ -1117,6 +1141,6 @@ mod tests { let mut server = make_server(); let v = Uuid::new_v4(); server.mock_add_snapshot(v, 1000, b"SNAP"); - assert_eq!(server.get_snapshot().unwrap(), Some((v, b"SNAP".into()))); + assert_eq!(server.get_snapshot().unwrap(), Some((v, b"SNAP".to_vec()))); } }