Skip to content

Commit

Permalink
Add conversions between DataBits, StopBits and their numeric represen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
DNedic authored and eldruin committed Feb 15, 2024
1 parent 819b640 commit 5d66560
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]
### Added
* Added conversions between `DataBits`, `StopBits` types and their numeric representations
### Changed
### Fixed
* Fixes a bug where `available_ports()` returned disabled devices on Windows.
Expand Down
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ impl fmt::Display for DataBits {
}
}

impl From<DataBits> for u8 {
fn from(value: DataBits) -> Self {
match value {
DataBits::Five => 5,
DataBits::Six => 6,
DataBits::Seven => 7,
DataBits::Eight => 8,
}
}
}

impl TryFrom<u8> for DataBits {
type Error = ();

fn try_from(value: u8) -> core::result::Result<Self, Self::Error> {
match value {
5 => Ok(Self::Five),
6 => Ok(Self::Six),
7 => Ok(Self::Seven),
8 => Ok(Self::Eight),
_ => Err(()),
}
}
}

/// Parity checking modes
///
/// When parity checking is enabled (`Odd` or `Even`) an extra bit is transmitted with
Expand Down Expand Up @@ -214,6 +239,27 @@ impl fmt::Display for StopBits {
}
}

impl From<StopBits> for u8 {
fn from(value: StopBits) -> Self {
match value {
StopBits::One => 1,
StopBits::Two => 2,
}
}
}

impl TryFrom<u8> for StopBits {
type Error = ();

fn try_from(value: u8) -> core::result::Result<Self, Self::Error> {
match value {
1 => Ok(Self::One),
2 => Ok(Self::Two),
_ => Err(()),
}
}
}

/// Flow control modes
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down

0 comments on commit 5d66560

Please sign in to comment.