bnr_xfs/xfs/
fault.rs

1//! XFS `fault` entry types.
2
3use std::fmt;
4
5use super::{
6    value::XfsValue,
7    xfs_struct::{XfsMember, XfsStruct},
8};
9use crate::impl_default;
10
11/// Represents an XFS `fault` entry in an
12/// [XfsMethodResponse](super::method_response::XfsMethodResponse).
13#[repr(C)]
14#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
15#[serde(rename = "fault")]
16pub struct XfsFault {
17    value: XfsValue,
18}
19
20impl fmt::Display for XfsFault {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "{}", self.value)
23    }
24}
25
26impl XfsFault {
27    /// Creates a new [XfsFault].
28    pub fn new() -> Self {
29        Self {
30            value: XfsValue::new().with_xfs_struct(XfsStruct::create([
31                XfsMember::create("faultCode", XfsValue::new().with_i4(0)),
32                XfsMember::create("faultString", XfsValue::new().with_string(String::new())),
33            ])),
34        }
35    }
36
37    /// Creates a new [XfsFault] with the provided fault code.
38    pub fn create<S: Into<String>>(code: i32, string: S) -> Self {
39        Self {
40            value: XfsValue::new().with_xfs_struct(XfsStruct::create([
41                XfsMember::create("faultCode", XfsValue::new().with_i4(code)),
42                XfsMember::create("faultString", XfsValue::new().with_string(string.into())),
43            ])),
44        }
45    }
46
47    /// Gets the fault code value.
48    pub fn code(&self) -> i32 {
49        match self.value.xfs_struct() {
50            Some(fc) => match fc.members().first() {
51                Some(m) => match m.inner().value().i4() {
52                    Some(val) => *val,
53                    None => 0,
54                },
55                None => 0,
56            },
57            None => 0,
58        }
59    }
60
61    /// Sets the fault code value.
62    pub fn set_code(&mut self, code: i32) {
63        if let Some(fc) = self.value.xfs_struct.as_mut() {
64            if let Some(m) = fc.members_mut().get_mut(0) {
65                m.inner_mut().set_value(XfsValue::new().with_i4(code));
66            }
67        }
68    }
69
70    /// Builder function that sets the fault code value.
71    pub fn with_code(mut self, code: i32) -> Self {
72        self.set_code(code);
73        self
74    }
75
76    /// Gets the fault string value.
77    pub fn fault_string(&self) -> &str {
78        match self.value.xfs_struct() {
79            Some(fc) => match fc.members().get(1) {
80                Some(m) => match m.inner().value().string() {
81                    Some(val) => val,
82                    None => "",
83                },
84                None => "",
85            },
86            None => "",
87        }
88    }
89
90    /// Sets the fault code value.
91    pub fn set_fault_string<S: Into<String>>(&mut self, string: S) {
92        if let Some(fc) = self.value.xfs_struct.as_mut() {
93            if let Some(m) = fc.members_mut().get_mut(1) {
94                m.inner_mut()
95                    .set_value(XfsValue::new().with_string(string.into()));
96            }
97        }
98    }
99
100    /// Builder function that sets the fault code value.
101    pub fn with_fault_string<S: Into<String>>(mut self, string: S) -> Self {
102        self.set_fault_string(string);
103        self
104    }
105}
106
107impl_default!(XfsFault);
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    use crate::{xfs, Result};
113
114    #[test]
115    fn test_fault_serde() -> Result<()> {
116        let exp_xml = r#"<?xml version="1.0" encoding="UTF-8"?><fault><value><struct><member><name>faultCode</name><value><i4>0</i4></value></member><member><name>faultString</name><value><string></string></value></member></struct></value></fault>"#;
117        let exp_fault = XfsFault::new();
118        let xml_str = xfs::to_string(&exp_fault)?;
119
120        assert_eq!(xml_str.as_str(), exp_xml);
121        assert_eq!(xfs::from_str::<XfsFault>(exp_xml)?, exp_fault);
122
123        let exp_xml = r#"<?xml version="1.0" encoding="UTF-8"?><fault><value><struct><member><name>faultCode</name><value><i4>1010</i4></value></member><member><name>faultString</name><value><string></string></value></member></struct></value></fault>"#;
124        let exp_fault = XfsFault::create(1010, "");
125        let xml_str = xfs::to_string(&exp_fault)?;
126
127        assert_eq!(xml_str.as_str(), exp_xml);
128        assert_eq!(xfs::from_str::<XfsFault>(exp_xml)?, exp_fault);
129
130        Ok(())
131    }
132
133    #[test]
134    fn test_fault_accessors() -> Result<()> {
135        let exp_code = 6080;
136        let mut fault = XfsFault::create(exp_code, "");
137
138        assert_eq!(fault.code(), exp_code);
139        assert_eq!(XfsFault::new().code(), 0);
140        assert_eq!(XfsFault::new().with_code(exp_code).code(), exp_code);
141
142        let new_code = 1010;
143        fault.set_code(new_code);
144
145        assert_eq!(fault.code(), new_code);
146
147        Ok(())
148    }
149}