Skip to content

Commit 9e01496

Browse files
committed
fixup! feat(#215): impl validate_bluetooth_address
1 parent d037af9 commit 9e01496

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

nmrs/src/util/validation.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -537,27 +537,31 @@ fn validate_ip_address(ip: &str) -> Result<(), ConnectionError> {
537537
/// # Errors
538538
/// Returns `ConnectionError::InvalidAddress` if the Bluetooth address is invalid.
539539
pub fn validate_bluetooth_address(bdaddr: &str) -> Result<(), ConnectionError> {
540-
if bdaddr.len() != 17 {
540+
let parts: Vec<&str> = bdaddr.split(':').collect();
541+
542+
if parts.len() != 6 {
541543
return Err(ConnectionError::InvalidAddress(format!(
542-
"Invalid Bluetooth Address '{}' (expected length 17)",
543-
bdaddr
544+
"Invalid Bluetooth Address '{}' (must have 6 segments)",
545+
bdaddr,
544546
)));
545547
}
546-
for (index, c) in bdaddr.chars().enumerate() {
547-
if (index + 1) % 3 == 0 {
548-
if c != ':' {
549-
return Err(ConnectionError::InvalidAddress(format!(
550-
"Invalid Bluetooth Address '{}' (expected ':', found {})",
551-
bdaddr, c
552-
)));
553-
}
554-
} else if !c.is_ascii_hexdigit() {
548+
549+
for part in parts {
550+
if part.len() != 2 {
555551
return Err(ConnectionError::InvalidAddress(format!(
556-
"Invalid Bluetooth Address '{}' ('{}' is not a hex digit)",
557-
bdaddr, c
552+
"Invalid segment '{}' in Bluetooth Address '{}' (must be 2 characters)",
553+
part, bdaddr
554+
)));
555+
}
556+
557+
if !part.chars().all(|c| c.is_ascii_hexdigit()) {
558+
return Err(ConnectionError::InvalidAddress(format!(
559+
"Invalid segment '{}' in Bluetooth Address '{}' (must be hex digits)",
560+
part, bdaddr
558561
)));
559562
}
560563
}
564+
561565
Ok(())
562566
}
563567

0 commit comments

Comments
 (0)