1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{cmd, resp::ResultValueExt, Database, Result};
use async_trait::async_trait;

/// Database flushing mode
pub enum FlushingMode {
    Default,
    /// Flushes the database(s) asynchronously
    Async,
    /// Flushed the database(s) synchronously
    Sync,
}

impl Default for FlushingMode {
    fn default() -> Self {
        FlushingMode::Default
    }
}

/// A group of Redis commands related to Server Management
/// # See Also
/// [Redis Server Management Commands](https://redis.io/commands/?group=server)
#[async_trait]
pub trait ServerCommands {
    /// Delete all the keys of the currently selected DB.
    ///
    /// # See Also
    /// [https://redis.io/commands/flushdb/](https://redis.io/commands/flushdb/)
    async fn flushdb(&self, flushing_mode: FlushingMode) -> Result<()>;

    /// Delete all the keys of all the existing databases, not just the currently selected one.
    ///
    /// # See Also
    /// [https://redis.io/commands/flushall/](https://redis.io/commands/flushall/)
    async fn flushall(&self, flushing_mode: FlushingMode) -> Result<()>;
}

#[async_trait]
impl ServerCommands for Database {
    async fn flushdb(&self, flushing_mode: FlushingMode) -> Result<()> {
        let mut command = cmd("FLUSHDB");
        match flushing_mode {
            FlushingMode::Default => (),
            FlushingMode::Async => command = command.arg("ASYNC"),
            FlushingMode::Sync => command = command.arg("SYNC"),
        }
        self.send(command).await.into_unit()
    }

    async fn flushall(&self, flushing_mode: FlushingMode) -> Result<()> {
        let mut command = cmd("FLUSHALL");
        match flushing_mode {
            FlushingMode::Default => (),
            FlushingMode::Async => command = command.arg("ASYNC"),
            FlushingMode::Sync => command = command.arg("SYNC"),
        }
        self.send(command).await.into_unit()
    }
}