From f8be0b20300e979a014f636cd27dcc5a139aab28 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 14 Oct 2023 06:09:33 -0700 Subject: [PATCH] backends: deduplicate definition of backend names I copied the example set by `DefaultSubmoduleStore`. --- lib/src/default_index_store.rs | 6 +++++- lib/src/default_submodule_store.rs | 2 +- lib/src/git_backend.rs | 6 +++++- lib/src/local_backend.rs | 6 +++++- lib/src/repo.rs | 10 +++++----- lib/src/simple_op_heads_store.rs | 6 +++++- lib/src/simple_op_store.rs | 6 +++++- 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 98b8289e4..f66ce864a 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -60,6 +60,10 @@ pub struct DefaultIndexStore { } impl DefaultIndexStore { + pub fn name() -> &'static str { + "default" + } + pub fn init(dir: &Path) -> Self { std::fs::create_dir(dir.join("operations")).unwrap(); DefaultIndexStore { @@ -207,7 +211,7 @@ impl IndexStore for DefaultIndexStore { } fn name(&self) -> &str { - "default" + Self::name() } fn get_index_at_op(&self, op: &Operation, store: &Arc) -> Box { diff --git a/lib/src/default_submodule_store.rs b/lib/src/default_submodule_store.rs index d44f2d776..e15000fc2 100644 --- a/lib/src/default_submodule_store.rs +++ b/lib/src/default_submodule_store.rs @@ -45,6 +45,6 @@ impl DefaultSubmoduleStore { impl SubmoduleStore for DefaultSubmoduleStore { fn name(&self) -> &str { - DefaultSubmoduleStore::name() + Self::name() } } diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 5058c09d9..8c4a89b6d 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -102,6 +102,10 @@ pub struct GitBackend { } impl GitBackend { + pub fn name() -> &'static str { + "git" + } + fn new(repo: git2::Repository, extra_metadata_store: TableStore) -> Self { let root_commit_id = CommitId::from_bytes(&[0; HASH_LENGTH]); let root_change_id = ChangeId::from_bytes(&[0; CHANGE_ID_LENGTH]); @@ -487,7 +491,7 @@ impl Backend for GitBackend { } fn name(&self) -> &str { - "git" + Self::name() } fn commit_id_length(&self) -> usize { diff --git a/lib/src/local_backend.rs b/lib/src/local_backend.rs index e9a13cc7d..f02e8c89f 100644 --- a/lib/src/local_backend.rs +++ b/lib/src/local_backend.rs @@ -68,6 +68,10 @@ pub struct LocalBackend { } impl LocalBackend { + pub fn name() -> &'static str { + "local" + } + pub fn init(store_path: &Path) -> Self { fs::create_dir(store_path.join("commits")).unwrap(); fs::create_dir(store_path.join("trees")).unwrap(); @@ -122,7 +126,7 @@ impl Backend for LocalBackend { } fn name(&self) -> &str { - "local" + Self::name() } fn commit_id_length(&self) -> usize { diff --git a/lib/src/repo.rs b/lib/src/repo.rs index e8889c42c..4d71387e3 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -359,29 +359,29 @@ impl Default for StoreFactories { // Backends factories.add_backend( - "local", + LocalBackend::name(), Box::new(|store_path| Ok(Box::new(LocalBackend::load(store_path)))), ); factories.add_backend( - "git", + GitBackend::name(), Box::new(|store_path| Ok(Box::new(GitBackend::load(store_path)?))), ); // OpStores factories.add_op_store( - "simple_op_store", + SimpleOpStore::name(), Box::new(|store_path| Box::new(SimpleOpStore::load(store_path))), ); // OpHeadsStores factories.add_op_heads_store( - "simple_op_heads_store", + SimpleOpHeadsStore::name(), Box::new(|store_path| Box::new(SimpleOpHeadsStore::load(store_path))), ); // Index factories.add_index_store( - "default", + DefaultIndexStore::name(), Box::new(|store_path| Box::new(DefaultIndexStore::load(store_path))), ); diff --git a/lib/src/simple_op_heads_store.rs b/lib/src/simple_op_heads_store.rs index 90a41c4a6..1874b2159 100644 --- a/lib/src/simple_op_heads_store.rs +++ b/lib/src/simple_op_heads_store.rs @@ -37,6 +37,10 @@ impl Debug for SimpleOpHeadsStore { } impl SimpleOpHeadsStore { + pub fn name() -> &'static str { + "simple_op_heads_store" + } + pub fn init(dir: &Path) -> Self { let op_heads_dir = dir.join("heads"); fs::create_dir(&op_heads_dir).unwrap(); @@ -85,7 +89,7 @@ impl OpHeadsStoreLock<'_> for SimpleOpHeadsStoreLock<'_> { impl OpHeadsStore for SimpleOpHeadsStore { fn name(&self) -> &str { - "simple_op_heads_store" + Self::name() } fn add_op_head(&self, id: &OperationId) { diff --git a/lib/src/simple_op_store.rs b/lib/src/simple_op_store.rs index 3575388e9..e90f4ac71 100644 --- a/lib/src/simple_op_store.rs +++ b/lib/src/simple_op_store.rs @@ -61,6 +61,10 @@ pub struct SimpleOpStore { } impl SimpleOpStore { + pub fn name() -> &'static str { + "simple_op_store" + } + /// Creates an empty OpStore, panics if it already exists pub fn init(store_path: &Path) -> Self { fs::create_dir(store_path.join("views")).unwrap(); @@ -88,7 +92,7 @@ impl SimpleOpStore { impl OpStore for SimpleOpStore { fn name(&self) -> &str { - "simple_op_store" + Self::name() } fn read_view(&self, id: &ViewId) -> OpStoreResult {