From 427db2b96efd593f4a2c8a0f2ac070d96ae136c6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 18 Oct 2022 12:18:18 +0200 Subject: [PATCH 1/5] Make method name more explicit --- crates/fj-host/src/lib.rs | 5 ++++- crates/fj-window/src/run.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index 57c133714..3fa91634a 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -307,7 +307,10 @@ impl Watcher { /// /// Returns `None`, if the model has not changed since the last time this /// method was called. - pub fn receive(&self, status: &mut StatusReport) -> Option { + pub fn receive_shape( + &self, + status: &mut StatusReport, + ) -> Option { match self.channel.try_recv() { Ok(()) => { let shape = match self.model.load_once(&self.parameters, status) diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 350216313..6b446c633 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -50,7 +50,7 @@ pub fn run( trace!("Handling event: {:?}", event); if let Some(watcher) = &watcher { - if let Some(new_shape) = watcher.receive(&mut status) { + if let Some(new_shape) = watcher.receive_shape(&mut status) { match shape_processor.process(&new_shape) { Ok(new_shape) => { viewer.handle_shape_update(new_shape); From 3a5f8dc2fa9f58098f61f44a22023b954c71fe3a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 19 Oct 2022 11:30:16 +0200 Subject: [PATCH 2/5] Simplify variable name --- crates/fj-window/src/run.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 6b446c633..baf846b54 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -52,8 +52,8 @@ pub fn run( if let Some(watcher) = &watcher { if let Some(new_shape) = watcher.receive_shape(&mut status) { match shape_processor.process(&new_shape) { - Ok(new_shape) => { - viewer.handle_shape_update(new_shape); + Ok(shape) => { + viewer.handle_shape_update(shape); } Err(err) => { // Can be cleaned up, once `Report` is stable: From d7dc5a3cb2838e0e26ca455068c8cbfd1f8688cd Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 19 Oct 2022 11:32:13 +0200 Subject: [PATCH 3/5] Simplify variable name --- crates/fj-window/src/run.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index baf846b54..0953a1721 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -50,8 +50,8 @@ pub fn run( trace!("Handling event: {:?}", event); if let Some(watcher) = &watcher { - if let Some(new_shape) = watcher.receive_shape(&mut status) { - match shape_processor.process(&new_shape) { + if let Some(shape) = watcher.receive_shape(&mut status) { + match shape_processor.process(&shape) { Ok(shape) => { viewer.handle_shape_update(shape); } From acce6124c6483ccc2b4339f4f46ccbaf712a7cc6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 19 Oct 2022 11:35:00 +0200 Subject: [PATCH 4/5] Return `Result` from `Watcher::receive_shape` --- crates/fj-host/src/lib.rs | 8 ++++---- crates/fj-window/src/run.rs | 39 +++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index 3fa91634a..8c7c36e7c 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -310,7 +310,7 @@ impl Watcher { pub fn receive_shape( &self, status: &mut StatusReport, - ) -> Option { + ) -> Result, Error> { match self.channel.try_recv() { Ok(()) => { let shape = match self.model.load_once(&self.parameters, status) @@ -320,18 +320,18 @@ impl Watcher { // An error is being displayed to the user via the // `StatusReport that is passed to `load_once` above, so // no need to do anything else here. - return None; + return Ok(None); } Err(err) => { panic!("Error reloading model: {:?}", err); } }; - Some(shape) + Ok(Some(shape)) } Err(mpsc::TryRecvError::Empty) => { // Nothing to receive from the channel. - None + Ok(None) } Err(mpsc::TryRecvError::Disconnected) => { // The other end has disconnected. This is probably the result diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 0953a1721..8d101dc26 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -50,27 +50,36 @@ pub fn run( trace!("Handling event: {:?}", event); if let Some(watcher) = &watcher { - if let Some(shape) = watcher.receive_shape(&mut status) { - match shape_processor.process(&shape) { - Ok(shape) => { - viewer.handle_shape_update(shape); - } - Err(err) => { - // Can be cleaned up, once `Report` is stable: - // https://doc.rust-lang.org/std/error/struct.Report.html + match watcher.receive_shape(&mut status) { + Ok(shape) => { + if let Some(shape) = shape { + match shape_processor.process(&shape) { + Ok(shape) => { + viewer.handle_shape_update(shape); + } + Err(err) => { + // Can be cleaned up, once `Report` is stable: + // https://doc.rust-lang.org/std/error/struct.Report.html - println!("Shape processing error: {}", err); + println!("Shape processing error: {}", err); - let mut current_err = &err as &dyn error::Error; - while let Some(err) = current_err.source() { - println!(); - println!("Caused by:"); - println!(" {}", err); + let mut current_err = &err as &dyn error::Error; + while let Some(err) = current_err.source() { + println!(); + println!("Caused by:"); + println!(" {}", err); - current_err = err; + current_err = err; + } + } } } } + Err(err) => { + println!("Error receiving updated shape: {}", err); + *control_flow = ControlFlow::Exit; + return; + } } } From 5e2fbec8b1b59ae58a274b1465d9c638f0c1e4b7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 19 Oct 2022 11:37:41 +0200 Subject: [PATCH 5/5] Return error instead of panicking --- crates/fj-host/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index 8c7c36e7c..3b09d70a8 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -323,7 +323,7 @@ impl Watcher { return Ok(None); } Err(err) => { - panic!("Error reloading model: {:?}", err); + return Err(err); } };