1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::Settings;

/// Trait for objects that can configure a serial port.
///
/// This trait is also implemented for `u32`.
/// That implementation also configures a char size of 8 bits,
/// one stop bit, no parity checks and disables flow control.
///
/// If you need more control, you can use a `Fn(Settings) -> std::io::Result<Settings>`.
pub trait IntoSettings {
	/// Apply the configuration to an existing [`Settings`] struct.
	fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()>;
}

impl<F> IntoSettings for F
where
	F: FnOnce(Settings) -> std::io::Result<Settings>,
{
	fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()> {
		*settings = (self)(settings.clone())?;
		Ok(())
	}
}

impl IntoSettings for u32 {
	fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()> {
		settings.set_baud_rate(self)?;
		settings.set_char_size(crate::CharSize::Bits8);
		settings.set_stop_bits(crate::StopBits::One);
		settings.set_parity(crate::Parity::None);
		settings.set_flow_control(crate::FlowControl::None);
		Ok(())
	}
}

/// A serial port "configuration" that simply keeps all existing settings.
///
/// You can pass this to [`SerialPort::open()`] to prevent it from changing any port settings.
///
/// However, be aware that on many platforms, configuration of serial ports resets to a default when they are closed.
/// Usually, you should explicitly configure all important settings.
pub struct KeepSettings;

impl IntoSettings for KeepSettings {
	fn apply_to_settings(self, _settings: &mut Settings) -> std::io::Result<()> {
		Ok(())
	}
}