| 1 | use std::collections::{ |
| 2 | linked_list::{Iter, IterMut}, |
| 3 | LinkedList, |
| 4 | }; |
| 5 | use std::sync::Arc; |
| 6 | |
| 7 | use crate::{Udev, UdevDevice}; |
| 8 | |
| 9 | /// Convenience alias for a [LinkedList] of [UdevEntry]. |
| 10 | pub type UdevEntryList = LinkedList<UdevEntry>; |
| 11 | |
| 12 | /// Represents a list wrapper around a [UdevEntryList]. |
| 13 | #[repr(C)] |
| 14 | #[derive(Clone, Debug, Default, PartialEq)] |
| 15 | pub struct UdevList { |
| 16 | udev: Arc<Udev>, |
| 17 | list: UdevEntryList, |
| 18 | entries_cur: usize, |
| 19 | entries_max: usize, |
| 20 | unique: bool, |
| 21 | } |
| 22 | |
| 23 | impl UdevList { |
| 24 | /// Creates a new [UdevList]. |
| 25 | pub const fn new(udev: Arc<Udev>) -> Self { |
| 26 | Self { |
| 27 | udev, |
| 28 | list: LinkedList::new(), |
| 29 | entries_cur: 0, |
| 30 | entries_max: 0, |
| 31 | unique: true, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// Creates a new [UdevList] from the provided parameters. |
| 36 | pub const fn create(udev: Arc<Udev>, list: UdevEntryList) -> Self { |
| 37 | Self { |
| 38 | udev, |
| 39 | list, |
| 40 | entries_cur: 0, |
| 41 | entries_max: 0, |
| 42 | unique: true, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Gets an [`Iterator`] over [UdevEntry] items. |
| 47 | pub fn iter(&self) -> Iter<UdevEntry> { |
| 48 | self.list.iter() |
| 49 | } |
| 50 | |
| 51 | /// Gets an [`Iterator`] over [UdevEntry] items. |
| 52 | pub fn iter_mut(&mut self) -> IterMut<UdevEntry> { |
| 53 | self.list.iter_mut() |
| 54 | } |
| 55 | |
| 56 | /// Gets a reference to the [UdevEntryList]. |
| 57 | pub fn list(&self) -> &UdevEntryList { |
| 58 | &self.list |
| 59 | } |
| 60 | |
| 61 | /// Gets a mutable reference to the [UdevEntryList]. |
| 62 | pub fn list_mut(&mut self) -> &mut UdevEntryList { |
| 63 | &mut self.list |
| 64 | } |
| 65 | |
| 66 | /// Sets the [UdevEntryList]. |
| 67 | pub fn set_list<L: Into<UdevEntryList>>(&mut self, list: L) { |
| 68 | self.list = list.into(); |
| 69 | } |
| 70 | |
| 71 | /// Builder function that sets the [UdevEntryList]. |
| 72 | pub fn with_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self { |
| 73 | self.set_list(list); |
| 74 | self |
| 75 | } |
| 76 | |
| 77 | /// Gets the length of the [UdevEntry] list. |
| 78 | pub fn len(&self) -> usize { |
| 79 | self.list.len() |
| 80 | } |
| 81 | |
| 82 | /// Gets whether the [UdevEntryList] is empty. |
| 83 | pub fn is_empty(&self) -> bool { |
| 84 | self.list.is_empty() |
| 85 | } |
| 86 | |
| 87 | /// Clears the list of all entries. |
| 88 | pub fn clear(&mut self) { |
| 89 | self.list.clear(); |
| 90 | } |
| 91 | |
| 92 | /// Gets an optional reference to the first [UdevEntry] in the [UdevEntryList]. |
| 93 | pub fn entry(&self) -> Option<&UdevEntry> { |
| 94 | self.list.front() |
| 95 | } |
| 96 | |
| 97 | /// Gets an optional mutable reference to the first [UdevEntry] in the [UdevEntryList]. |
| 98 | pub fn entry_mut(&mut self) -> Option<&mut UdevEntry> { |
| 99 | self.list.front_mut() |
| 100 | } |
| 101 | |
| 102 | /// Gets an optional reference to an [UdevEntry] with a matching `name`. |
| 103 | pub fn entry_by_name(&self, name: &str) -> Option<&UdevEntry> { |
| 104 | self.list.iter().find(|e| e.name() == name) |
| 105 | } |
| 106 | |
| 107 | /// Gets an optional mutable reference to an [UdevEntry] with a matching `name`. |
| 108 | pub fn entry_by_name_mut(&mut self, name: &str) -> Option<&mut UdevEntry> { |
| 109 | self.list.iter_mut().find(|e| e.name() == name) |
| 110 | } |
| 111 | |
| 112 | /// Gets the next [UdevEntry] in the list. |
| 113 | pub fn next_entry(&self) -> Option<&UdevEntry> { |
| 114 | self.list.iter().nth(self.entries_cur) |
| 115 | } |
| 116 | |
| 117 | /// Gets the next [UdevEntry] in the list. |
| 118 | pub fn next_entry_mut(&mut self) -> Option<&mut UdevEntry> { |
| 119 | self.list.iter_mut().nth(self.entries_cur) |
| 120 | } |
| 121 | |
| 122 | /// Adds an entry to the list. |
| 123 | /// |
| 124 | /// If an [UdevEntry] with the same `name` exists, the `value` will be updated. |
| 125 | /// |
| 126 | /// If `value` is empty, the entry value with be empty. |
| 127 | pub fn add_entry(&mut self, name: &str, value: &str) -> Option<&UdevEntry> { |
| 128 | if self.unique() && self.entry_by_name(name).is_some() { |
| 129 | let existing = self.entry_by_name_mut(name).unwrap(); |
| 130 | log::trace!("Updating property, {name}: {} => {value}", existing.value()); |
| 131 | existing.set_value(value); |
| 132 | Some(existing) |
| 133 | } else { |
| 134 | log::trace!("Adding property, {name}: {value}"); |
| 135 | self.list |
| 136 | .push_back(UdevEntry::new().with_name(name).with_value(value)); |
| 137 | self.list.back() |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Removes an [UdevEntry] if an entry exists with a matching `name`. |
| 142 | pub fn remove_entry(&mut self, name: &str) { |
| 143 | if let Some(pos) = self.list.iter().position(|e| e.name() == name) { |
| 144 | let mut ext = self.list.split_off(pos); |
| 145 | |
| 146 | if ext.len() > 1 { |
| 147 | ext.pop_front(); |
| 148 | self.list.append(&mut ext); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /// Gets the current [UdevEntry]. |
| 154 | pub const fn entries_cur(&self) -> usize { |
| 155 | self.entries_cur |
| 156 | } |
| 157 | |
| 158 | /// Gets the maximum number of [UdevEntry] items. |
| 159 | pub const fn entries_max(&self) -> usize { |
| 160 | self.entries_max |
| 161 | } |
| 162 | |
| 163 | /// Gets whether the [UdevList] is unique. |
| 164 | pub const fn unique(&self) -> bool { |
| 165 | self.unique |
| 166 | } |
| 167 | |
| 168 | /// Gets whether the [UdevDevice] matches an [UdevEntry] in the list. |
| 169 | pub fn has_tag(&self, device: &UdevDevice) -> bool { |
| 170 | if self.is_empty() { |
| 171 | true |
| 172 | } else { |
| 173 | self.iter() |
| 174 | .filter(|e| device.tags_list().entry_by_name(e.name()).is_some()) |
| 175 | .count() |
| 176 | != 0 |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /// UDEV list entry. |
| 182 | /// |
| 183 | /// An entry contains contains a name, and optionally a value. |
| 184 | #[repr(C)] |
| 185 | #[derive(Clone, Debug, Default, PartialEq)] |
| 186 | pub struct UdevEntry { |
| 187 | name: String, |
| 188 | value: String, |
| 189 | num: i32, |
| 190 | } |
| 191 | |
| 192 | impl UdevEntry { |
| 193 | /// Creates a new [UdevEntry]. |
| 194 | pub const fn new() -> Self { |
| 195 | Self { |
| 196 | name: String::new(), |
| 197 | value: String::new(), |
| 198 | num: 0, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Gets the [UdevEntry] name. |
| 203 | pub fn name(&self) -> &str { |
| 204 | self.name.as_str() |
| 205 | } |
| 206 | |
| 207 | /// Sets the [UdevEntry] name. |
| 208 | pub fn set_name<N: Into<String>>(&mut self, name: N) { |
| 209 | self.name = name.into(); |
| 210 | } |
| 211 | |
| 212 | /// Builder function that sets the [UdevEntry] name. |
| 213 | pub fn with_name<N: Into<String>>(mut self, name: N) -> Self { |
| 214 | self.set_name(name); |
| 215 | self |
| 216 | } |
| 217 | |
| 218 | /// Gets the [UdevEntry] value. |
| 219 | pub fn value(&self) -> &str { |
| 220 | self.value.as_str() |
| 221 | } |
| 222 | |
| 223 | /// Sets the [UdevEntry] value. |
| 224 | pub fn set_value<N: Into<String>>(&mut self, value: N) { |
| 225 | self.value = value.into(); |
| 226 | } |
| 227 | |
| 228 | /// Builder function that sets the [UdevEntry] value. |
| 229 | pub fn with_value<N: Into<String>>(mut self, value: N) -> Self { |
| 230 | self.set_value(value); |
| 231 | self |
| 232 | } |
| 233 | |
| 234 | /// Gets the [UdevEntry] number. |
| 235 | pub const fn num(&self) -> i32 { |
| 236 | self.num |
| 237 | } |
| 238 | |
| 239 | /// Sets the [UdevEntry] number. |
| 240 | pub fn set_num(&mut self, num: i32) { |
| 241 | self.num = num; |
| 242 | } |
| 243 | |
| 244 | /// Builder function that sets the [UdevEntry] number. |
| 245 | pub fn with_num(mut self, num: i32) -> Self { |
| 246 | self.set_num(num); |
| 247 | self |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | #[cfg(test)] |
| 252 | mod tests { |
| 253 | use super::*; |
| 254 | |
| 255 | #[test] |
| 256 | fn test_udev_entry() { |
| 257 | let mut null_entry = UdevEntry::new(); |
| 258 | |
| 259 | let exp_name = "test_name"; |
| 260 | let exp_value = "test_value"; |
| 261 | let exp_num = 42; |
| 262 | |
| 263 | let exp_entry = UdevEntry::new() |
| 264 | .with_name(exp_name) |
| 265 | .with_value(exp_value) |
| 266 | .with_num(exp_num); |
| 267 | |
| 268 | assert_eq!(null_entry.name(), ""); |
| 269 | assert_eq!(null_entry.value(), ""); |
| 270 | assert_eq!(null_entry.num(), 0); |
| 271 | |
| 272 | assert_eq!(exp_entry.name(), exp_name); |
| 273 | assert_eq!(exp_entry.value(), exp_value); |
| 274 | assert_eq!(exp_entry.num(), exp_num); |
| 275 | |
| 276 | null_entry.set_name(exp_name); |
| 277 | assert_eq!(null_entry.name(), exp_name); |
| 278 | |
| 279 | null_entry.set_value(exp_value); |
| 280 | assert_eq!(null_entry.value(), exp_value); |
| 281 | |
| 282 | null_entry.set_num(exp_num); |
| 283 | assert_eq!(null_entry.num(), exp_num); |
| 284 | |
| 285 | assert_eq!(null_entry, exp_entry); |
| 286 | } |
| 287 | } |