blazing_agi/command/
set_variable.rs1use super::*;
4
5#[derive(Debug)]
18pub struct SetVariable {
19 var_name: String,
20 value: String,
21}
22impl SetVariable {
23 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#[derive(Debug, PartialEq)]
40pub struct SetVariableResponse {}
41impl InnerAGIResponse for SetVariableResponse {}
42impl<'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}