Skip to main content

fjall_cli/command/keyspace_command/
clear_command.rs

1use 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        handle!(keyspace_handle.clear(), ClearFailed, keyspace);
16        handle!(db.persist(PersistMode::SyncAll), PersistFailed, keyspace);
17        Ok(ExitCode::SUCCESS)
18    }
19}
20
21#[derive(Error, Debug)]
22pub enum ClearCommandRunError {
23    #[error("keyspace '{keyspace}' not found")]
24    KeyspaceNotFound { keyspace: String },
25
26    #[error("failed to open keyspace '{keyspace}'")]
27    KeyspaceFailed { source: fjall::Error, keyspace: String },
28
29    #[error("failed to clear keyspace '{keyspace}'")]
30    ClearFailed { source: fjall::Error, keyspace: String },
31
32    #[error("failed to persist keyspace clear operation for keyspace '{keyspace}'")]
33    PersistFailed { source: fjall::Error, keyspace: String },
34}