blazing_agi/command/
set_variable.rs

1//! Defines the `SET VARIABLE` AGI command.
2//! See also the [official documentation](https://docs.asterisk.org/Asterisk_22_Documentation/API_Documentation/AGI_Commands/get_full_variable/)
3use super::*;
4
5/// The Set Variable command.
6///
7/// Use with
8/// ```
9/// use blazing_agi::command::SetVariable;
10/// let cmd = SetVariable::new("TheVariable".to_owned(), "TheValue".to_owned());
11/// // Will send:
12/// assert_eq!(cmd.to_string(), "SET VARIABLE \"TheVariable\" \"TheValue\"\n")
13/// ```
14///
15/// The associated [`InnerAGIResponse`] from [`send_command`](crate::connection::Connection::send_command) is
16/// [`SetVariableResponse`].
17#[derive(Debug)]
18pub struct SetVariable {
19    var_name: String,
20    value: String,
21}
22impl SetVariable {
23    /// Create [`SetVariable`]. When sent, this will set `var_name` to `value`.
24    pub fn new(var_name: String, value: String) -> Self {
25        Self { var_name, value }
26    }
27}
28impl core::fmt::Display for SetVariable {
29    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
30        writeln!(f, "SET VARIABLE \"{}\" \"{}\"", self.var_name, self.value)
31    }
32}
33impl AGICommand for SetVariable {
34    type Response = SetVariableResponse;
35}
36
37/// The responses we can get when sending [`SetVariableResponse`] that returned 200.
38/// There is only one acceptable response: `200 result=1`, so this is the empty struct.
39#[derive(Debug, PartialEq)]
40pub struct SetVariableResponse {}
41impl InnerAGIResponse for SetVariableResponse {}
42/// Convert from a tuple `(result, operational_data)` to [`SetVariableResponse`]. This is used
43/// internally when parsing AGI responses to sending a [`SetVariable`] command.
44impl<'a> TryFrom<(&'a str, Option<&'a str>)> for SetVariableResponse {
45    type Error = AGIStatusParseError;
46    fn try_from((result, op_data): (&str, Option<&str>)) -> Result<Self, Self::Error> {
47        let res_parsed = result.parse::<u16>();
48        match res_parsed {
49            Ok(1) => Ok(SetVariableResponse {}),
50            _ => Err(AGIStatusParseError {
51                result: result.to_owned(),
52                op_data: op_data.map(|x| x.to_owned()),
53                response_to_command: "SET VARIABLE",
54            }),
55        }
56    }
57}
58
59#[cfg(test)]
60mod test {
61    use super::*;
62
63    #[test]
64    fn run_normal_set() {
65        let cmd = SetVariable::new("TEST_VAR_NAME".to_owned(), "the-value".to_owned());
66        assert_eq!(
67            cmd.to_string(),
68            "SET VARIABLE \"TEST_VAR_NAME\" \"the-value\"\n"
69        );
70    }
71
72    #[test]
73    fn parse_success() {
74        assert_eq!(
75            SetVariableResponse::try_from(("1", None)).unwrap(),
76            SetVariableResponse {}
77        );
78    }
79
80    #[test]
81    fn parse_incorrect_result() {
82        assert_eq!(
83            SetVariableResponse::try_from(("0", Some("other stuff"))),
84            Err(AGIStatusParseError {
85                result: "0".to_owned(),
86                op_data: Some("other stuff".to_owned()),
87                response_to_command: "SET VARIABLE"
88            })
89        );
90    }
91}