Skip to content

Commit

Permalink
Updated run to return a Result rather than Never
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Sep 2, 2023
1 parent 7ab945c commit cc2de02
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ rustflags = [
"-Wclippy::pedantic",
# globally allowed clippy exceptions
"-Aclippy::module_name_repetitions",
"-Aclippy::missing_errors_doc",
# Allow missing panic docs (see <https://github.com/rust-lang/rust-clippy/issues/11436>)
"-Aclippy::missing_panics_doc",
]
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion crates/gooey-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use std::any::{type_name, Any};
use std::collections::HashMap;
use std::fmt::Debug;
use std::error::Error;
use std::fmt::{Debug, Display};
use std::ops::{Deref, DerefMut};
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::sync::Arc;
Expand Down Expand Up @@ -820,6 +821,31 @@ impl Debug for Child {
}
}

pub struct EventLoopError(Box<dyn Error>);

impl EventLoopError {
pub fn new<T>(error: T) -> Self
where
T: Error + 'static,
{
Self(Box::new(error))
}
}

impl Debug for EventLoopError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}

impl Display for EventLoopError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}

impl Error for EventLoopError {}

#[test]
fn children_debug() {
#[derive(Debug)]
Expand Down
9 changes: 6 additions & 3 deletions crates/gooey-kludgine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use gooey_core::graphics::{Drawable, Options, TextMetrics};
use gooey_core::math::units::UPx;
use gooey_core::math::{IntoSigned, Point, Rect, ScreenUnit};
use gooey_core::window::{NewWindow, Window, WindowAttributes, WindowLevel};
use gooey_core::{Context, IntoNewWidget, NewWidget, Runtime, Widgets};
use gooey_core::{Context, EventLoopError, IntoNewWidget, NewWidget, Runtime, Widgets};
use gooey_raster::{DrawableState, RasterContext, Rasterizable, RasterizedApp, Surface};
use kludgine::app::winit::dpi::{PhysicalPosition, PhysicalSize};
use kludgine::app::winit::event::{ElementState, MouseButton};
Expand All @@ -17,11 +17,14 @@ use kludgine::shapes::Shape;
use kludgine::text::TextOrigin;
use kludgine::{Clipped, Color};

pub fn run<Widget>(widgets: Arc<Widgets<RasterizedApp<Kludgine>>>, init: NewWindow<Widget>) -> !
pub fn run<Widget>(
widgets: Arc<Widgets<RasterizedApp<Kludgine>>>,
init: NewWindow<Widget>,
) -> Result<(), EventLoopError>
where
Widget: gooey_core::Widget,
{
GooeyWindow::run_with((widgets, init))
GooeyWindow::run_with((widgets, init)).map_err(EventLoopError::new)
}

#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion examples/button.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::string::ToString;

use gooey::App;
use gooey_core::EventLoopError;
use gooey_widgets::Button;

fn main() {
fn main() -> Result<(), EventLoopError> {
App::default().run(|cx, _window| {
let counter = cx.new_dynamic(0i32);
let label = counter.map_each(ToString::to_string).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/counter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::string::ToString;

use gooey::App;
use gooey_core::Children;
use gooey_core::{Children, EventLoopError};
use gooey_widgets::{Button, Flex, Label};

fn main() {
fn main() -> Result<(), EventLoopError> {
App::default().run(|cx, _window| {
let counter = cx.new_dynamic(0i32);
let label = counter.map_each(ToString::to_string).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/counters.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use gooey::App;
use gooey_core::Children;
use gooey_core::{Children, EventLoopError};
use gooey_widgets::{Button, Flex};

fn main() {
fn main() -> Result<(), EventLoopError> {
App::default().run(|cx, _window| {
let counters = cx.new_dynamic(Children::new(cx));

Expand Down
4 changes: 2 additions & 2 deletions examples/mouse_event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gooey::App;
use gooey_core::events::MouseEvent;
use gooey_core::{Children, Context};
use gooey_core::{Children, Context, EventLoopError};
use gooey_widgets::{Button, Flex};

fn position_button(cx: &Context) -> Button {
Expand All @@ -10,7 +10,7 @@ fn position_button(cx: &Context) -> Button {
})
}

fn main() {
fn main() -> Result<(), EventLoopError> {
App::default().run(|cx, _window| {
Flex::rows(Children::new(cx).with(position_button).with(|cx| {
Flex::columns(
Expand Down
23 changes: 17 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ where

#[cfg(all(feature = "web", target_arch = "wasm32"))]
impl App<gooey_web::WebApp> {
pub fn run_with<Widget, Initializer>(self, init: Initializer) -> !
pub fn run_with<Widget, Initializer>(
self,
init: Initializer,
) -> Result<(), gooey_core::EventLoopError>
where
Initializer: FnOnce(WindowBuilder) -> NewWindow<Widget>,
Widget: gooey_core::Widget,
{
gooey_web::attach_to_body(self.widgets, init(WindowBuilder::default()));

wasm_bindgen::throw_str("This is not an actual error. Please ignore.");
Ok(())
}

pub fn run<Widget, Initializer>(self, init: Initializer) -> !
pub fn run<Widget, Initializer>(
self,
init: Initializer,
) -> Result<(), gooey_core::EventLoopError>
where
Initializer: FnOnce(&Context, &Window) -> Widget + std::panic::UnwindSafe + Send + 'static,
Widget: gooey_core::Widget,
Expand All @@ -65,15 +70,21 @@ impl App<gooey_web::WebApp> {

#[cfg(all(feature = "desktop", not(target_arch = "wasm32")))]
impl App<gooey_raster::RasterizedApp<gooey_kludgine::Kludgine>> {
pub fn run_with<Widget, Initializer>(self, init: Initializer) -> !
pub fn run_with<Widget, Initializer>(
self,
init: Initializer,
) -> Result<(), gooey_core::EventLoopError>
where
Initializer: FnOnce(WindowBuilder) -> NewWindow<Widget>,
Widget: gooey_core::Widget,
{
gooey_kludgine::run(self.widgets, init(WindowBuilder::default()))
}

pub fn run<Widget, Initializer>(self, init: Initializer) -> !
pub fn run<Widget, Initializer>(
self,
init: Initializer,
) -> Result<(), gooey_core::EventLoopError>
where
Initializer: FnOnce(&Context, &Window) -> Widget + std::panic::UnwindSafe + Send + 'static,
Widget: gooey_core::Widget,
Expand Down

0 comments on commit cc2de02

Please sign in to comment.