From ebf90384f6700d8671dd3e7a22c7e05a9b8fac17 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 23 Feb 2024 12:30:24 +0900 Subject: [PATCH] operation: add shorthand for .store_operation().metadata --- cli/src/operation_templater.rs | 2 +- lib/src/op_heads_store.rs | 2 +- lib/src/op_walk.rs | 4 ++-- lib/src/operation.rs | 6 +++++- lib/tests/test_operations.rs | 30 ++++++------------------------ 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/cli/src/operation_templater.rs b/cli/src/operation_templater.rs index 0d3981cee..32e8f2a64 100644 --- a/cli/src/operation_templater.rs +++ b/cli/src/operation_templater.rs @@ -154,7 +154,7 @@ fn build_operation_keyword_opt( property: impl TemplateProperty, f: impl Fn(&OperationMetadata) -> O, ) -> impl TemplateProperty { - TemplateFunction::new(property, move |op| f(&op.store_operation().metadata)) + TemplateFunction::new(property, move |op| f(op.metadata())) } let property = match name { diff --git a/lib/src/op_heads_store.rs b/lib/src/op_heads_store.rs index 828eafc4c..254297dd9 100644 --- a/lib/src/op_heads_store.rs +++ b/lib/src/op_heads_store.rs @@ -129,7 +129,7 @@ where return Ok(op_head.clone()); } - op_heads.sort_by_key(|op| op.store_operation().metadata.end_time.timestamp.clone()); + op_heads.sort_by_key(|op| op.metadata().end_time.timestamp.clone()); let new_op = resolver(op_heads)?; let mut old_op_heads = ancestor_op_heads; old_op_heads.extend_from_slice(new_op.parent_ids()); diff --git a/lib/src/op_walk.rs b/lib/src/op_walk.rs index 44fd5cf00..14eee4fc6 100644 --- a/lib/src/op_walk.rs +++ b/lib/src/op_walk.rs @@ -191,8 +191,8 @@ struct OperationByEndTime(Operation); impl Ord for OperationByEndTime { fn cmp(&self, other: &Self) -> Ordering { - let self_end_time = &self.0.store_operation().metadata.end_time; - let other_end_time = &other.0.store_operation().metadata.end_time; + let self_end_time = &self.0.metadata().end_time; + let other_end_time = &other.0.metadata().end_time; self_end_time .cmp(other_end_time) .then_with(|| self.0.cmp(&other.0)) // to comply with Eq diff --git a/lib/src/operation.rs b/lib/src/operation.rs index 8f09e8142..f8d0115a9 100644 --- a/lib/src/operation.rs +++ b/lib/src/operation.rs @@ -20,7 +20,7 @@ use std::hash::{Hash, Hasher}; use std::sync::Arc; use crate::op_store; -use crate::op_store::{OpStore, OpStoreResult, OperationId, ViewId}; +use crate::op_store::{OpStore, OpStoreResult, OperationId, OperationMetadata, ViewId}; use crate::view::View; #[derive(Clone)] @@ -104,6 +104,10 @@ impl Operation { Ok(View::new(data)) } + pub fn metadata(&self) -> &OperationMetadata { + &self.data.metadata + } + pub fn store_operation(&self) -> &op_store::Operation { &self.data } diff --git a/lib/tests/test_operations.rs b/lib/tests/test_operations.rs index 66b5d18d4..d7bb81b74 100644 --- a/lib/tests/test_operations.rs +++ b/lib/tests/test_operations.rs @@ -237,16 +237,10 @@ fn test_reparent_range_linear() { assert_eq!(stats.rewritten_count, 2); assert_eq!(stats.unreachable_count, 1); let new_op_d = read_op(&stats.new_head_ids[0]); - assert_eq!( - new_op_d.store_operation().metadata, - repo_d.operation().store_operation().metadata - ); + assert_eq!(new_op_d.metadata(), repo_d.operation().metadata()); assert_eq!(new_op_d.view_id(), repo_d.operation().view_id()); let [new_op_c] = op_parents(&new_op_d); - assert_eq!( - new_op_c.store_operation().metadata, - repo_c.operation().store_operation().metadata - ); + assert_eq!(new_op_c.metadata(), repo_c.operation().metadata()); assert_eq!(new_op_c.view_id(), repo_c.operation().view_id()); assert_eq!(new_op_c.parent_ids(), slice::from_ref(repo_a.op_id())); @@ -325,10 +319,7 @@ fn test_reparent_range_branchy() { assert_eq!(stats.rewritten_count, 3); assert_eq!(stats.unreachable_count, 2); let new_op_g = read_op(&stats.new_head_ids[0]); - assert_eq!( - new_op_g.store_operation().metadata, - repo_g.operation().store_operation().metadata - ); + assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); let [new_op_e, new_op_f] = op_parents(&new_op_g); assert_eq!(new_op_e.parent_ids(), slice::from_ref(repo_b.op_id())); @@ -355,10 +346,7 @@ fn test_reparent_range_branchy() { assert_eq!(stats.rewritten_count, 5); assert_eq!(stats.unreachable_count, 1); let new_op_g = read_op(&stats.new_head_ids[0]); - assert_eq!( - new_op_g.store_operation().metadata, - repo_g.operation().store_operation().metadata - ); + assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); let [new_op_e, new_op_f] = op_parents(&new_op_g); let [new_op_d] = op_parents(&new_op_e); @@ -384,10 +372,7 @@ fn test_reparent_range_branchy() { assert_eq!(stats.rewritten_count, 1); assert_eq!(stats.unreachable_count, 2); let new_op_g = read_op(&stats.new_head_ids[0]); - assert_eq!( - new_op_g.store_operation().metadata, - repo_g.operation().store_operation().metadata - ); + assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); assert_eq!(new_op_g.parent_ids(), slice::from_ref(repo_d.op_id())); @@ -409,10 +394,7 @@ fn test_reparent_range_branchy() { assert_eq!(stats.rewritten_count, 1); assert_eq!(stats.unreachable_count, 0); let new_op_f = read_op(&stats.new_head_ids[0]); - assert_eq!( - new_op_f.store_operation().metadata, - op_f.store_operation().metadata - ); + assert_eq!(new_op_f.metadata(), op_f.metadata()); assert_eq!(new_op_f.view_id(), op_f.view_id()); assert_eq!(new_op_f.parent_ids(), slice::from_ref(repo_d.op_id())); }