switchbot_api/
device_list.rs1use super::Device;
2use std::ops::{Deref, DerefMut};
3
4#[derive(Debug, Default, serde::Deserialize)]
5#[serde(transparent)] pub struct DeviceList {
7 devices: Vec<Device>,
8}
9
10impl DeviceList {
11 pub fn new() -> Self {
12 Self {
13 devices: Vec::new(),
14 }
15 }
16
17 #[allow(dead_code)]
18 pub fn with_capacity(capacity: usize) -> Self {
19 Self {
20 devices: Vec::with_capacity(capacity),
21 }
22 }
23
24 pub fn push(&mut self, device: Device) {
25 self.devices.push(device);
26 }
27
28 pub fn extend<T: IntoIterator<Item = Device>>(&mut self, iter: T) {
29 self.devices.extend(iter);
30 }
31
32 pub fn index_by_device_id(&self, device_id: &str) -> Option<usize> {
33 self.devices.iter().position(|d| d.device_id() == device_id)
34 }
35
36 pub fn iter(&self) -> std::slice::Iter<'_, Device> {
38 self.devices.iter()
39 }
40
41 pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Device> {
42 self.devices.iter_mut()
43 }
44
45 pub fn get(&self, index: usize) -> Option<&Device> {
46 self.devices.get(index)
47 }
48
49 pub fn get_mut(&mut self, index: usize) -> Option<&mut Device> {
50 self.devices.get_mut(index)
51 }
52
53 pub fn is_empty(&self) -> bool {
54 self.devices.is_empty()
55 }
56
57 pub fn len(&self) -> usize {
58 self.devices.len()
59 }
60}
61
62impl Deref for DeviceList {
63 type Target = Vec<Device>;
64
65 fn deref(&self) -> &Self::Target {
66 &self.devices
67 }
68}
69
70impl DerefMut for DeviceList {
71 fn deref_mut(&mut self) -> &mut Self::Target {
72 &mut self.devices
73 }
74}
75
76impl IntoIterator for DeviceList {
77 type Item = Device;
78 type IntoIter = std::vec::IntoIter<Self::Item>;
79
80 fn into_iter(self) -> Self::IntoIter {
81 self.devices.into_iter()
82 }
83}