Skip to content

Commit 5417449

Browse files
Jason Mobarakjohn-michaelburke
andauthored
simpler FlowControl wrapper, wrap generated Tabs object (#54)
* Respond to review requests. * simpler FlowControl wrapper, wrap generated Tabs object Co-authored-by: John Michael Burke <[email protected]>
1 parent aba6837 commit 5417449

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

console_backend/src/cli_options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{ops, path::PathBuf, str::FromStr};
33

44
use crate::common_constants::Tabs;
55
use crate::constants::{AVAILABLE_BAUDRATES, AVAILABLE_REFRESH_RATES, TAB_LIST};
6-
use crate::types::FlowControl;
6+
use crate::types::{CliTabs, FlowControl};
77

88
#[derive(Clap, Debug)]
99
#[clap(name = "swift_navigation_console", about = "Swift Navigation Console.")]
@@ -24,8 +24,8 @@ pub struct CliOptions {
2424
#[clap(long = "refresh-rate", validator(is_refresh_rate))]
2525
pub refresh_rate: Option<u8>,
2626

27-
#[clap(long = "tab", validator(is_tab))]
28-
pub tab: Option<String>,
27+
#[clap(long = "tab")]
28+
pub tab: Option<CliTabs>,
2929
}
3030

3131
impl CliOptions {

console_backend/src/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use pyo3::types::PyBytes;
66

77
use async_logger_log::Logger;
88

9+
use clap::Clap;
910
use std::{
1011
io::{BufReader, Cursor},
1112
str::FromStr,

console_backend/src/types.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::common_constants as cc;
1+
use crate::common_constants::{self as cc, Tabs};
22
use crate::constants::*;
33
use crate::formatters::*;
44
use crate::piksi_tools_constants::*;
@@ -25,6 +25,7 @@ use std::{
2525
hash::Hash,
2626
net::TcpStream,
2727
ops::Deref,
28+
str::FromStr,
2829
sync::{mpsc::Sender, Arc, Mutex},
2930
thread,
3031
thread::JoinHandle,
@@ -414,13 +415,9 @@ pub enum RealtimeDelay {
414415
// NavBar Types.
415416
// Enum wrapping around various Vel NED Message types.
416417
#[derive(Debug)]
417-
pub enum FlowControl {
418-
None,
419-
Software,
420-
Hardware,
421-
}
418+
pub struct FlowControl(SPFlowControl);
422419

423-
impl std::str::FromStr for FlowControl {
420+
impl FromStr for FlowControl {
424421
type Err = String;
425422

426423
/// Convert flow control string slice to expected serialport FlowControl variant.
@@ -430,9 +427,9 @@ impl std::str::FromStr for FlowControl {
430427
/// - `sat_str`: The signal code.
431428
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
432429
match s {
433-
FLOW_CONTROL_NONE => Ok(FlowControl::None),
434-
FLOW_CONTROL_SOFTWARE => Ok(FlowControl::Software),
435-
FLOW_CONTROL_HARDWARE => Ok(FlowControl::Hardware),
430+
FLOW_CONTROL_NONE => Ok(FlowControl(SPFlowControl::None)),
431+
FLOW_CONTROL_SOFTWARE => Ok(FlowControl(SPFlowControl::Software)),
432+
FLOW_CONTROL_HARDWARE => Ok(FlowControl(SPFlowControl::Hardware)),
436433
_ => Err(format!(
437434
"Not a valid flow control option. Choose from [\"{}\", \"{}\", \"{}\"]",
438435
FLOW_CONTROL_NONE, FLOW_CONTROL_SOFTWARE, FLOW_CONTROL_HARDWARE
@@ -442,13 +439,32 @@ impl std::str::FromStr for FlowControl {
442439
}
443440

444441
impl Deref for FlowControl {
442+
445443
type Target = SPFlowControl;
444+
446445
fn deref(&self) -> &Self::Target {
447-
match self {
448-
FlowControl::None => &SPFlowControl::None,
449-
FlowControl::Software => &SPFlowControl::Software,
450-
FlowControl::Hardware => &SPFlowControl::Hardware,
451-
}
446+
&self.0
447+
}
448+
}
449+
450+
#[derive(Debug)]
451+
pub struct CliTabs(Tabs);
452+
453+
impl Deref for CliTabs {
454+
455+
type Target = Tabs;
456+
457+
fn deref(&self) -> &Self::Target {
458+
&self.0
459+
}
460+
}
461+
462+
impl FromStr for CliTabs {
463+
type Err = String;
464+
465+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
466+
// TODO: add custom error handling
467+
Ok(CliTabs(Tabs::from_str(s).map_err(|e| format!("{}", e))?))
452468
}
453469
}
454470

0 commit comments

Comments
 (0)