arma_rs/value/loadout/
assigned.rs

1use crate::{FromArma, FromArmaError, IntoArma, Value};
2
3#[derive(Debug, Default, Clone, PartialEq, Eq)]
4/// Assigned items in the loadout
5pub struct AssignedItems(String, String, String, String, String, String);
6impl AssignedItems {
7    /// The class name of the assigned map
8    #[must_use]
9    pub fn map(&self) -> &str {
10        &self.0
11    }
12
13    /// Set the class name of the assigned map
14    pub fn set_map(&mut self, map: String) {
15        self.0 = map;
16    }
17
18    /// The class name of the assigned terminal
19    #[must_use]
20    pub fn terminal(&self) -> &str {
21        &self.1
22    }
23
24    /// Set the class name of the assigned terminal
25    pub fn set_terminal(&mut self, terminal: String) {
26        self.1 = terminal;
27    }
28
29    /// The class name of the assigned radio
30    #[must_use]
31    pub fn radio(&self) -> &str {
32        &self.2
33    }
34
35    /// Set the class name of the assigned radio
36    pub fn set_radio(&mut self, radio: String) {
37        self.2 = radio;
38    }
39
40    /// The class name of the assigned compass
41    #[must_use]
42    pub fn compass(&self) -> &str {
43        &self.3
44    }
45
46    /// Set the class name of the assigned compass
47    pub fn set_compass(&mut self, compass: String) {
48        self.3 = compass;
49    }
50
51    /// The class name of the assigned watch
52    #[must_use]
53    pub fn watch(&self) -> &str {
54        &self.4
55    }
56
57    /// Set the class name of the assigned watch
58    pub fn set_watch(&mut self, watch: String) {
59        self.4 = watch;
60    }
61
62    /// The class name of the assigned NVG
63    #[must_use]
64    pub fn nvg(&self) -> &str {
65        &self.5
66    }
67
68    /// Set the class name of the assigned NVG
69    pub fn set_nvg(&mut self, nvg: String) {
70        self.5 = nvg;
71    }
72
73    /// Get all items
74    #[must_use]
75    pub fn classes(&self) -> [&str; 6] {
76        [
77            self.map(),
78            self.terminal(),
79            self.radio(),
80            self.compass(),
81            self.watch(),
82            self.nvg(),
83        ]
84    }
85
86    #[deprecated(note = "Use `classes` instead")]
87    #[must_use]
88    /// Get all items
89    pub fn items(&self) -> [&str; 6] {
90        self.classes()
91    }
92}
93impl FromArma for AssignedItems {
94    fn from_arma(s: String) -> Result<Self, FromArmaError> {
95        <(String, String, String, String, String, String)>::from_arma(s).map(
96            |(map, gps, radio, compass, watch, nvg)| Self(map, gps, radio, compass, watch, nvg),
97        )
98    }
99}
100impl IntoArma for AssignedItems {
101    fn to_arma(&self) -> Value {
102        Value::Array(vec![
103            Value::String(self.map().to_owned()),
104            Value::String(self.terminal().to_owned()),
105            Value::String(self.radio().to_owned()),
106            Value::String(self.compass().to_owned()),
107            Value::String(self.watch().to_owned()),
108            Value::String(self.nvg().to_owned()),
109        ])
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::AssignedItems;
116    use crate::{FromArma, IntoArma, Value};
117
118    #[test]
119    fn map() {
120        let mut assigned = AssignedItems::default();
121        assert_eq!(assigned.map(), "");
122        assigned.set_map("map".to_owned());
123        assert_eq!(assigned.map(), "map");
124    }
125
126    #[test]
127    fn terminal() {
128        let mut assigned = AssignedItems::default();
129        assert_eq!(assigned.terminal(), "");
130        assigned.set_terminal("terminal".to_owned());
131        assert_eq!(assigned.terminal(), "terminal");
132    }
133
134    #[test]
135    fn radio() {
136        let mut assigned = AssignedItems::default();
137        assert_eq!(assigned.radio(), "");
138        assigned.set_radio("radio".to_owned());
139        assert_eq!(assigned.radio(), "radio");
140    }
141
142    #[test]
143    fn compass() {
144        let mut assigned = AssignedItems::default();
145        assert_eq!(assigned.compass(), "");
146        assigned.set_compass("compass".to_owned());
147        assert_eq!(assigned.compass(), "compass");
148    }
149
150    #[test]
151    fn watch() {
152        let mut assigned = AssignedItems::default();
153        assert_eq!(assigned.watch(), "");
154        assigned.set_watch("watch".to_owned());
155        assert_eq!(assigned.watch(), "watch");
156    }
157
158    #[test]
159    fn nvg() {
160        let mut assigned = AssignedItems::default();
161        assert_eq!(assigned.nvg(), "");
162        assigned.set_nvg("nvg".to_owned());
163        assert_eq!(assigned.nvg(), "nvg");
164    }
165
166    #[test]
167    fn from_arma() {
168        let s = "[\"map\",\"terminal\",\"radio\",\"compass\",\"watch\",\"nvg\"]";
169        let assigned = AssignedItems::from_arma(s.to_owned()).unwrap();
170        assert_eq!(assigned.map(), "map");
171        assert_eq!(assigned.terminal(), "terminal");
172        assert_eq!(assigned.radio(), "radio");
173        assert_eq!(assigned.compass(), "compass");
174        assert_eq!(assigned.watch(), "watch");
175        assert_eq!(assigned.nvg(), "nvg");
176    }
177
178    #[test]
179    fn to_arma() {
180        let mut assigned = AssignedItems::default();
181        assigned.set_map("map".to_owned());
182        assigned.set_terminal("terminal".to_owned());
183        assigned.set_radio("radio".to_owned());
184        assigned.set_compass("compass".to_owned());
185        assigned.set_watch("watch".to_owned());
186        assigned.set_nvg("nvg".to_owned());
187        let s = assigned.to_arma();
188        assert_eq!(
189            s,
190            Value::Array(vec![
191                Value::String("map".to_owned()),
192                Value::String("terminal".to_owned()),
193                Value::String("radio".to_owned()),
194                Value::String("compass".to_owned()),
195                Value::String("watch".to_owned()),
196                Value::String("nvg".to_owned()),
197            ])
198        );
199    }
200}