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
use super::StreamCommand;
use crate::result::*;
use std::fmt;

#[derive(Debug)]
pub enum TriggerAction<'a> {
    Consolidate,
    Backup(&'a str),
    Restore(&'a str),
}

impl Default for TriggerAction<'_> {
    fn default() -> Self {
        TriggerAction::Consolidate
    }
}

impl fmt::Display for TriggerAction<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        match self {
            TriggerAction::Consolidate => write!(f, "consolidate"),
            TriggerAction::Backup(data) => write!(f, "backup {}", data),
            TriggerAction::Restore(data) => write!(f, "restore {}", data),
        }
    }
}

#[derive(Debug, Default)]
pub struct TriggerCommand<'a> {
    pub action: TriggerAction<'a>,
}

impl StreamCommand for TriggerCommand<'_> {
    type Response = bool;

    fn message(&self) -> String {
        format!("TRIGGER {}\r\n", self.action)
    }

    fn receive(&self, message: String) -> Result<Self::Response> {
        if message == "OK\r\n" {
            Ok(true)
        } else {
            Err(Error::new(ErrorKind::WrongSonicResponse))
        }
    }
}