diff --git a/lib/src/op_heads_store.rs b/lib/src/op_heads_store.rs index 3d323bec0..a0709267a 100644 --- a/lib/src/op_heads_store.rs +++ b/lib/src/op_heads_store.rs @@ -84,7 +84,7 @@ impl OpHeadsStore { let op_head_file_name = op_head_entry.unwrap().file_name(); let op_head_file_name = op_head_file_name.to_str().unwrap(); if let Ok(op_head) = hex::decode(op_head_file_name) { - op_heads.push(OperationId(op_head)); + op_heads.push(OperationId::new(op_head)); } } op_heads diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index bb41eaebc..6a7225294 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -18,7 +18,7 @@ use std::fmt::{Debug, Error, Formatter}; use crate::backend::{CommitId, Timestamp}; #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] -pub struct ViewId(pub Vec); +pub struct ViewId(Vec); impl Debug for ViewId { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { @@ -27,13 +27,29 @@ impl Debug for ViewId { } impl ViewId { + pub fn new(value: Vec) -> Self { + Self(value) + } + + pub fn from_hex(hex: &str) -> Self { + Self(hex::decode(hex).unwrap()) + } + + pub fn as_bytes(&self) -> &[u8] { + &self.0 + } + + pub fn to_bytes(&self) -> Vec { + self.0.clone() + } + pub fn hex(&self) -> String { hex::encode(&self.0) } } #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] -pub struct OperationId(pub Vec); +pub struct OperationId(Vec); impl Debug for OperationId { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { @@ -42,6 +58,22 @@ impl Debug for OperationId { } impl OperationId { + pub fn new(value: Vec) -> Self { + Self(value) + } + + pub fn from_hex(hex: &str) -> Self { + Self(hex::decode(hex).unwrap()) + } + + pub fn as_bytes(&self) -> &[u8] { + &self.0 + } + + pub fn to_bytes(&self) -> Vec { + self.0.clone() + } + pub fn hex(&self) -> String { hex::encode(&self.0) } diff --git a/lib/src/simple_op_store.rs b/lib/src/simple_op_store.rs index 9ce3f139c..ebe5e39ff 100644 --- a/lib/src/simple_op_store.rs +++ b/lib/src/simple_op_store.rs @@ -100,7 +100,7 @@ impl OpStore for SimpleOpStore { temp_file.as_file().write_all(&proto_bytes)?; - let id = ViewId(Blake2b::digest(&proto_bytes).to_vec()); + let id = ViewId::new(Blake2b::digest(&proto_bytes).to_vec()); persist_content_addressed_temp_file(temp_file, self.view_path(&id))?; Ok(id) @@ -123,7 +123,7 @@ impl OpStore for SimpleOpStore { temp_file.as_file().write_all(&proto_bytes)?; - let id = OperationId(Blake2b::digest(&proto_bytes).to_vec()); + let id = OperationId::new(Blake2b::digest(&proto_bytes).to_vec()); persist_content_addressed_temp_file(temp_file, self.operation_path(&id))?; Ok(id) @@ -178,18 +178,18 @@ fn operation_metadata_from_proto( fn operation_to_proto(operation: &Operation) -> crate::protos::op_store::Operation { let mut proto = crate::protos::op_store::Operation::new(); - proto.set_view_id(operation.view_id.0.clone()); + proto.set_view_id(operation.view_id.as_bytes().to_vec()); for parent in &operation.parents { - proto.parents.push(parent.0.clone()); + proto.parents.push(parent.to_bytes()); } proto.set_metadata(operation_metadata_to_proto(&operation.metadata)); proto } fn operation_from_proto(proto: &crate::protos::op_store::Operation) -> Operation { - let operation_id_from_proto = |parent: &Vec| OperationId(parent.clone()); + let operation_id_from_proto = |parent: &Vec| OperationId::new(parent.clone()); let parents = proto.parents.iter().map(operation_id_from_proto).collect(); - let view_id = ViewId(proto.view_id.to_vec()); + let view_id = ViewId::new(proto.view_id.clone()); let metadata = operation_metadata_from_proto(proto.get_metadata()); Operation { view_id, @@ -399,10 +399,10 @@ mod tests { let temp_dir = TempDir::new().unwrap(); let store = SimpleOpStore::init(temp_dir.path().to_owned()); let operation = Operation { - view_id: ViewId(b"aaa111".to_vec()), + view_id: ViewId::from_hex("aaa111"), parents: vec![ - OperationId(b"bbb111".to_vec()), - OperationId(b"bbb222".to_vec()), + OperationId::from_hex("bbb111"), + OperationId::from_hex("bbb222"), ], metadata: OperationMetadata { start_time: Timestamp { diff --git a/src/commands.rs b/src/commands.rs index 8f2e15678..e58ee43d8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -504,7 +504,7 @@ fn resolve_single_op_from_store( op_str: &str, ) -> Result { if let Ok(binary_op_id) = hex::decode(op_str) { - let op_id = OperationId(binary_op_id); + let op_id = OperationId::new(binary_op_id); match op_store.read_operation(&op_id) { Ok(operation) => { return Ok(Operation::new(op_store.clone(), op_id, operation));