Skip to content

Commit

Permalink
feat: FlowControl: add FromStr impl (#163)
Browse files Browse the repository at this point in the history
Allow creating a 'FlowControl' enum from a 'str' type. This is useful
when parsing strings from config files or command line arguments.
  • Loading branch information
jdswensen authored Mar 6, 2024
1 parent 954a622 commit 84f6066
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Added
* Added conversions between `DataBits`, `StopBits` types and their numeric representations
* Added `FromStr` implementation for `FlowControl`
### Changed
### Fixed
* Fixes a bug where `available_ports()` returned disabled devices on Windows.
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::convert::From;
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::str::FromStr;
use std::time::Duration;

#[cfg(unix)]
Expand Down Expand Up @@ -284,6 +285,19 @@ impl fmt::Display for FlowControl {
}
}

impl FromStr for FlowControl {
type Err = ();

fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
match s {
"None" | "none" | "n" => Ok(FlowControl::None),
"Software" | "software" | "SW" | "sw" | "s" => Ok(FlowControl::Software),
"Hardware" | "hardware" | "HW" | "hw" | "h" => Ok(FlowControl::Hardware),
_ => Err(()),
}
}
}

/// Specifies which buffer or buffers to purge when calling [`clear`]
///
/// [`clear`]: trait.SerialPort.html#tymethod.clear
Expand Down

0 comments on commit 84f6066

Please sign in to comment.