Skip to content

Commit 7419d10

Browse files
committed
Return an error when the parity is not supported
1 parent 9091401 commit 7419d10

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/posix/termios.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ pub(crate) fn set_termios(fd: RawFd, termios: &Termios) -> Result<()> {
145145
crate::posix::ioctl::tcsets2(fd, termios)
146146
}
147147

148-
pub(crate) fn set_parity(termios: &mut Termios, parity: Parity) {
148+
// The Result return will seem pointless on platforms that support all parity variants, but we need
149+
// it for the others.
150+
#[allow(clippy::unnecessary_wraps)]
151+
pub(crate) fn set_parity(termios: &mut Termios, parity: Parity) -> Result<()> {
149152
match parity {
150153
Parity::None => {
151154
termios.c_cflag &= !(libc::PARENB | libc::PARODD);
@@ -163,27 +166,38 @@ pub(crate) fn set_parity(termios: &mut Termios, parity: Parity) {
163166
termios.c_iflag |= libc::INPCK;
164167
termios.c_iflag &= !libc::IGNPAR;
165168
}
169+
#[cfg(any(target_os = "linux", target_os = "android"))]
166170
Parity::Mark => {
167171
termios.c_cflag |= libc::PARODD;
168172
termios.c_cflag |= libc::PARENB;
169173
termios.c_iflag |= libc::INPCK;
170174
termios.c_iflag &= !libc::IGNPAR;
171-
#[cfg(any(target_os = "linux", target_os = "android"))]
172-
{
173-
termios.c_iflag |= libc::CMSPAR;
174-
}
175+
termios.c_iflag |= libc::CMSPAR;
175176
}
177+
#[cfg(any(target_os = "linux", target_os = "android"))]
176178
Parity::Space => {
177179
termios.c_cflag &= !libc::PARODD;
178180
termios.c_cflag |= libc::PARENB;
179181
termios.c_iflag |= libc::INPCK;
180182
termios.c_iflag &= !libc::IGNPAR;
181-
#[cfg(any(target_os = "linux", target_os = "android"))]
182-
{
183-
termios.c_iflag |= libc::CMSPAR;
184-
}
183+
termios.c_iflag |= libc::CMSPAR;
184+
}
185+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
186+
Parity::Mark => {
187+
return Err(crate::Error::new(
188+
crate::ErrorKind::InvalidInput,
189+
"Mark parity not supported on this platform",
190+
));
191+
}
192+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
193+
Parity::Space => {
194+
return Err(crate::Error::new(
195+
crate::ErrorKind::InvalidInput,
196+
"Space parity not supported on this platform",
197+
));
185198
}
186199
};
200+
Ok(())
187201
}
188202

189203
pub(crate) fn set_flow_control(termios: &mut Termios, flow_control: FlowControl) {

src/posix/tty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl TTYPort {
171171

172172
// Configure the low-level port settings
173173
let mut termios = termios::get_termios(fd.0)?;
174-
termios::set_parity(&mut termios, builder.parity);
174+
termios::set_parity(&mut termios, builder.parity)?;
175175
termios::set_flow_control(&mut termios, builder.flow_control);
176176
termios::set_data_bits(&mut termios, builder.data_bits);
177177
termios::set_stop_bits(&mut termios, builder.stop_bits);
@@ -653,7 +653,7 @@ impl SerialPort for TTYPort {
653653

654654
fn set_parity(&mut self, parity: Parity) -> Result<()> {
655655
let mut termios = termios::get_termios(self.fd)?;
656-
termios::set_parity(&mut termios, parity);
656+
termios::set_parity(&mut termios, parity)?;
657657
#[cfg(any(target_os = "ios", target_os = "macos"))]
658658
return termios::set_termios(self.fd, &termios, self.baud_rate);
659659
#[cfg(not(any(target_os = "ios", target_os = "macos")))]

0 commit comments

Comments
 (0)