From 84a60d15bcb6e2da8001d132e631fdc32703a8f5 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 26 Jul 2023 11:39:43 -0700 Subject: [PATCH] op_store: make `ViewId` and `OperationId` implement `ObjectId` --- lib/src/backend.rs | 10 ++-- lib/src/op_store.rs | 69 +---------------------- lib/src/simple_op_heads_store.rs | 2 + lib/tests/test_operations.rs | 2 +- lib/tests/test_working_copy.rs | 2 +- lib/tests/test_working_copy_concurrent.rs | 2 +- src/commands/operation.rs | 1 + src/operation_templater.rs | 1 + 8 files changed, 16 insertions(+), 73 deletions(-) diff --git a/lib/src/backend.rs b/lib/src/backend.rs index f9cf5425d..cc06e6e0a 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -16,7 +16,7 @@ use std::any::Any; use std::collections::BTreeMap; -use std::fmt::{Debug, Error, Formatter}; +use std::fmt::Debug; use std::io::Read; use std::result::Result; use std::vec::Vec; @@ -43,14 +43,14 @@ macro_rules! id_type { #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] $vis struct $name(Vec); } - impl_id_type!($name); + $crate::backend::impl_id_type!($name); }; } macro_rules! impl_id_type { ($name:ident) => { - impl Debug for $name { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + impl std::fmt::Debug for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { f.debug_tuple(stringify!($name)).field(&self.hex()).finish() } } @@ -91,6 +91,8 @@ macro_rules! impl_id_type { }; } +pub(crate) use {id_type, impl_id_type}; + id_type!(pub CommitId); id_type!(pub ChangeId); id_type!(pub TreeId); diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index 46aa495f0..08f75e90c 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -22,7 +22,7 @@ use itertools::Itertools as _; use once_cell::sync::Lazy; use thiserror::Error; -use crate::backend::{CommitId, Timestamp}; +use crate::backend::{id_type, CommitId, ObjectId, Timestamp}; use crate::conflicts::Conflict; use crate::content_hash::ContentHash; @@ -53,71 +53,8 @@ impl WorkspaceId { } } -content_hash! { - #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] - pub struct ViewId(Vec); -} - -impl Debug for ViewId { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { - f.debug_tuple("ViewId").field(&self.hex()).finish() - } -} - -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) - } -} - -content_hash! { - #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] - pub struct OperationId(Vec); -} - -impl Debug for OperationId { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { - f.debug_tuple("OperationId").field(&self.hex()).finish() - } -} - -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) - } -} +id_type!(pub ViewId); +id_type!(pub OperationId); #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub struct RefTarget { diff --git a/lib/src/simple_op_heads_store.rs b/lib/src/simple_op_heads_store.rs index 98df310ac..90a41c4a6 100644 --- a/lib/src/simple_op_heads_store.rs +++ b/lib/src/simple_op_heads_store.rs @@ -18,6 +18,7 @@ use std::fmt::{Debug, Formatter}; use std::fs; use std::path::{Path, PathBuf}; +use crate::backend::ObjectId; use crate::lock::FileLock; use crate::op_heads_store::{OpHeadsStore, OpHeadsStoreLock}; use crate::op_store::OperationId; @@ -127,6 +128,7 @@ mod tests { use itertools::Itertools; + use crate::backend::ObjectId; use crate::op_heads_store::OpHeadsStore; use crate::op_store::OperationId; use crate::simple_op_heads_store::SimpleOpHeadsStore; diff --git a/lib/tests/test_operations.rs b/lib/tests/test_operations.rs index d6e209217..cc5fb7d2a 100644 --- a/lib/tests/test_operations.rs +++ b/lib/tests/test_operations.rs @@ -14,7 +14,7 @@ use std::path::Path; -use jj_lib::backend::CommitId; +use jj_lib::backend::{CommitId, ObjectId}; use jj_lib::repo::Repo; use test_case::test_case; use testutils::{create_random_commit, write_random_commit, TestRepo}; diff --git a/lib/tests/test_working_copy.rs b/lib/tests/test_working_copy.rs index 01c6b0a6b..33ac79871 100644 --- a/lib/tests/test_working_copy.rs +++ b/lib/tests/test_working_copy.rs @@ -21,7 +21,7 @@ use std::os::unix::net::UnixListener; use std::sync::Arc; use itertools::Itertools; -use jj_lib::backend::{TreeId, TreeValue}; +use jj_lib::backend::{ObjectId, TreeId, TreeValue}; use jj_lib::conflicts::Conflict; use jj_lib::fsmonitor::FsmonitorKind; #[cfg(unix)] diff --git a/lib/tests/test_working_copy_concurrent.rs b/lib/tests/test_working_copy_concurrent.rs index bfb64d42b..c446e4bd0 100644 --- a/lib/tests/test_working_copy_concurrent.rs +++ b/lib/tests/test_working_copy_concurrent.rs @@ -16,7 +16,7 @@ use std::cmp::max; use std::thread; use assert_matches::assert_matches; -use jj_lib::backend::TreeId; +use jj_lib::backend::{ObjectId, TreeId}; use jj_lib::op_store::OperationId; use jj_lib::repo::{Repo, StoreFactories}; use jj_lib::repo_path::RepoPath; diff --git a/src/commands/operation.rs b/src/commands/operation.rs index 4396aade0..fbf96eba9 100644 --- a/src/commands/operation.rs +++ b/src/commands/operation.rs @@ -1,6 +1,7 @@ use std::collections::{BTreeMap, BTreeSet}; use clap::Subcommand; +use jj_lib::backend::ObjectId; use jj_lib::op_store::{BranchTarget, RefTarget}; use jj_lib::operation; use jj_lib::repo::Repo; diff --git a/src/operation_templater.rs b/src/operation_templater.rs index dc1278f6f..ebc1c7a83 100644 --- a/src/operation_templater.rs +++ b/src/operation_templater.rs @@ -15,6 +15,7 @@ use std::io; use itertools::Itertools as _; +use jj_lib::backend::ObjectId; use jj_lib::op_store::{OperationId, OperationMetadata}; use jj_lib::operation::Operation; use jj_lib::repo::ReadonlyRepo;