bnr_xfs/callback_response/
operation.rs

1use std::fmt;
2
3use crate::{create_xfs_i4, impl_xfs_struct, xfs::OperationId};
4
5create_xfs_i4!(
6    OperationIdentificationId,
7    "identificationId",
8    "Represents the specific call instance for a particular callback operation."
9);
10
11/// Represents a response to a callback call made by the device.
12#[repr(C)]
13#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
14pub struct CallbackOperationResponse {
15    operation_id: OperationId,
16    identification_id: OperationIdentificationId,
17}
18
19impl CallbackOperationResponse {
20    /// Creates a new [CallbackOperationResponse].
21    pub const fn new() -> Self {
22        Self {
23            operation_id: OperationId::new(),
24            identification_id: OperationIdentificationId::new(),
25        }
26    }
27
28    /// Creates a new [CallbackOperationResponse] from the provided parameters.
29    pub const fn create(operation_id: i32, identification_id: i32) -> Self {
30        Self {
31            operation_id: OperationId::create(operation_id as u32),
32            identification_id: OperationIdentificationId::create(identification_id as u32),
33        }
34    }
35
36    /// Gets the [OperationId] value.
37    pub const fn operation_id(&self) -> u32 {
38        self.operation_id.inner()
39    }
40
41    /// Sets the [OperationId] value.
42    pub fn set_operation_id(&mut self, val: i32) {
43        self.operation_id = OperationId::create(val as u32);
44    }
45
46    /// Builder function that sets the [OperationId] value.
47    pub fn with_operation_id(mut self, val: i32) -> Self {
48        self.set_operation_id(val);
49        self
50    }
51
52    /// Gets the [IdentificationId] value.
53    pub const fn identification_id(&self) -> u32 {
54        self.identification_id.inner()
55    }
56
57    /// Sets the [IdentificationId] value.
58    pub fn set_identification_id(&mut self, val: i32) {
59        self.identification_id.set_inner(val as u32)
60    }
61
62    /// Builder function that sets the [IdentificationId] value.
63    pub fn with_identification_id(mut self, val: i32) -> Self {
64        self.set_identification_id(val);
65        self
66    }
67}
68
69impl fmt::Display for CallbackOperationResponse {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(f, "{{")?;
72        write!(f, r#""operation_id": {}, "#, self.operation_id())?;
73        write!(f, r#""identification_id": {}"#, self.identification_id())?;
74        write!(f, "}}")
75    }
76}
77
78impl_xfs_struct!(CallbackOperationResponse, "callbackResponse", [operation_id: OperationId, identification_id: OperationIdentificationId]);
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    use crate::xfs::{
85        self,
86        method_response::{XfsMethodResponse, XfsMethodResponseStruct},
87        params::XfsParam,
88    };
89    use crate::Result;
90
91    #[test]
92    fn test_callback_response_serde() -> Result<()> {
93        let exp_xml = r#"<?xml version="1.0" encoding="ISO-8859-1"?><methodResponse><params><param><value><struct><member><name>operationId</name><value><i4>6121</i4></value></member><member><name>identificationId</name><value><i4>4</i4></value></member></struct></value></param></params></methodResponse>"#;
94        let exp_res =
95            XfsMethodResponseStruct::new(XfsMethodResponse::new_params([XfsParam::create(
96                CallbackOperationResponse::create(6121, 4).into(),
97            )]));
98
99        assert_eq!(xfs::to_iso_string(&exp_res)?.as_str(), exp_xml);
100        assert_eq!(xfs::from_str::<XfsMethodResponseStruct>(exp_xml)?, exp_res);
101
102        Ok(())
103    }
104}