Skip to content

Commit

Permalink
refactor(engine): ♻️ eliminate unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jak2k committed Jan 4, 2024
1 parent dfb240c commit d8edbfd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
17 changes: 9 additions & 8 deletions nexara_text_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod prelude {
pub use crate::scene::{Option, Scene, Scenes};
pub use crate::scenify;
pub use serde::{Deserialize, Serialize};
pub use color_eyre::Result;
}

#[macro_export]
Expand Down Expand Up @@ -37,17 +38,17 @@ macro_rules! game {

impl Scenes<$enum_name, $struct_name> for $enum_name {

fn get_current_scene(&self, context: &mut $struct_name) -> Scene<$enum_name> {
$body(self, context)
fn get_current_scene(&self, context: &mut $struct_name) -> Result<Scene<$enum_name>> {
Ok($body(self, context))
}

fn new() -> Self {
$initial_scene
}

fn run(&mut self, context: &mut $struct_name) {
fn run(&mut self, context: &mut $struct_name)-> Result<()> {
let mut old_context = context.clone();
let mut scene = self.get_current_scene(context);
let mut scene = self.get_current_scene(context)?;

{
use std::io::Write;
Expand All @@ -59,15 +60,15 @@ macro_rules! game {
loop {


nexara_text_engine::render::render(&scene);
nexara_text_engine::render::render(&scene)?;

let index = nexara_text_engine::input::input_letter(scene.options.len());

old_context = context.clone();

// get the target scene
let target = scene.options[index].target.clone();
scene = target.get_current_scene(context);
scene = target.get_current_scene(context)?;

use std::io::Write;
let mut file = std::fs::File::create("save.json").unwrap();
Expand All @@ -77,7 +78,7 @@ macro_rules! game {
}
}

fn main() {
fn main() -> Result<()> {
let mut scenes = $enum_name::new();
let mut context = $initial_context;

Expand All @@ -90,7 +91,7 @@ macro_rules! game {
context = context_;
}

scenes.run(&mut context);
scenes.run(&mut context)
}
};
}
44 changes: 27 additions & 17 deletions nexara_text_engine/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ use crossterm::{
style::{Color, Print, ResetColor, SetForegroundColor},
};

fn typewriter(text: &str) {
use color_eyre::Result;

fn typewriter(text: &str) -> Result<()> {
const MAX_DELAY: u64 = 50;

for c in text.chars() {
execute!(std::io::stdout(), Print(c)).unwrap();
execute!(std::io::stdout(), Print(c))?;
std::thread::sleep(std::time::Duration::from_millis(
rand::random::<u64>() % MAX_DELAY,
));
}

Ok(())
}

fn render_location(location: &str) {
fn render_location(location: &str) -> Result<()> {
// calculate the location indent (centered) using the width of the terminal
let mut location_indent =
(i32::from(crossterm::terminal::size().unwrap().0) - location.len() as i32) / 2;
(i32::from(crossterm::terminal::size()?.0) - location.len() as i32) / 2;

if location_indent < 0 {
location_indent = 0;
Expand All @@ -33,11 +37,13 @@ fn render_location(location: &str) {
ResetColor,
Print("\n\n"),
)
.unwrap();
?;

Ok(())
}

fn render_options<T>(options: &Vec<crate::scene::Option<T>>) {
execute!(std::io::stdout(), Print("\n")).unwrap();
fn render_options<T>(options: &Vec<crate::scene::Option<T>>) -> Result<()> {
execute!(std::io::stdout(), Print("\n"))?;

let mut next_option = 'A';

Expand All @@ -51,13 +57,13 @@ fn render_options<T>(options: &Vec<crate::scene::Option<T>>) {
Print(") "),
SetForegroundColor(Color::Blue),
)
.unwrap();
?;

next_option = (next_option as u8 + 1) as char;

typewriter(&option.title);
typewriter(&option.title)?;

execute!(std::io::stdout(), ResetColor).unwrap();
execute!(std::io::stdout(), ResetColor)?;
}

execute!(
Expand All @@ -67,10 +73,12 @@ fn render_options<T>(options: &Vec<crate::scene::Option<T>>) {
Print(" Exit"),
ResetColor,
)
.unwrap();
?;

Ok(())
}

pub fn render<T>(scene: &crate::scene::Scene<T>) {
pub fn render<T>(scene: &crate::scene::Scene<T>) -> Result<()> {
// ASCII art how the screen should look like this (the lines are not part of the output):
/*
Expand All @@ -94,16 +102,18 @@ pub fn render<T>(scene: &crate::scene::Scene<T>) {
std::io::stdout(),
crossterm::terminal::Clear(crossterm::terminal::ClearType::All)
)
.unwrap();
?;

execute!(std::io::stdout(), crossterm::cursor::MoveTo(0, 0)).unwrap();
execute!(std::io::stdout(), crossterm::cursor::MoveTo(0, 0))?;

// Print the location
render_location(&scene.location);
render_location(&scene.location)?;

// Print the text with animation
typewriter(&scene.text);
typewriter(&scene.text)?;

// Print the options
render_options(&scene.options);
render_options(&scene.options)?;

Ok(())
}
14 changes: 8 additions & 6 deletions nexara_text_engine/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use color_eyre::Result;

pub struct Scene<ScenesData> {
pub location: String,
pub text: String,
Expand All @@ -14,18 +16,18 @@ pub trait Scenes<
Context: serde::Serialize + for<'b> serde::Deserialize<'b> + Clone,
>
{
fn get_current_scene(&self, context: &mut Context) -> Scene<ScenesData>;
fn get_current_scene(&self, context: &mut Context) -> Result<Scene<ScenesData>>;
fn new() -> Self;
fn run(&mut self, context: &mut Context) {
let mut scene = self.get_current_scene(context);
fn run(&mut self, context: &mut Context) -> Result<()> {
let mut scene = self.get_current_scene(context)?;

loop {
crate::render::render(&scene);
crate::render::render(&scene)?;

let index = crate::input::input_letter(scene.options.len());

// get the target scene
scene = scene.options[index].target.get_current_scene(context);
}
scene = scene.options[index].target.get_current_scene(context)?;
};
}
}

0 comments on commit d8edbfd

Please sign in to comment.