sonic_channel/commands/
flush.rs

1use super::StreamCommand;
2use crate::misc::*;
3use crate::protocol;
4use crate::result::*;
5
6/// Parameters for the `flush` command.
7#[derive(Debug)]
8pub struct FlushRequest(OptDest);
9
10impl FlushRequest {
11    /// Creates a new request to flush all data in the collection.
12    pub fn collection(collection: impl ToString) -> FlushRequest {
13        Self(OptDest::col(collection))
14    }
15
16    /// Creates a new request to flush all data in the collection bucket.
17    pub fn bucket(collection: impl ToString, bucket: impl ToString) -> FlushRequest {
18        Self(OptDest::col_buc(collection, bucket))
19    }
20
21    /// Creates a new request to flush all data in the collection bucket object.
22    pub fn object(
23        collection: impl ToString,
24        bucket: impl ToString,
25        object: impl ToString,
26    ) -> FlushRequest {
27        Self(OptDest::col_buc_obj(collection, bucket, object))
28    }
29}
30
31impl From<Dest> for FlushRequest {
32    fn from(d: Dest) -> Self {
33        Self(OptDest::from(d))
34    }
35}
36
37impl From<ObjDest> for FlushRequest {
38    fn from(d: ObjDest) -> Self {
39        Self(OptDest::from(d))
40    }
41}
42
43#[derive(Debug)]
44pub struct FlushCommand {
45    pub(crate) req: FlushRequest,
46}
47
48impl StreamCommand for FlushCommand {
49    type Response = usize;
50
51    fn request(&self) -> protocol::Request {
52        let dest = &self.req.0;
53
54        protocol::Request::Flush {
55            collection: dest.collection.clone(),
56            bucket: dest.bucket.clone(),
57            object: dest.object.clone(),
58        }
59    }
60
61    fn receive(&self, res: protocol::Response) -> Result<Self::Response> {
62        if let protocol::Response::Result(count) = res {
63            Ok(count)
64        } else {
65            Err(Error::WrongResponse)
66        }
67    }
68}