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
59
60
61
62
63
64
65
66
67
68
use super::StreamCommand;
use crate::misc::*;
use crate::protocol;
use crate::result::*;

/// Parameters for the `flush` command.
#[derive(Debug)]
pub struct FlushRequest(OptDest);

impl FlushRequest {
    /// Creates a new request to flush all data in the collection.
    pub fn collection(collection: impl ToString) -> FlushRequest {
        Self(OptDest::col(collection))
    }

    /// Creates a new request to flush all data in the collection bucket.
    pub fn bucket(collection: impl ToString, bucket: impl ToString) -> FlushRequest {
        Self(OptDest::col_buc(collection, bucket))
    }

    /// Creates a new request to flush all data in the collection bucket object.
    pub fn object(
        collection: impl ToString,
        bucket: impl ToString,
        object: impl ToString,
    ) -> FlushRequest {
        Self(OptDest::col_buc_obj(collection, bucket, object))
    }
}

impl From<Dest> for FlushRequest {
    fn from(d: Dest) -> Self {
        Self(OptDest::from(d))
    }
}

impl From<ObjDest> for FlushRequest {
    fn from(d: ObjDest) -> Self {
        Self(OptDest::from(d))
    }
}

#[derive(Debug)]
pub struct FlushCommand {
    pub(crate) req: FlushRequest,
}

impl StreamCommand for FlushCommand {
    type Response = usize;

    fn request(&self) -> protocol::Request {
        let dest = &self.req.0;

        protocol::Request::Flush {
            collection: dest.collection.clone(),
            bucket: dest.bucket.clone(),
            object: dest.object.clone(),
        }
    }

    fn receive(&self, res: protocol::Response) -> Result<Self::Response> {
        if let protocol::Response::Result(count) = res {
            Ok(count)
        } else {
            Err(Error::WrongResponse)
        }
    }
}