ids_rs 0.1.0

A no_std PCI device identification library for operating systems
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Advanced query interface for the PCI database.

use crate::database::PciDatabase;
use crate::vendors::Vendor;
use crate::devices::Device;
use crate::classes::{DeviceClass, SubClass};
use crate::types::*;
use alloc::{vec::Vec, string::String, string::ToString};

/// Builder for constructing complex PCI device queries.
///
/// This provides a fluent interface for building sophisticated queries
/// against the PCI database, allowing filtering and searching across
/// multiple criteria.
///
/// # Examples
///
/// ```rust
/// use ids_rs::{PciDatabase, QueryBuilder};
///
/// let db = PciDatabase::get();
/// let intel_network_devices = QueryBuilder::new(db)
///     .vendor_name_contains("Intel")
///     .class_name_contains("Network")
///     .execute();
/// ```
#[derive(Debug)]
pub struct QueryBuilder<'db> {
    database: &'db PciDatabase,
    vendor_id_filter: Option<VendorId>,
    vendor_name_filter: Option<String>,
    device_id_filter: Option<DeviceId>,
    device_name_filter: Option<String>,
    class_id_filter: Option<DeviceClassId>,
    class_name_filter: Option<String>,
    subclass_id_filter: Option<SubClassId>,
    subclass_name_filter: Option<String>,
}

impl<'db> QueryBuilder<'db> {
    /// Create a new query builder for the given database.
    pub fn new(database: &'db PciDatabase) -> Self {
        Self {
            database,
            vendor_id_filter: None,
            vendor_name_filter: None,
            device_id_filter: None,
            device_name_filter: None,
            class_id_filter: None,
            class_name_filter: None,
            subclass_id_filter: None,
            subclass_name_filter: None,
        }
    }

    /// Filter by vendor ID.
    pub fn vendor_id(mut self, vendor_id: VendorId) -> Self {
        self.vendor_id_filter = Some(vendor_id);
        self
    }

    /// Filter by vendor name (case-insensitive substring match).
    pub fn vendor_name_contains(mut self, name: &str) -> Self {
        self.vendor_name_filter = Some(name.to_lowercase());
        self
    }

    /// Filter by device ID.
    pub fn device_id(mut self, device_id: DeviceId) -> Self {
        self.device_id_filter = Some(device_id);
        self
    }

    /// Filter by device name (case-insensitive substring match).
    pub fn device_name_contains(mut self, name: &str) -> Self {
        self.device_name_filter = Some(name.to_lowercase());
        self
    }

    /// Filter by device class ID.
    pub fn class_id(mut self, class_id: DeviceClassId) -> Self {
        self.class_id_filter = Some(class_id);
        self
    }

    /// Filter by device class name (case-insensitive substring match).
    pub fn class_name_contains(mut self, name: &str) -> Self {
        self.class_name_filter = Some(name.to_lowercase());
        self
    }

    /// Filter by subclass ID.
    pub fn subclass_id(mut self, subclass_id: SubClassId) -> Self {
        self.subclass_id_filter = Some(subclass_id);
        self
    }

    /// Filter by subclass name (case-insensitive substring match).
    pub fn subclass_name_contains(mut self, name: &str) -> Self {
        self.subclass_name_filter = Some(name.to_lowercase());
        self
    }

    /// Execute the query and return matching device results.
    pub fn execute(self) -> Vec<DeviceMatch<'db>> {
        let mut results = Vec::new();

        for vendor in self.database.vendors() {
            // Check vendor filters
            if let Some(ref vendor_id) = self.vendor_id_filter {
                if vendor.id() != *vendor_id {
                    continue;
                }
            }

            if let Some(ref vendor_name) = self.vendor_name_filter {
                if !vendor.name().to_lowercase().contains(vendor_name) {
                    continue;
                }
            }

            for device in vendor.devices() {
                // Check device filters
                if let Some(ref device_id) = self.device_id_filter {
                    if device.id() != *device_id {
                        continue;
                    }
                }

                if let Some(ref device_name) = self.device_name_filter {
                    if !device.name().to_lowercase().contains(device_name) {
                        continue;
                    }
                }

                // If we have class filters, we need to check if any class matches
                let class_match = self.find_matching_class();

                if self.has_class_filters() && class_match.is_none() {
                    continue;
                }

                results.push(DeviceMatch {
                    vendor,
                    device,
                    class_info: class_match,
                });
            }
        }

