Skip to content

Commit 923456b

Browse files
authored
fix: fallback to default values instead of unwrap (#39)
1 parent 928d258 commit 923456b

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

src/adapter.rs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ impl Adapter {
6060
pub fn render(&self, frame: &mut Frame, color_mode: ColorMode, focused_block: FocusedBlock) {
6161
match self.device.mode {
6262
Mode::Station => {
63-
if self.device.station.is_some() {
64-
self.render_station_mode(frame, color_mode, focused_block);
65-
}
63+
self.render_station_mode(frame, color_mode, focused_block);
6664
}
6765
Mode::Ap => {
6866
if self.device.access_point.is_some() {
@@ -589,18 +587,15 @@ impl Adapter {
589587
}
590588
};
591589

590+
let mut station_state = "".to_string();
591+
let mut station_is_scanning = "".to_string();
592+
if let Some(station) = self.device.station.as_ref() {
593+
station_state = station.state.clone();
594+
station_is_scanning = station.is_scanning.clone().to_string();
595+
}
592596
let row = vec![
593-
Line::from(self.device.station.as_ref().unwrap().state.clone()).centered(),
594-
Line::from(
595-
self.device
596-
.station
597-
.as_ref()
598-
.unwrap()
599-
.is_scanning
600-
.clone()
601-
.to_string(),
602-
)
603-
.centered(),
597+
Line::from(station_state).centered(),
598+
Line::from(station_is_scanning).centered(),
604599
Line::from(station_frequency).centered(),
605600
Line::from(station_security).centered(),
606601
];
@@ -699,13 +694,12 @@ impl Adapter {
699694
frame.render_stateful_widget(station_table, station_block, &mut station_state);
700695

701696
// Known networks
702-
703-
let rows: Vec<Row> = self
704-
.device
705-
.station
706-
.as_ref()
707-
.unwrap()
708-
.known_networks
697+
let known_networks = if let Some(station) = self.device.station.as_ref() {
698+
&station.known_networks
699+
} else {
700+
&vec![]
701+
};
702+
let rows: Vec<Row> = known_networks
709703
.iter()
710704
.map(|(net, signal)| {
711705
let net = net.known_network.as_ref().unwrap();
@@ -857,26 +851,24 @@ impl Adapter {
857851
Style::default()
858852
});
859853

854+
let mut known_networks_state = if let Some(station) = self.device.station.as_ref() {
855+
station.known_networks_state.clone()
856+
} else {
857+
TableState::default()
858+
};
860859
frame.render_stateful_widget(
861860
known_networks_table,
862861
known_networks_block,
863-
&mut self
864-
.device
865-
.station
866-
.as_ref()
867-
.unwrap()
868-
.known_networks_state
869-
.clone(),
862+
&mut known_networks_state,
870863
);
871864

872865
// New networks
873-
874-
let rows: Vec<Row> = self
875-
.device
876-
.station
877-
.as_ref()
878-
.unwrap()
879-
.new_networks
866+
let new_networks = if let Some(station) = self.device.station.as_ref() {
867+
&station.new_networks
868+
} else {
869+
&vec![]
870+
};
871+
let rows: Vec<Row> = new_networks
880872
.iter()
881873
.map(|(net, signal)| {
882874
Row::new(vec![
@@ -982,16 +974,15 @@ impl Adapter {
982974
Style::default()
983975
});
984976

977+
let mut new_networks_state = if let Some(station) = self.device.station.as_ref() {
978+
station.new_networks_state.clone()
979+
} else {
980+
TableState::default()
981+
};
985982
frame.render_stateful_widget(
986983
new_networks_table,
987984
new_networks_block,
988-
&mut self
989-
.device
990-
.station
991-
.as_ref()
992-
.unwrap()
993-
.new_networks_state
994-
.clone(),
985+
&mut new_networks_state,
995986
);
996987
}
997988

0 commit comments

Comments
 (0)