From 21073b2e4b495fc063d11f7fb0adec2d7ebb4768 Mon Sep 17 00:00:00 2001 From: Cherry Date: Fri, 1 Dec 2023 13:48:24 -0500 Subject: [PATCH] use `try` blocks instead of unwrap spam --- pets-lib/src/dialogue/dbox.rs | 30 +++++++++++------------ pets-lib/src/lib.rs | 1 + pets-lib/src/main_menu.rs | 46 +++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/pets-lib/src/dialogue/dbox.rs b/pets-lib/src/dialogue/dbox.rs index dca50bcd..85f95a25 100644 --- a/pets-lib/src/dialogue/dbox.rs +++ b/pets-lib/src/dialogue/dbox.rs @@ -155,23 +155,23 @@ impl DialogBox { viewport_y }; - let mut y_tween = node.create_tween().unwrap(); - y_tween - .tween_property( - node.clone().upcast(), - "position:y".into(), - Variant::from(tw_end), - DBOX_TWEEN_TIME, - ) - .unwrap() - .from(Variant::from(self.node.get_position().y)) - .unwrap() - .set_trans(DBOX_TWEEN_TRANS) - .unwrap(); + let y_tween: Option> = try { + let mut y_tween = node.create_tween()?; + y_tween + .tween_property( + node.clone().upcast(), + "position:y".into(), + Variant::from(tw_end), + DBOX_TWEEN_TIME, + )? + .from(Variant::from(self.node.get_position().y))? + .set_trans(DBOX_TWEEN_TRANS)?; + y_tween + }; self.active = up; - self.tween = Some(y_tween.clone()); - y_tween + self.tween = y_tween.clone(); + y_tween.unwrap() } } diff --git a/pets-lib/src/lib.rs b/pets-lib/src/lib.rs index 463f3ae7..6beb0c5a 100644 --- a/pets-lib/src/lib.rs +++ b/pets-lib/src/lib.rs @@ -10,6 +10,7 @@ #![allow(dead_code)] #![allow(unused_imports)] #![feature(variant_count)] +#![feature(try_blocks)] use godot::engine::Engine; use godot::prelude::*; diff --git a/pets-lib/src/main_menu.rs b/pets-lib/src/main_menu.rs index b92e7037..1b8e03c0 100644 --- a/pets-lib/src/main_menu.rs +++ b/pets-lib/src/main_menu.rs @@ -72,27 +72,31 @@ impl TitleScreen { theme.get_color("default_color".into(), "RichTextLabel".into()) }; - let mut x_tween = node.create_tween().unwrap(); - x_tween - .tween_property( - node.clone().upcast(), - "position:x".into(), - Variant::from(target_x), - MENU_TWEEN_TIME, - ) - .unwrap() - .set_trans(MENU_TWEEN_TRANS); - - let mut color_tween = node.create_tween().unwrap(); - color_tween - .tween_property( - node.clone().upcast(), - "theme_override_colors/default_color".into(), - Variant::from(target_col), - MENU_TWEEN_TIME, - ) - .unwrap() - .set_trans(MENU_TWEEN_TRANS); + // Tweens + let tweens: Option<()> = try { + // tween x + node.create_tween()? + .tween_property( + node.clone().upcast(), + "position:x".into(), + Variant::from(target_x), + MENU_TWEEN_TIME, + )? + .set_trans(MENU_TWEEN_TRANS); + + // tween color + node.create_tween()? + .tween_property( + node.clone().upcast(), + "theme_override_colors/default_color".into(), + Variant::from(target_col), + MENU_TWEEN_TIME, + )? + .set_trans(MENU_TWEEN_TRANS); + }; + + // panic if null + tweens.unwrap(); // set bbcode // extremely ugly and hacky solution, but...