        results
    }

    /// Execute the query and return matching vendor results.
    pub fn execute_vendors(self) -> Vec<&'db Vendor> {
        let mut results = Vec::new();

        for vendor in self.database.vendors() {
            // Check vendor filters
            if let Some(ref vendor_id) = self.vendor_id_filter {
                if vendor.id() != *vendor_id {
                    continue;
                }
            }

            if let Some(ref vendor_name) = self.vendor_name_filter {
                if !vendor.name().to_lowercase().contains(vendor_name) {
                    continue;
                }
            }

            results.push(vendor);
        }

        results
    }

    /// Execute the query and return matching class results.
    pub fn execute_classes(self) -> Vec<ClassMatch<'db>> {
        let mut results = Vec::new();

        for class in self.database.classes() {
            // Check class filters
            if let Some(ref class_id) = self.class_id_filter {
                if class.id() != *class_id {
                    continue;
                }
            }

            if let Some(ref class_name) = self.class_name_filter {
                if !class.name().to_lowercase().contains(class_name) {
                    continue;
                }
            }

            // Check subclass filters
            let matching_subclasses: Vec<&SubClass> = class
                .subclasses()
                .iter()
                .filter(|subclass| {
                    if let Some(ref subclass_id) = self.subclass_id_filter {
                        if subclass.id() != *subclass_id {
                            return false;
                        }
                    }

                    if let Some(ref subclass_name) = self.subclass_name_filter {
                        if !subclass.name().to_lowercase().contains(subclass_name) {
                            return false;
                        }
                    }

                    true
                })
                .collect();

            if self.has_subclass_filters() && matching_subclasses.is_empty() {
                continue;
            }

            results.push(ClassMatch {
                class,
                matching_subclasses,
            });
        }

        results
    }

    fn has_class_filters(&self) -> bool {
        self.class_id_filter.is_some() || self.class_name_filter.is_some() || self.has_subclass_filters()
    }

    fn has_subclass_filters(&self) -> bool {
        self.subclass_id_filter.is_some() || self.subclass_name_filter.is_some()
    }

    fn find_matching_class(&self) -> Option<&'db DeviceClass> {
        for class in self.database.classes() {
            if let Some(ref class_id) = self.class_id_filter {
                if class.id() != *class_id {
                    continue;
                }
            }

            if let Some(ref class_name) = self.class_name_filter {
                if !class.name().to_lowercase().contains(class_name) {
                    continue;
                }
            }

            // Check if any subclass matches
            if self.has_subclass_filters() {
                let has_matching_subclass = class.subclasses().iter().any(|subclass| {
                    if let Some(ref subclass_id) = self.subclass_id_filter {
                        if subclass.id() != *subclass_id {
                            return false;
                        }
                    }

                    if let Some(ref subclass_name) = self.subclass_name_filter {
                        if !subclass.name().to_lowercase().contains(subclass_name) {
                            return false;
                        }
                    }

                    true
                });

                if !has_matching_subclass {
                    continue;
                }
            }

            return Some(class);
        }

        None
    }
}

/// A device match result from a query.
#[derive(Debug)]
pub struct DeviceMatch<'db> {
    /// The matching vendor
    pub vendor: &'db Vendor,
    /// The matching device
    pub device: &'db Device,
    /// Optional class information if class filters were used
    pub class_info: Option<&'db DeviceClass>,
}

impl<'db> DeviceMatch<'db> {
    /// Get the vendor ID.
    pub fn vendor_id(&self) -> VendorId {
        self.vendor.id()
    }

    /// Get the vendor name.
    pub fn vendor_name(&self) -> &'static str {
        self.vendor.name()
    }

    /// Get the device ID.
    pub fn device_id(&self) -> DeviceId {
        self.device.id()
    }

    /// Get the device name.
    pub fn device_name(&self) -> &'static str {
        self.device.name()
    }

    /// Get a formatted description of this device match.
    pub fn description(&self) -> String {
        if let Some(class) = self.class_info {
            alloc::format!(
                "{} {} ({})",
                self.vendor_name(),
                self.device_name(),
                class.name()
            )
        } else {
            alloc::format!("{} {}", self.vendor_name(), self.device_name())
        }
    }
}

