| 1 | use superrust-analyzer cargo udevrs 0.4.0 hwdb/::trie_stringrust-analyzer cargo udevrs 0.4.0 hwdb/trie/trie_string().; |
| 2 | use craterust-analyzer cargo udevrs 0.4.0 crate/::{Errorrust-analyzer cargo udevrs 0.4.0 error/Error#, Resultrust-analyzer cargo udevrs 0.4.0 error/Result#, TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#, UdevHwdbrust-analyzer cargo udevrs 0.4.0 hwdb/UdevHwdb#, UdevListrust-analyzer cargo udevrs 0.4.0 list/UdevList#}; |
| 3 | use heaplessrust-analyzer cargo heapless 0.8.0 crate/::Vecrust-analyzer cargo heapless 0.8.0 vec/Vec#; |
| 4 | |
| 5 | /// Maximum length for a file line. |
| 6 | pub const LINE_MAXrust-analyzer cargo udevrs 0.4.0 hwdb/line/LINE_MAX.: usize = 4096; |
| 7 | |
| 8 | /// Line buffer for parsing HWDB on-disk file format. |
| 9 | #[repr(C)] |
| 10 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 11 | pub struct LineBufrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf# { |
| 12 | bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes.: Vecrust-analyzer cargo heapless 0.8.0 vec/Vec#<u8, LINE_MAXrust-analyzer cargo udevrs 0.4.0 hwdb/line/LINE_MAX.>, |
| 13 | } |
| 14 | |
| 15 | impl LineBufrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf# { |
| 16 | /// Creates a new [LineBuf]. |
| 17 | pub const fn newrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]new().() -> Selfrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf] { |
| 18 | Selfrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf] { bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes.: Vecrust-analyzer cargo heapless 0.8.0 vec/Vec#::newrust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`]new().() } |
| 19 | } |
| 20 | |
| 21 | /// Gets the line buffer as a string reference. |
| 22 | pub fn getrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]get().(&self) -> &str { |
| 23 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..as_refrust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`][`AsRef<[T]>`]as_ref().()).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().("") |
| 24 | } |
| 25 | |
| 26 | /// Adds `val` string to the [LineBuf]. |
| 27 | /// |
| 28 | /// `val` must not cause the total length of the [LineBuf] to exceed [LINE_MAX]. |
| 29 | pub fn addrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]add().(&mut self, val: &str) -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<()> { |
| 30 | self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..extend_from_slicerust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`]extend_from_slice().(val.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| { |
| 31 | Errorrust-analyzer cargo udevrs 0.4.0 error/Error#::UdevHwdbrust-analyzer cargo udevrs 0.4.0 error/Error#UdevHwdb#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 32 | "byte string exceeds the max line length: {LINE_MAX}" |
| 33 | )) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | /// Adds `val` character to the [LineBuf]. |
| 38 | /// |
| 39 | /// `val` must not cause the total length of the [LineBuf] to exceed [LINE_MAX]. |
| 40 | pub fn add_charrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]add_char().<C: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<u8>>(&mut self, val: C) -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<()> { |
| 41 | self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..pushrust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`]push().(val.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| { |
| 42 | Errorrust-analyzer cargo udevrs 0.4.0 error/Error#::UdevHwdbrust-analyzer cargo udevrs 0.4.0 error/Error#UdevHwdb#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("character exceeds the max line length: {LINE_MAX}")) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | /// Removes `count` characters from the [LineBuf]. |
| 47 | /// |
| 48 | /// **NOTE**: clears the buffer if count is larger than the [LineBuf] length. |
| 49 | pub fn removerust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]remove().(&mut self, count: usize) { |
| 50 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 51 | self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..clearrust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`]clear().(); |
| 52 | } else { |
| 53 | self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..truncaterust-analyzer cargo heapless 0.8.0 vec/impl#[`Vec<T, N>`]truncate().(self.bytesrust-analyzer cargo udevrs 0.4.0 hwdb/line/LineBuf#bytes..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().count); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Removes a single character from the [LineBuf]. |
| 58 | pub fn remove_charrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]remove_char().(&mut self) { |
| 59 | self.removerust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]remove().(1); |
| 60 | } |
| 61 | |
| 62 | /// Searches the [LineBuf] for a matching property. |
| 63 | /// |
| 64 | /// If a property is found, it is added to [`list`](UdevList). |
| 65 | pub fn trie_fnmatchrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]trie_fnmatch().( |
| 66 | &mut self, |
| 67 | list: &mut UdevListrust-analyzer cargo udevrs 0.4.0 list/UdevList#, |
| 68 | hwdb_buf: &[u8], |
| 69 | entry: &TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#, |
| 70 | p: usize, |
| 71 | search: &str, |
| 72 | ) -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<()> { |
| 73 | let prefix_off = entry.noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]node().().prefix_offrust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/impl#[TrieNode]prefix_off().() as usize; |
| 74 | let prefix = trie_stringrust-analyzer cargo udevrs 0.4.0 hwdb/trie/trie_string().(hwdb_buf, prefix_off)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 75 | let prefix_len = prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 76 | |
| 77 | logrust-analyzer cargo log 0.4.25 crate/::tracerust-analyzer cargo log 0.4.25 macros/trace!!( |
| 78 | "Entering fnmatch, prefix: {prefix}, glob: {}, search: {search}", |
| 79 | self.getrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]get().() |
| 80 | ); |
| 81 | |
| 82 | let (start, end) = ( |
| 83 | p, |
| 84 | prefix |
| 85 | .as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().() |
| 86 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 87 | .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(p) |
| 88 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#position().(|c| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'\0') |
| 89 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(prefix_len), |
| 90 | ); |
| 91 | |
| 92 | // the logic of add only if within bounds but always remove is odd but the linebuf_add return is not checked in https://github.com/cr8t/udev/issues/25#L187 |
| 93 | // so behavior should match |
| 94 | if (start..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#end).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&prefix_len) { |
| 95 | self.addrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]add().(&prefix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 96 | } |
| 97 | |
| 98 | for child in entry.childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]children().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 99 | let child_off = child.child_offrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry]child_off().() as usize; |
| 100 | if child_off rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().hwdb_buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 101 | self.add_charrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]add_char().(child.crust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry]c().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 102 | self.trie_fnmatchrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]trie_fnmatch().( |
| 103 | list, |
| 104 | hwdb_buf, |
| 105 | &TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#::try_fromrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`]try_from().(&hwdb_buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child_off..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 106 | 0, |
| 107 | search, |
| 108 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 109 | self.remove_charrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]remove_char().(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if globrust-analyzer cargo glob 0.3.2 crate/::Patternrust-analyzer cargo glob 0.3.2 Pattern#::newrust-analyzer cargo glob 0.3.2 impl#[Pattern]new().(self.getrust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]get().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..matchesrust-analyzer cargo glob 0.3.2 impl#[Pattern]matches().(search) { |
| 114 | logrust-analyzer cargo log 0.4.25 crate/::tracerust-analyzer cargo log 0.4.25 macros/trace!!("Found matching entry, entry: {entry:?}, search: {search}"); |
| 115 | for value in entry.valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]values().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 116 | UdevHwdbrust-analyzer cargo udevrs 0.4.0 hwdb/UdevHwdb#::_add_propertyrust-analyzer cargo udevrs 0.4.0 hwdb/impl#[UdevHwdb]_add_property().( |
| 117 | list, |
| 118 | trie_stringrust-analyzer cargo udevrs 0.4.0 hwdb/trie/trie_string().(hwdb_buf, value.key_offrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/impl#[TrieValueEntry]key_off().() as usize)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 119 | trie_stringrust-analyzer cargo udevrs 0.4.0 hwdb/trie/trie_string().(hwdb_buf, value.value_offrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/impl#[TrieValueEntry]value_off().() as usize)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 120 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | self.removerust-analyzer cargo udevrs 0.4.0 hwdb/line/impl#[LineBuf]remove().(end.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(start)); |
| 125 | |
| 126 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 127 | } |
| 128 | } |