fjall_cli/command/keyspace_command/
clear_command.rs1use errgonomic::{handle, handle_bool};
2use fjall::{Database, KeyspaceCreateOptions, PersistMode};
3use std::process::ExitCode;
4use thiserror::Error;
5
6#[derive(clap::Parser, Clone, Debug)]
7pub struct ClearCommand {}
8
9impl ClearCommand {
10 pub async fn run(self, db: &Database, keyspace: impl Into<String>) -> Result<ExitCode, ClearCommandRunError> {
11 use ClearCommandRunError::*;
12 let keyspace = keyspace.into();
13 handle_bool!(!db.keyspace_exists(&keyspace), KeyspaceNotFound, keyspace);
14 let keyspace_handle = handle!(db.keyspace(&keyspace, KeyspaceCreateOptions::default), KeyspaceFailed, keyspace);
15 eprintln!("This command may not clear the keyspace due to a bug in fjall v3.0.1");
16 handle!(keyspace_handle.clear(), ClearFailed, keyspace);
17 handle!(db.persist(PersistMode::SyncAll), PersistFailed, keyspace);
18 Ok(ExitCode::SUCCESS)
19 }
20}
21
22#[derive(Error, Debug)]
23pub enum ClearCommandRunError {
24 #[error("keyspace '{keyspace}' not found")]
25 KeyspaceNotFound { keyspace: String },
26
27 #[error("failed to open keyspace '{keyspace}'")]
28 KeyspaceFailed { source: fjall::Error, keyspace: String },
29
30 #[error("failed to clear keyspace '{keyspace}'")]
31 ClearFailed { source: fjall::Error, keyspace: String },
32
33 #[error("failed to persist keyspace clear operation for keyspace '{keyspace}'")]
34 PersistFailed { source: fjall::Error, keyspace: String },
35}