From cd01bb65676100ac6e2a47dfcbd5393394958f18 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 28 May 2023 15:45:16 -0700 Subject: [PATCH] cargo fmt --- renrs-gui/src/lib.rs | 37 ++++++++++++++++------- renrs/src/lib.rs | 4 +-- renrs/src/parser/block.rs | 58 ++++++++++++++++++++----------------- renrs/src/parser/command.rs | 43 +++++++++++++++++---------- renrs/src/parser/control.rs | 8 +++-- renrs/src/parser/event.rs | 8 +++-- renrs/src/parser/mod.rs | 10 +++---- renrs/src/parser/token.rs | 7 +++-- renrs/src/parser/utils.rs | 12 ++++---- 9 files changed, 114 insertions(+), 73 deletions(-) diff --git a/renrs-gui/src/lib.rs b/renrs-gui/src/lib.rs index b285d60..25be051 100644 --- a/renrs-gui/src/lib.rs +++ b/renrs-gui/src/lib.rs @@ -1,16 +1,19 @@ -use std::{path::PathBuf, sync::{Arc, Mutex}}; use std::thread; use std::time::Duration; +use std::{ + path::PathBuf, + sync::{Arc, Mutex}, +}; use eframe::egui; use renrs::State; +use egui_sfml::SfEgui; use sfml::{ - graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable}, + graphics::{Color, RectangleShape, RenderTarget, RenderWindow, Transformable}, system::Vector2f, - window::{Event, mouse, Key, Style}, + window::{mouse, Event, Key, Style}, }; -use egui_sfml::SfEgui; const WIDTH: f32 = 800.0; const HEIGHT: f32 = 600.0; @@ -121,8 +124,17 @@ impl App { while window.is_open() { while let Some(event) = window.poll_event() { match event { - Event::Closed | Event::KeyPressed { code: Key::Escape, .. } => return, - Event::MouseButtonPressed { button: mouse::Button::Left, .. } | Event::KeyPressed { code: Key::Space, .. } => self.handle_interaction(), + Event::Closed + | Event::KeyPressed { + code: Key::Escape, .. + } => return, + Event::MouseButtonPressed { + button: mouse::Button::Left, + .. + } + | Event::KeyPressed { + code: Key::Space, .. + } => self.handle_interaction(), _ => {} } } @@ -137,15 +149,18 @@ impl App { ..Default::default() }) .show(ctx, |ui| { - if ctx.input(|i| + if ctx.input(|i| { i.key_pressed(Key::Space) - || i.pointer.button_clicked(PointerButton::Primary) - ) { + || i.pointer.button_clicked(PointerButton::Primary) + }) { self.handle_interaction(); } - ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()])); + ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| { + ui.heading(&self.text[0..*self.chars.lock().unwrap()]) + }); }); - }).unwrap(); + }) + .unwrap(); } let Vector2f { x, y } = shape.position(); diff --git a/renrs/src/lib.rs b/renrs/src/lib.rs index 6ec26d6..7e8193a 100644 --- a/renrs/src/lib.rs +++ b/renrs/src/lib.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, rc::Rc, cell::RefCell}; +use std::{cell::RefCell, path::PathBuf, rc::Rc}; pub mod parser; use parser::block::CommandBlock; @@ -30,4 +30,4 @@ impl State { fn next_command(&mut self) -> Option { self.script.borrow_mut().next() } -} \ No newline at end of file +} diff --git a/renrs/src/parser/block.rs b/renrs/src/parser/block.rs index 6602d20..e1f770f 100644 --- a/renrs/src/parser/block.rs +++ b/renrs/src/parser/block.rs @@ -1,7 +1,11 @@ -use std::{rc::Rc, cell::RefCell, collections::HashMap}; - -use super::{command::{Command, parse_command}, Pair, token::Token, Rule, control::{parse_control, Control}}; +use std::{cell::RefCell, collections::HashMap, rc::Rc}; +use super::{ + command::{parse_command, Command}, + control::{parse_control, Control}, + token::Token, + Pair, Rule, +}; #[derive(Debug)] pub struct CommandBlock { @@ -29,28 +33,22 @@ impl CommandBlock { result = Some(command.clone()); next += 1; break; - }, - BlockElement::Block(block) => { - match block.borrow_mut().next() { - Some(command) => { - result = Some(command.clone()); - break; - }, - None => { - next += 1; - } - } } + BlockElement::Block(block) => match block.borrow_mut().next() { + Some(command) => { + result = Some(command.clone()); + break; + } + None => { + next += 1; + } + }, }; } - self.next = if count >= next { - Some(next) - } else { - None - }; + self.next = if count >= next { Some(next) } else { None }; result } - + fn evaluate_control(&self) -> bool { use Control::*; self.control.as_ref().map_or(true, |control| match control { @@ -69,7 +67,7 @@ impl CommandBlock { } else { variables } - }, + } None => HashMap::new(), } } @@ -93,12 +91,19 @@ enum BlockElement { Block(Rc>), } -pub fn parse_block(pair: Pair, definitions: Option>>>) -> Rc> { +pub fn parse_block( + pair: Pair, + definitions: Option>>>, +) -> Rc> { //let variables: HashMap = HashMap::new(); let is_root = definitions.is_none(); let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new()))); let block_rc = Rc::new(RefCell::new(CommandBlock { - variables: if is_root { Some(definitions.clone()) } else { None }, + variables: if is_root { + Some(definitions.clone()) + } else { + None + }, ..Default::default() })); { @@ -114,7 +119,7 @@ pub fn parse_block(pair: Pair, definitions: Option BlockElement::Command(match parse_command(pair) { Command::Define { variable, value } => { let mut value = value; @@ -137,7 +142,8 @@ pub fn parse_block(pair: Pair, definitions: Option>) -> Option { use Command::*; - Some(match self { - Say { name, text } => Event::Say { - name: name.clone(), - text: text.clone(), - }, - Define { .. } => panic!("define command should not be executed at runtime!"), - Eat { .. } => return None, - }.process(context)) + Some( + match self { + Say { name, text } => Event::Say { + name: name.clone(), + text: text.clone(), + }, + Define { .. } => panic!("define command should not be executed at runtime!"), + Eat { .. } => return None, + } + .process(context), + ) } } @@ -42,10 +51,14 @@ pub fn parse_command(pair: Pair) -> Command { name: Some(name.to_owned()), text: text.to_owned(), }, - [Keyword(keyword), Keyword(variable), Keyword(equals), value] if keyword.eq("define") && equals.eq("=") => Define { - variable: variable.to_owned(), - value: value.clone(), - }, + [Keyword(keyword), Keyword(variable), Keyword(equals), value] + if keyword.eq("define") && equals.eq("=") => + { + Define { + variable: variable.to_owned(), + value: value.clone(), + } + } [Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat { food: food.to_owned(), politely: match tail { @@ -55,4 +68,4 @@ pub fn parse_command(pair: Pair) -> Command { }, _ => unknown!(), } -} \ No newline at end of file +} diff --git a/renrs/src/parser/control.rs b/renrs/src/parser/control.rs index 06bb5bd..deccae2 100644 --- a/renrs/src/parser/control.rs +++ b/renrs/src/parser/control.rs @@ -1,4 +1,8 @@ -use super::{token::Token, Pair, utils::{describe_line, parse_line}}; +use super::{ + token::Token, + utils::{describe_line, parse_line}, + Pair, +}; #[derive(Debug)] #[allow(dead_code)] @@ -29,4 +33,4 @@ pub fn parse_control(pair: Pair) -> Control { }, _ => unknown!(), } -} \ No newline at end of file +} diff --git a/renrs/src/parser/event.rs b/renrs/src/parser/event.rs index d1b84e4..8bdcced 100644 --- a/renrs/src/parser/event.rs +++ b/renrs/src/parser/event.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc, cell::RefCell, collections::HashMap}; +use std::{cell::RefCell, collections::HashMap, rc::Rc}; use regex::Regex; @@ -23,7 +23,9 @@ impl Event { Say { name, text } => { let context = context.borrow(); let variables = context.get_variables(); - *name = name.as_deref().map(|name| interpolate_string(&name, &variables)); + *name = name + .as_deref() + .map(|name| interpolate_string(&name, &variables)); *text = interpolate_string(&text, &variables); } } @@ -43,4 +45,4 @@ fn interpolate_string(input: &str, variables: &HashMap) -> String }); interpolated_string.into_owned() -} \ No newline at end of file +} diff --git a/renrs/src/parser/mod.rs b/renrs/src/parser/mod.rs index 8bb7fc8..e446c75 100644 --- a/renrs/src/parser/mod.rs +++ b/renrs/src/parser/mod.rs @@ -1,18 +1,18 @@ -pub mod token; +pub mod block; pub mod command; pub mod control; pub mod event; -pub mod block; +pub mod token; mod utils; pub type Pair<'a> = pest::iterators::Pair<'a, Rule>; -use std::{path::PathBuf, rc::Rc, cell::RefCell, fs}; +use std::{cell::RefCell, fs, path::PathBuf, rc::Rc}; pub use pest::Parser; use pest_derive::Parser; -use block::{CommandBlock, parse_block}; +use block::{parse_block, CommandBlock}; #[derive(Parser)] #[grammar = "rpy.pest"] @@ -30,4 +30,4 @@ pub fn parse(script: &str) -> Rc> { .next() .unwrap(); parse_block(file, None) -} \ No newline at end of file +} diff --git a/renrs/src/parser/token.rs b/renrs/src/parser/token.rs index dc436c0..32660f5 100644 --- a/renrs/src/parser/token.rs +++ b/renrs/src/parser/token.rs @@ -1,4 +1,4 @@ -use super::{Rule, Pair}; +use super::{Pair, Rule}; // Raw script tokens #[derive(Debug, Clone)] @@ -40,7 +40,8 @@ impl ToString for Token { Boolean(boolean) => match boolean { true => "True", false => "False", - }.to_owned(), + } + .to_owned(), Number(number) => number.to_string(), } } @@ -79,4 +80,4 @@ pub fn parse_token(pair: Pair) -> Token { Rule::Keyword => Token::Keyword(pair.as_str().to_owned()), __ => unreachable!(), } -} \ No newline at end of file +} diff --git a/renrs/src/parser/utils.rs b/renrs/src/parser/utils.rs index 566a909..395257c 100644 --- a/renrs/src/parser/utils.rs +++ b/renrs/src/parser/utils.rs @@ -1,10 +1,10 @@ -use super::{Pair, token::{Token, parse_token}}; +use super::{ + token::{parse_token, Token}, + Pair, +}; pub fn parse_line(pair: Pair) -> Vec { - pair - .into_inner() - .map(|pair| parse_token(pair)) - .collect() + pair.into_inner().map(|pair| parse_token(pair)).collect() } // Line description e.g. [String, Keyword, Array] @@ -18,4 +18,4 @@ pub fn describe_line(line: &[Token]) -> String { } description.push_str("]"); description -} \ No newline at end of file +}