Skip to content

Commit

Permalink
Refactoring & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Reiter committed Sep 28, 2024
1 parent 87e0880 commit 848fb95
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
18 changes: 11 additions & 7 deletions wireman/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::model::messages::{do_request, RequestResult};
use crate::model::selection::SelectionMode;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent};
use event_handler::EventHandler;
use logger::Logger;
pub(crate) use selection::methods::MethodsSelectionEventsHandler;
pub(crate) use selection::methods_search::MethodsSearchEventsHandler;
use selection::reflection::ReflectionDialogEventHandler;
Expand Down Expand Up @@ -159,15 +160,18 @@ impl App {
resp.set(&mut self.ctx.messages.borrow_mut().response.editor);
self.ctx.messages.borrow_mut().handler.take();
}
InternalStreamData::Reflection(desc) => {
if let Ok(desc) = desc {
self.ctx
.selection
.borrow_mut()
.update_proto_descriptor(desc.clone());
InternalStreamData::Reflection(desc) => match desc {
Ok(desc) => {
let d = desc.clone();
self.ctx.selection.borrow_mut().update_descriptor(d);
self.ctx.reflection.borrow_mut().error = None;
self.ctx.selection.borrow_mut().selection_mode = SelectionMode::Reflection;
}
}
Err(err) => {
self.ctx.reflection.borrow_mut().error = Some(err.clone());
Logger::critical(err);
}
},
}
}

Expand Down
21 changes: 8 additions & 13 deletions wireman/src/model/reflection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(clippy::module_name_repetitions)]
use core::ProtoDescriptor;
use logger::Logger;
use std::cell::RefCell;
use std::rc::Rc;
use tokio::sync::mpsc::Sender;
Expand All @@ -21,6 +20,8 @@ pub struct ReflectionModel {
pub selection: Rc<RefCell<SelectionModel>>,
/// Dispatch a reflection event
pub dispatch_reflection: bool,
/// An error that occured during reflection
pub error: Option<String>,
}

impl ReflectionModel {
Expand All @@ -36,31 +37,25 @@ impl ReflectionModel {
headers,
selection,
dispatch_reflection: false,
error: None,
}
}

pub fn dispatch_reflection(&mut self) {
Logger::debug("boom toggle");
self.dispatch_reflection = true;
}

pub fn handle_reflection(&mut self, sx: Sender<InternalStreamData>) {
let host = self.headers.borrow().address();
self.dispatch_reflection = false;
tokio::spawn(async move {
Logger::debug("spawn");
match ProtoDescriptor::reflect(&host).await {
Ok(desc) => {
let _ = sx.send(InternalStreamData::Reflection(Ok(desc))).await;
}
let event = match ProtoDescriptor::reflect(&host).await {
Ok(desc) => InternalStreamData::Reflection(Ok(desc)),
Err(err) => {
let err_str = format!("Server reflection failed: {err}");
let _ = sx
.send(InternalStreamData::Reflection(Err(err_str.clone())))
.await;
Logger::critical(err_str)
InternalStreamData::Reflection(Err(format!("Server reflection failed: {err}")))
}
}
};
let _ = sx.send(event).await;
});
}
}
2 changes: 1 addition & 1 deletion wireman/src/model/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl SelectionModel {
}

/// Update the proto descriptor.
pub fn update_proto_descriptor(&mut self, desc: ProtoDescriptor) {
pub fn update_descriptor(&mut self, desc: ProtoDescriptor) {
self.core_client.borrow_mut().update_proto_descriptor(desc);
self.load_core_services_and_methods_from_files();
}
Expand Down
14 changes: 9 additions & 5 deletions wireman/src/view/reflection_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::root::layout;
use crate::model::headers::{AuthSelection, HeadersTab};
use crate::model::reflection::ReflectionModel;
use ratatui::layout::Layout;
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::{
buffer::Buffer,
Expand Down Expand Up @@ -40,10 +41,10 @@ impl Widget for ReflectionDialog {
block.render(area, buf);
inner_area
};
let [main, footer] = Layout::vertical([Min(0), Length(1)]).areas(area);
let [m, f] = Layout::vertical([Min(0), Length(1)]).areas(area);

// Address
let layout = layout(main, Direction::Vertical, &[1, 1, 3, 1, 1, 4]);
let layout = layout(m, Direction::Vertical, &[1, 1, 3, 1, 1, 4]);
let [_, addr_title, addr_content, _, auth_title, auth_content] = layout;
ListElements::VDivider(String::from(" Address ")).render(addr_title, buf);
Address {
Expand Down Expand Up @@ -71,8 +72,11 @@ impl Widget for ReflectionDialog {
};
body.render(auth_content, buf);

Line::from("Press Enter ")
.right_aligned()
.render(footer, buf);
// Status line
if let Some(err) = self.model.error {
Line::from(err).left_aligned().red().render(f, buf);
} else {
Line::from("Press Enter ").right_aligned().render(f, buf);
}
}
}

0 comments on commit 848fb95

Please sign in to comment.