From 84f6066ffd92056829b91771afc7eebcbc6642e2 Mon Sep 17 00:00:00 2001 From: Jake Swensen Date: Wed, 6 Mar 2024 03:07:01 -0600 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + src/lib.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9479029..bdc8e453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 47ecffbd..f369c41c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] @@ -284,6 +285,19 @@ impl fmt::Display for FlowControl { } } +impl FromStr for FlowControl { + type Err = (); + + fn from_str(s: &str) -> core::result::Result { + 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