Skip to content

Commit 84f6066

Browse files
authored
feat: FlowControl: add FromStr impl (#163)
Allow creating a 'FlowControl' enum from a 'str' type. This is useful when parsing strings from config files or command line arguments.
1 parent 954a622 commit 84f6066

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ project adheres to [Semantic Versioning](https://semver.org/).
88
## [Unreleased]
99
### Added
1010
* Added conversions between `DataBits`, `StopBits` types and their numeric representations
11+
* Added `FromStr` implementation for `FlowControl`
1112
### Changed
1213
### Fixed
1314
* Fixes a bug where `available_ports()` returned disabled devices on Windows.

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::convert::From;
3636
use std::error::Error as StdError;
3737
use std::fmt;
3838
use std::io;
39+
use std::str::FromStr;
3940
use std::time::Duration;
4041

4142
#[cfg(unix)]
@@ -284,6 +285,19 @@ impl fmt::Display for FlowControl {
284285
}
285286
}
286287

288+
impl FromStr for FlowControl {
289+
type Err = ();
290+
291+
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
292+
match s {
293+
"None" | "none" | "n" => Ok(FlowControl::None),
294+
"Software" | "software" | "SW" | "sw" | "s" => Ok(FlowControl::Software),
295+
"Hardware" | "hardware" | "HW" | "hw" | "h" => Ok(FlowControl::Hardware),
296+
_ => Err(()),
297+
}
298+
}
299+
}
300+
287301
/// Specifies which buffer or buffers to purge when calling [`clear`]
288302
///
289303
/// [`clear`]: trait.SerialPort.html#tymethod.clear

0 commit comments

Comments
 (0)