Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds fmt::Debug for dyn SerialPort and related enums. #91

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ pub enum DataBits {
Eight,
}

impl fmt::Display for DataBits {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
DataBits::Five => write!(f, "Five"),
DataBits::Six => write!(f, "Six"),
DataBits::Seven => write!(f, "Seven"),
DataBits::Eight => write!(f, "Eight"),
}
}
}

/// Parity checking modes
///
/// When parity checking is enabled (`Odd` or `Even`) an extra bit is transmitted with
Expand All @@ -165,6 +176,16 @@ pub enum Parity {
Even,
}

impl fmt::Display for Parity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Parity::None => write!(f, "None"),
Parity::Odd => write!(f, "Odd"),
Parity::Even => write!(f, "Even"),
}
}
}

/// Number of stop bits
///
/// Stop bits are transmitted after every character.
Expand All @@ -178,6 +199,15 @@ pub enum StopBits {
Two,
}

impl fmt::Display for StopBits {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
StopBits::One => write!(f, "One"),
StopBits::Two => write!(f, "Two"),
}
}
}

/// Flow control modes
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -192,6 +222,16 @@ pub enum FlowControl {
Hardware,
}

impl fmt::Display for FlowControl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
FlowControl::None => write!(f, "None"),
FlowControl::Software => write!(f, "Software"),
FlowControl::Hardware => write!(f, "Hardware"),
}
}
}

/// Specifies which buffer or buffers to purge when calling [`clear`]
///
/// [`clear`]: trait.SerialPort.html#tymethod.clear
Expand Down Expand Up @@ -620,6 +660,38 @@ impl<T: SerialPort> SerialPort for &mut T {
}
}

impl fmt::Debug for dyn SerialPort {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SerialPort ( ")?;
match self.name().as_ref() {
Some(n) => write!(f, "name: {} ", n)?,
None => {}
};
match self.baud_rate().as_ref() {
Ok(b) => write!(f, "baud_rate: {}", b)?,
Err(_) => {}
};
match self.data_bits().as_ref() {
Ok(b) => write!(f, "data_bits: {} ", b)?,
Err(_) => {}
};
match self.flow_control().as_ref() {
Ok(c) => write!(f, "flow_control: {} ", c)?,
Err(_) => {}
}
match self.parity().as_ref() {
Ok(p) => write!(f, "parity: {} ", p)?,
Err(_) => {}
}
match self.stop_bits().as_ref() {
Ok(s) => write!(f, "stop_bits: {} ", s)?,
Err(_) => {}
}

write!(f, ")")
}
}

/// Contains all possible USB information about a `SerialPort`
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down