Skip to content

Commit f015317

Browse files
committed
Refactor apply_baud_rate_flag to remove duplicate cfg blocks
1 parent 61d85f2 commit f015317

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

src/uu/stty/src/flags.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use nix::sys::termios::{
2727
SpecialCharacterIndices as S,
2828
};
2929

30+
#[derive(Debug)]
3031
pub enum BaudType {
3132
Input,
3233
Output,

src/uu/stty/src/stty.rs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -979,42 +979,13 @@ fn apply_setting(termios: &mut Termios, setting: &AllFlags) -> nix::Result<()> {
979979
}
980980

981981
fn apply_baud_rate_flag(termios: &mut Termios, input: &AllFlags) -> nix::Result<()> {
982-
// BSDs use a u32 for the baud rate, so any decimal number applies.
983-
#[cfg(any(
984-
target_os = "freebsd",
985-
target_os = "dragonfly",
986-
target_os = "ios",
987-
target_os = "macos",
988-
target_os = "netbsd",
989-
target_os = "openbsd"
990-
))]
991-
if let AllFlags::Baud(n, baud_type) = input {
992-
match baud_type {
993-
flags::BaudType::Input => cfsetispeed(termios, *n)?,
994-
flags::BaudType::Output => cfsetospeed(termios, *n)?,
995-
flags::BaudType::Both => {
996-
cfsetispeed(termios, *n)?;
997-
cfsetospeed(termios, *n)?;
998-
}
999-
}
1000-
}
1001-
1002-
// Other platforms use an enum.
1003-
#[cfg(not(any(
1004-
target_os = "freebsd",
1005-
target_os = "dragonfly",
1006-
target_os = "ios",
1007-
target_os = "macos",
1008-
target_os = "netbsd",
1009-
target_os = "openbsd"
1010-
)))]
1011-
if let AllFlags::Baud(br, baud_type) = input {
982+
if let AllFlags::Baud(rate, baud_type) = input {
1012983
match baud_type {
1013-
flags::BaudType::Input => cfsetispeed(termios, *br)?,
1014-
flags::BaudType::Output => cfsetospeed(termios, *br)?,
984+
flags::BaudType::Input => cfsetispeed(termios, *rate)?,
985+
flags::BaudType::Output => cfsetospeed(termios, *rate)?,
1015986
flags::BaudType::Both => {
1016-
cfsetispeed(termios, *br)?;
1017-
cfsetospeed(termios, *br)?;
987+
cfsetispeed(termios, *rate)?;
988+
cfsetospeed(termios, *rate)?;
1018989
}
1019990
}
1020991
}
@@ -1029,7 +1000,7 @@ fn apply_char_mapping(termios: &mut Termios, mapping: &(S, u8)) {
10291000
///
10301001
/// The state array contains:
10311002
/// - `state[0]`: input flags
1032-
/// - `state[1]`: output flags
1003+
/// - `state[1]`: output flags
10331004
/// - `state[2]`: control flags
10341005
/// - `state[3]`: local flags
10351006
/// - `state[4..]`: control characters (optional)
@@ -1477,10 +1448,10 @@ mod tests {
14771448
target_os = "openbsd"
14781449
)))]
14791450
{
1480-
assert!(string_to_baud("9600").is_some());
1481-
assert!(string_to_baud("115200").is_some());
1482-
assert!(string_to_baud("38400").is_some());
1483-
assert!(string_to_baud("19200").is_some());
1451+
assert!(string_to_baud("9600", flags::BaudType::Both).is_some());
1452+
assert!(string_to_baud("115200", flags::BaudType::Both).is_some());
1453+
assert!(string_to_baud("38400", flags::BaudType::Both).is_some());
1454+
assert!(string_to_baud("19200", flags::BaudType::Both).is_some());
14841455
}
14851456

14861457
#[cfg(any(
@@ -1492,10 +1463,10 @@ mod tests {
14921463
target_os = "openbsd"
14931464
))]
14941465
{
1495-
assert!(string_to_baud("9600").is_some());
1496-
assert!(string_to_baud("115200").is_some());
1497-
assert!(string_to_baud("1000000").is_some());
1498-
assert!(string_to_baud("0").is_some());
1466+
assert!(string_to_baud("9600", flags::BaudType::Both).is_some());
1467+
assert!(string_to_baud("115200", flags::BaudType::Both).is_some());
1468+
assert!(string_to_baud("1000000", flags::BaudType::Both).is_some());
1469+
assert!(string_to_baud("0", flags::BaudType::Both).is_some());
14991470
}
15001471
}
15011472

@@ -1510,10 +1481,10 @@ mod tests {
15101481
target_os = "openbsd"
15111482
)))]
15121483
{
1513-
assert_eq!(string_to_baud("995"), None);
1514-
assert_eq!(string_to_baud("invalid"), None);
1515-
assert_eq!(string_to_baud(""), None);
1516-
assert_eq!(string_to_baud("abc"), None);
1484+
assert_eq!(string_to_baud("995", flags::BaudType::Both), None);
1485+
assert_eq!(string_to_baud("invalid", flags::BaudType::Both), None);
1486+
assert_eq!(string_to_baud("", flags::BaudType::Both), None);
1487+
assert_eq!(string_to_baud("abc", flags::BaudType::Both), None);
15171488
}
15181489
}
15191490

0 commit comments

Comments
 (0)