Skip to content

Commit

Permalink
Un-nest port_type helper function (on Linux)
Browse files Browse the repository at this point in the history
There is no special requirement for having them there and they are
cluttering up the match statement.
  • Loading branch information
sirhcel committed Jul 25, 2024
1 parent 0cda296 commit e637abd
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions src/posix/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,53 +177,51 @@ fn port_type(d: &libudev::Device) -> Result<SerialPortType> {
Ok(SerialPortType::PciPort)
}
}
None => {
fn find_usb_interface_from_parents(
parent: Option<libudev::Device>,
) -> Option<libudev::Device> {
let mut p = parent?;

// limit the query depth
for _ in 1..4 {
match p.devtype() {
None => match p.parent() {
None => break,
Some(x) => p = x,
},
Some(s) => {
if s.to_str()? == "usb_interface" {
break;
} else {
match p.parent() {
None => break,
Some(x) => p = x,
}
}
}
None => find_usb_interface_from_parents(d.parent())
.and_then(get_modalias_from_device)
.as_deref()
.and_then(parse_modalias)
.map_or(Ok(SerialPortType::Unknown), |port_info| {
Ok(SerialPortType::UsbPort(port_info))
}),
_ => Ok(SerialPortType::Unknown),
}
}

#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
fn find_usb_interface_from_parents(parent: Option<libudev::Device>) -> Option<libudev::Device> {
let mut p = parent?;

// limit the query depth
for _ in 1..4 {
match p.devtype() {
None => match p.parent() {
None => break,
Some(x) => p = x,
},
Some(s) => {
if s.to_str()? == "usb_interface" {
break;
} else {
match p.parent() {
None => break,
Some(x) => p = x,
}
}

Some(p)
}

fn get_modalias_from_device(d: libudev::Device) -> Option<String> {
Some(
d.property_value("MODALIAS")
.and_then(OsStr::to_str)?
.to_owned(),
)
}

find_usb_interface_from_parents(d.parent())
.and_then(get_modalias_from_device)
.as_deref()
.and_then(parse_modalias)
.map_or(Ok(SerialPortType::Unknown), |port_info| {
Ok(SerialPortType::UsbPort(port_info))
})
}
_ => Ok(SerialPortType::Unknown),
}

Some(p)
}

#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
fn get_modalias_from_device(d: libudev::Device) -> Option<String> {
Some(
d.property_value("MODALIAS")
.and_then(OsStr::to_str)?
.to_owned(),
)
}

// MODALIAS = usb:v303Ap1001d0101dcEFdsc02dp01ic02isc02ip00in00
Expand Down

0 comments on commit e637abd

Please sign in to comment.