Skip to content

Commit 33d6118

Browse files
committed
repo: plumbing to propagate error from backend initializer/loader functions
1 parent 3cdaa5b commit 33d6118

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lib/src/repo.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,19 @@ pub enum RepoInitError {
157157

158158
impl ReadonlyRepo {
159159
pub fn default_op_store_initializer() -> &'static OpStoreInitializer<'static> {
160-
&|_settings, store_path, root_data| Box::new(SimpleOpStore::init(store_path, root_data))
160+
&|_settings, store_path, root_data| Ok(Box::new(SimpleOpStore::init(store_path, root_data)))
161161
}
162162

163163
pub fn default_op_heads_store_initializer() -> &'static OpHeadsStoreInitializer<'static> {
164-
&|_settings, store_path| {
165-
let store = SimpleOpHeadsStore::init(store_path);
166-
Box::new(store)
167-
}
164+
&|_settings, store_path| Ok(Box::new(SimpleOpHeadsStore::init(store_path)))
168165
}
169166

170167
pub fn default_index_store_initializer() -> &'static IndexStoreInitializer<'static> {
171168
&|_settings, store_path| Ok(Box::new(DefaultIndexStore::init(store_path)?))
172169
}
173170

174171
pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer<'static> {
175-
&|_settings, store_path| Box::new(DefaultSubmoduleStore::init(store_path))
172+
&|_settings, store_path| Ok(Box::new(DefaultSubmoduleStore::init(store_path)))
176173
}
177174

178175
#[allow(clippy::too_many_arguments)]
@@ -200,14 +197,14 @@ impl ReadonlyRepo {
200197
let root_op_data = RootOperationData {
201198
root_commit_id: store.root_commit_id().clone(),
202199
};
203-
let op_store = op_store_initializer(settings, &op_store_path, root_op_data);
200+
let op_store = op_store_initializer(settings, &op_store_path, root_op_data)?;
204201
let op_store_type_path = op_store_path.join("type");
205202
fs::write(&op_store_type_path, op_store.name()).context(&op_store_type_path)?;
206203
let op_store: Arc<dyn OpStore> = Arc::from(op_store);
207204

208205
let op_heads_path = repo_path.join("op_heads");
209206
fs::create_dir(&op_heads_path).context(&op_heads_path)?;
210-
let op_heads_store = op_heads_store_initializer(settings, &op_heads_path);
207+
let op_heads_store = op_heads_store_initializer(settings, &op_heads_path)?;
211208
let op_heads_type_path = op_heads_path.join("type");
212209
fs::write(&op_heads_type_path, op_heads_store.name()).context(&op_heads_type_path)?;
213210
op_heads_store.update_op_heads(&[], op_store.root_operation_id())?;
@@ -222,7 +219,7 @@ impl ReadonlyRepo {
222219

223220
let submodule_store_path = repo_path.join("submodule_store");
224221
fs::create_dir(&submodule_store_path).context(&submodule_store_path)?;
225-
let submodule_store = submodule_store_initializer(settings, &submodule_store_path);
222+
let submodule_store = submodule_store_initializer(settings, &submodule_store_path)?;
226223
let submodule_store_type_path = submodule_store_path.join("type");
227224
fs::write(&submodule_store_type_path, submodule_store.name())
228225
.context(&submodule_store_type_path)?;
@@ -347,21 +344,28 @@ impl Repo for ReadonlyRepo {
347344

348345
pub type BackendInitializer<'a> =
349346
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn Backend>, BackendInitError> + 'a;
347+
#[rustfmt::skip] // auto-formatted line would exceed the maximum width
350348
pub type OpStoreInitializer<'a> =
351-
dyn Fn(&UserSettings, &Path, RootOperationData) -> Box<dyn OpStore> + 'a;
352-
pub type OpHeadsStoreInitializer<'a> = dyn Fn(&UserSettings, &Path) -> Box<dyn OpHeadsStore> + 'a;
349+
dyn Fn(&UserSettings, &Path, RootOperationData) -> Result<Box<dyn OpStore>, BackendInitError>
350+
+ 'a;
351+
pub type OpHeadsStoreInitializer<'a> =
352+
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn OpHeadsStore>, BackendInitError> + 'a;
353353
pub type IndexStoreInitializer<'a> =
354354
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn IndexStore>, BackendInitError> + 'a;
355355
pub type SubmoduleStoreInitializer<'a> =
356-
dyn Fn(&UserSettings, &Path) -> Box<dyn SubmoduleStore> + 'a;
356+
dyn Fn(&UserSettings, &Path) -> Result<Box<dyn SubmoduleStore>, BackendInitError> + 'a;
357357

358358
type BackendFactory =
359359
Box<dyn Fn(&UserSettings, &Path) -> Result<Box<dyn Backend>, BackendLoadError>>;
360-
type OpStoreFactory = Box<dyn Fn(&UserSettings, &Path, RootOperationData) -> Box<dyn OpStore>>;
361-
type OpHeadsStoreFactory = Box<dyn Fn(&UserSettings, &Path) -> Box<dyn OpHeadsStore>>;
360+
type OpStoreFactory = Box<
361+
dyn Fn(&UserSettings, &Path, RootOperationData) -> Result<Box<dyn OpStore>, BackendLoadError>,
362+
>;
363+
type OpHeadsStoreFactory =
364+
Box<dyn Fn(&UserSettings, &Path) -> Result<Box<dyn OpHeadsStore>, BackendLoadError>>;
362365
type IndexStoreFactory =
363366
Box<dyn Fn(&UserSettings, &Path) -> Result<Box<dyn IndexStore>, BackendLoadError>>;
364-
type SubmoduleStoreFactory = Box<dyn Fn(&UserSettings, &Path) -> Box<dyn SubmoduleStore>>;
367+
type SubmoduleStoreFactory =
368+
Box<dyn Fn(&UserSettings, &Path) -> Result<Box<dyn SubmoduleStore>, BackendLoadError>>;
365369

366370
pub fn merge_factories_map<F>(base: &mut HashMap<String, F>, ext: HashMap<String, F>) {
367371
for (name, factory) in ext {
@@ -416,14 +420,14 @@ impl Default for StoreFactories {
416420
factories.add_op_store(
417421
SimpleOpStore::name(),
418422
Box::new(|_settings, store_path, root_data| {
419-
Box::new(SimpleOpStore::load(store_path, root_data))
423+
Ok(Box::new(SimpleOpStore::load(store_path, root_data)))
420424
}),
421425
);
422426

423427
// OpHeadsStores
424428
factories.add_op_heads_store(
425429
SimpleOpHeadsStore::name(),
426-
Box::new(|_settings, store_path| Box::new(SimpleOpHeadsStore::load(store_path))),
430+
Box::new(|_settings, store_path| Ok(Box::new(SimpleOpHeadsStore::load(store_path)))),
427431
);
428432

429433
// Index
@@ -435,7 +439,7 @@ impl Default for StoreFactories {
435439
// SubmoduleStores
436440
factories.add_submodule_store(
437441
DefaultSubmoduleStore::name(),
438-
Box::new(|_settings, store_path| Box::new(DefaultSubmoduleStore::load(store_path))),
442+
Box::new(|_settings, store_path| Ok(Box::new(DefaultSubmoduleStore::load(store_path)))),
439443
);
440444

441445
factories
@@ -526,7 +530,7 @@ impl StoreFactories {
526530
store_type: op_store_type.to_string(),
527531
}
528532
})?;
529-
Ok(op_store_factory(settings, store_path, root_data))
533+
Ok(op_store_factory(settings, store_path, root_data)?)
530534
}
531535

532536
pub fn add_op_heads_store(&mut self, name: &str, factory: OpHeadsStoreFactory) {
@@ -547,7 +551,7 @@ impl StoreFactories {
547551
store: "operation heads",
548552
store_type: op_heads_store_type.to_string(),
549553
})?;
550-
Ok(op_heads_store_factory(settings, store_path))
554+
Ok(op_heads_store_factory(settings, store_path)?)
551555
}
552556

553557
pub fn add_index_store(&mut self, name: &str, factory: IndexStoreFactory) {
@@ -589,7 +593,7 @@ impl StoreFactories {
589593
store_type: submodule_store_type.to_string(),
590594
})?;
591595

592-
Ok(submodule_store_factory(settings, store_path))
596+
Ok(submodule_store_factory(settings, store_path)?)
593597
}
594598
}
595599

0 commit comments

Comments
 (0)