Skip to content

Commit 763b2f7

Browse files
committed
Un-nest port_type helper functions (on Linux)
There is no special requirement for having them there and they are cluttering up the match statement.
1 parent 0cda296 commit 763b2f7

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

src/posix/enumerate.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -177,53 +177,51 @@ fn port_type(d: &libudev::Device) -> Result<SerialPortType> {
177177
Ok(SerialPortType::PciPort)
178178
}
179179
}
180-
None => {
181-
fn find_usb_interface_from_parents(
182-
parent: Option<libudev::Device>,
183-
) -> Option<libudev::Device> {
184-
let mut p = parent?;
185-
186-
// limit the query depth
187-
for _ in 1..4 {
188-
match p.devtype() {
189-
None => match p.parent() {
190-
None => break,
191-
Some(x) => p = x,
192-
},
193-
Some(s) => {
194-
if s.to_str()? == "usb_interface" {
195-
break;
196-
} else {
197-
match p.parent() {
198-
None => break,
199-
Some(x) => p = x,
200-
}
201-
}
202-
}
180+
None => find_usb_interface_from_parents(d.parent())
181+
.and_then(get_modalias_from_device)
182+
.as_deref()
183+
.and_then(parse_modalias)
184+
.map_or(Ok(SerialPortType::Unknown), |port_info| {
185+
Ok(SerialPortType::UsbPort(port_info))
186+
}),
187+
_ => Ok(SerialPortType::Unknown),
188+
}
189+
}
190+
191+
#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
192+
fn find_usb_interface_from_parents(parent: Option<libudev::Device>) -> Option<libudev::Device> {
193+
let mut p = parent?;
194+
195+
// limit the query depth
196+
for _ in 1..4 {
197+
match p.devtype() {
198+
None => match p.parent() {
199+
None => break,
200+
Some(x) => p = x,
201+
},
202+
Some(s) => {
203+
if s.to_str()? == "usb_interface" {
204+
break;
205+
} else {
206+
match p.parent() {
207+
None => break,
208+
Some(x) => p = x,
203209
}
204210
}
205-
206-
Some(p)
207-
}
208-
209-
fn get_modalias_from_device(d: libudev::Device) -> Option<String> {
210-
Some(
211-
d.property_value("MODALIAS")
212-
.and_then(OsStr::to_str)?
213-
.to_owned(),
214-
)
215211
}
216-
217-
find_usb_interface_from_parents(d.parent())
218-
.and_then(get_modalias_from_device)
219-
.as_deref()
220-
.and_then(parse_modalias)
221-
.map_or(Ok(SerialPortType::Unknown), |port_info| {
222-
Ok(SerialPortType::UsbPort(port_info))
223-
})
224212
}
225-
_ => Ok(SerialPortType::Unknown),
226213
}
214+
215+
Some(p)
216+
}
217+
218+
#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
219+
fn get_modalias_from_device(d: libudev::Device) -> Option<String> {
220+
Some(
221+
d.property_value("MODALIAS")
222+
.and_then(OsStr::to_str)?
223+
.to_owned(),
224+
)
227225
}
228226

229227
// MODALIAS = usb:v303Ap1001d0101dcEFdsc02dp01ic02isc02ip00in00

0 commit comments

Comments
 (0)