/// A class match result from a query.
#[derive(Debug)]
pub struct ClassMatch<'db> {
    /// The matching class
    pub class: &'db DeviceClass,
    /// Subclasses that matched the query (empty if no subclass filters were used)
    pub matching_subclasses: Vec<&'db SubClass>,
}

impl<'db> ClassMatch<'db> {
    /// Get the class ID.
    pub fn class_id(&self) -> DeviceClassId {
        self.class.id()
    }

    /// Get the class name.
    pub fn class_name(&self) -> &'static str {
        self.class.name()
    }

    /// Get a formatted description of this class match.
    pub fn description(&self) -> String {
        if self.matching_subclasses.is_empty() {
            self.class_name().to_string()
        } else {
            let subclass_names: Vec<&str> = self
                .matching_subclasses
                .iter()
                .map(|sc| sc.name())
                .collect();
            alloc::format!("{} ({})", self.class_name(), subclass_names.join(", "))
        }
    }
}

/// Convenience functions for common queries.
impl PciDatabase {
    /// Find all devices from a specific vendor.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ids_rs::{PciDatabase, VendorId};
    ///
    /// let db = PciDatabase::get();
    /// let intel_devices = db.devices_by_vendor(VendorId::new(0x8086));
    /// ```
    pub fn devices_by_vendor(&self, vendor_id: VendorId) -> Option<&[Device]> {
        self.find_vendor(vendor_id).map(|vendor| vendor.devices())
    }

    /// Find all devices of a specific class.
    ///
    /// Note: This returns device IDs that could belong to the class, but
    /// actual class assignment depends on the device's configuration.
    pub fn devices_by_class(&self, class_id: DeviceClassId) -> Vec<DeviceMatch<'_>> {
        QueryBuilder::new(self).class_id(class_id).execute()
    }

    /// Search for vendors by name (case-insensitive).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ids_rs::PciDatabase;
    ///
    /// let db = PciDatabase::get();
    /// let intel_vendors = db.search_vendors("intel");
    /// ```
    pub fn search_vendors(&self, name: &str) -> Vec<&Vendor> {
        QueryBuilder::new(self)
            .vendor_name_contains(name)
            .execute_vendors()
    }

    /// Search for devices by name (case-insensitive).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ids_rs::PciDatabase;
    ///
    /// let db = PciDatabase::get();
    /// let ethernet_devices = db.search_devices("ethernet");
    /// ```
    pub fn search_devices(&self, name: &str) -> Vec<DeviceMatch<'_>> {
        QueryBuilder::new(self)
            .device_name_contains(name)
            .execute()
    }

    /// Search for device classes by name (case-insensitive).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ids_rs::PciDatabase;
    ///
    /// let db = PciDatabase::get();
    /// let network_classes = db.search_classes("network");
    /// ```
    pub fn search_classes(&self, name: &str) -> Vec<ClassMatch<'_>> {
        QueryBuilder::new(self)
            .class_name_contains(name)
            .execute_classes()
    }

    /// Get a query builder for this database.
    ///
    /// This provides access to the full query interface.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ids_rs::PciDatabase;
    ///
    /// let db = PciDatabase::get();
    /// let results = db.query()
    ///     .vendor_name_contains("Intel")
    ///     .class_name_contains("Network")
    ///     .execute();
    /// ```
    pub fn query(&self) -> QueryBuilder<'_> {
        QueryBuilder::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vendors::Vendor;
    use crate::devices::Device;
    use crate::classes::DeviceClass;

    #[test]
    fn test_query_builder_creation() {
        let vendors: &[Vendor] = &[];
        let classes: &[DeviceClass] = &[];
        let db = PciDatabase::new(vendors, classes);

        let query = QueryBuilder::new(&db);
        let results = query.execute();
        assert!(results.is_empty());
    }

    #[test]
    fn test_empty_database_queries() {
        let vendors: &[Vendor] = &[];
        let classes: &[DeviceClass] = &[];
        let db = PciDatabase::new(vendors, classes);

        assert!(db.search_vendors("test").is_empty());
        assert!(db.search_devices("test").is_empty());
        assert!(db.search_classes("test").is_empty());
    }
}