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