From e7458c12adfe1440f5dc32e1c52d5e2376f36df2 Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Tue, 21 Apr 2020 00:09:43 -0600 Subject: [PATCH] cargo fix and fmt --- src/bin/dicebot-cmd.rs | 4 -- src/bot.rs | 14 ++++--- src/commands.rs | 15 ++++--- src/commands/parser.rs | 11 ++---- src/dice.rs | 1 - src/dice/parser.rs | 8 +--- src/lib.rs | 5 +-- src/parser.rs | 5 +-- src/roll.rs | 89 +++++++++++++++++++++++++++++------------- 9 files changed, 85 insertions(+), 67 deletions(-) diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index 5aeeb37..57a87cd 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -1,8 +1,4 @@ -use axfive_matrix_dicebot::dice::parser::parse_element_expression; -use axfive_matrix_dicebot::roll::{Roll, Rolled}; use axfive_matrix_dicebot::commands::parse_command; -use std::error::Error; -use std::string::ToString; fn main() -> Result<(), String> { let command = std::env::args().skip(1).collect::>().join(" "); diff --git a/src/bot.rs b/src/bot.rs index 7a158c9..0333ff9 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,4 +1,4 @@ -use crate::matrix::{SyncCommand, Event, RoomEvent, MessageContent}; +use crate::matrix::{Event, MessageContent, RoomEvent, SyncCommand}; use reqwest::{Client, Url}; use serde::{self, Deserialize, Serialize}; use std::fs; @@ -118,19 +118,21 @@ impl DiceBot { let sync: SyncCommand = serde_json::from_str(&body).unwrap(); // First join invited rooms for room in sync.rooms.invite.keys() { - let mut join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]); - self.client.post(join_url) + let join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]); + self.client + .post(join_url) .header("user-agent", USER_AGENT) .send() .await?; } - for (room_id, room) in sync.rooms.join.iter() { + for (_room_id, room) in sync.rooms.join.iter() { for event in &room.timeline.events { - if let Event::Room(RoomEvent{ + if let Event::Room(RoomEvent { content: MessageContent::Text(message), .. - }) = event { + }) = event + { // TODO: create command parser (maybe command.rs) to parse !roll/!r commands // and reply println!("Body: {}", message.body()); diff --git a/src/commands.rs b/src/commands.rs index d7c397d..739bd17 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,7 +1,6 @@ use crate::dice::ElementExpression; -use crate::roll::{Roll, Rolled}; -use nom::error::ErrorKind; -use nom::IResult; +use crate::roll::Roll; + pub mod parser; pub struct Execution { @@ -29,11 +28,11 @@ impl Command for RollCommand { fn execute(&self) -> Execution { let roll = self.0.roll(); let plain = format!("Dice: {}\nResult: {}", self.0, roll); - let html = format!("Dice: {}
Result: {}", self.0, roll); - Execution { - plain, - html, - } + let html = format!( + "Dice: {}
Result: {}", + self.0, roll + ); + Execution { plain, html } } } diff --git a/src/commands/parser.rs b/src/commands/parser.rs index 5023bb1..e562bc9 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -1,18 +1,15 @@ use nom::{ - tuple, - switch, - take_while, bytes::complete::{tag, take_while}, character::complete::digit1, character::is_alphabetic, complete, many0, named, sequence::tuple, - tag, IResult, + switch, tag, take_while, tuple, IResult, }; -use crate::parser::eat_whitespace; use crate::commands::{Command, RollCommand}; use crate::dice::parser::parse_element_expression; +use crate::parser::eat_whitespace; // Parse a roll expression. fn parse_roll(input: &str) -> IResult<&str, RollCommand> { @@ -29,14 +26,14 @@ pub fn parse_command(original_input: &str) -> IResult<&str, Option (input, result), - Err(e) => return Ok((original_input, None)), + Err(_e) => return Ok((original_input, None)), }; let (input, command) = match command { "r" | "roll" => { let (input, command) = parse_roll(input)?; let command: Box = Box::new(command); (input, command) - }, + } // No recognized command, ignore this. _ => return Ok((original_input, None)), }; diff --git a/src/dice.rs b/src/dice.rs index 3c3efe6..d0e3dd5 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -83,4 +83,3 @@ impl fmt::Display for ElementExpression { Ok(()) } } - diff --git a/src/dice/parser.rs b/src/dice/parser.rs index c282cbd..3e59557 100644 --- a/src/dice/parser.rs +++ b/src/dice/parser.rs @@ -1,10 +1,6 @@ use nom::{ - alt, - bytes::complete::{tag, take_while}, - character::complete::digit1, - complete, many0, named, - sequence::tuple, - tag, IResult, + alt, bytes::complete::tag, character::complete::digit1, complete, many0, named, + sequence::tuple, tag, IResult, }; use crate::dice::{Dice, Element, ElementExpression, SignedElement}; diff --git a/src/lib.rs b/src/lib.rs index beaf8dd..6e7d7f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ pub mod bot; +pub mod commands; pub mod dice; pub mod matrix; -pub mod roll; -pub mod commands; mod parser; - +pub mod roll; diff --git a/src/parser.rs b/src/parser.rs index e042978..43ec9a8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,7 +1,4 @@ -use nom::{ - bytes::complete::take_while, - IResult, -}; +use nom::{bytes::complete::take_while, IResult}; fn is_whitespace(input: char) -> bool { input == ' ' || input == '\n' || input == '\t' || input == '\r' diff --git a/src/roll.rs b/src/roll.rs index 7c5a033..fc26293 100644 --- a/src/roll.rs +++ b/src/roll.rs @@ -1,8 +1,7 @@ -use rand::prelude::*; -use std::ops::{Deref, DerefMut}; use crate::dice; +use rand::prelude::*; use std::fmt; -use std::string::ToString; +use std::ops::{Deref, DerefMut}; pub trait Roll { type Output; @@ -50,11 +49,13 @@ impl fmt::Display for DiceRoll { } impl Roll for dice::Dice { - type Output = DiceRoll; + type Output = DiceRoll; fn roll(&self) -> DiceRoll { let mut rng = rand::thread_rng(); - let rolls = (0..self.count).map(|_| rng.gen_range(1, self.sides + 1)).collect(); + let rolls = (0..self.count) + .map(|_| rng.gen_range(1, self.sides + 1)) + .collect(); DiceRoll(rolls) } } @@ -75,7 +76,7 @@ impl Rolled for ElementRoll { } impl Roll for dice::Element { - type Output = ElementRoll; + type Output = ElementRoll; fn roll(&self) -> ElementRoll { match self { @@ -110,7 +111,7 @@ impl Rolled for SignedElementRoll { } impl Roll for dice::SignedElement { - type Output = SignedElementRoll; + type Output = SignedElementRoll; fn roll(&self) -> SignedElementRoll { match self { @@ -153,7 +154,7 @@ impl Rolled for ElementExpressionRoll { } impl Roll for dice::ElementExpression { - type Output = ElementExpressionRoll; + type Output = ElementExpressionRoll; fn roll(&self) -> ElementExpressionRoll { ElementExpressionRoll(self.iter().map(Roll::roll).collect()) @@ -193,54 +194,86 @@ mod tests { fn dice_roll_display_test() { assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)"); assert_eq!(DiceRoll(vec![]).to_string(), "0"); - assert_eq!(DiceRoll(vec![4, 7, 2, 10]).to_string(), "23 (4 + 7 + 2 + 10)"); + assert_eq!( + DiceRoll(vec![4, 7, 2, 10]).to_string(), + "23 (4 + 7 + 2 + 10)" + ); } #[test] fn element_roll_display_test() { - assert_eq!(ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(), "8 (1 + 3 + 4)"); + assert_eq!( + ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(), + "8 (1 + 3 + 4)" + ); assert_eq!(ElementRoll::Bonus(7).to_string(), "7"); } #[test] fn signed_element_roll_display_test() { - assert_eq!(SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), "8 (1 + 3 + 4)"); - assert_eq!(SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), "-8 (1 + 3 + 4)"); - assert_eq!(SignedElementRoll::Positive(ElementRoll::Bonus(7)).to_string(), "7"); - assert_eq!(SignedElementRoll::Negative(ElementRoll::Bonus(7)).to_string(), "-7"); + assert_eq!( + SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), + "8 (1 + 3 + 4)" + ); + assert_eq!( + SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), + "-8 (1 + 3 + 4)" + ); + assert_eq!( + SignedElementRoll::Positive(ElementRoll::Bonus(7)).to_string(), + "7" + ); + assert_eq!( + SignedElementRoll::Negative(ElementRoll::Bonus(7)).to_string(), + "-7" + ); } #[test] fn element_expression_roll_display_test() { assert_eq!( - ElementExpressionRoll(vec![ - SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), - ]).to_string(), "8 (1 + 3 + 4)"); + ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice( + DiceRoll(vec![1, 3, 4]) + )),]) + .to_string(), + "8 (1 + 3 + 4)" + ); assert_eq!( - ElementExpressionRoll(vec![ - SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), - ]).to_string(), "-8 (1 + 3 + 4)"); + ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice( + DiceRoll(vec![1, 3, 4]) + )),]) + .to_string(), + "-8 (1 + 3 + 4)" + ); assert_eq!( - ElementExpressionRoll(vec![ - SignedElementRoll::Positive(ElementRoll::Bonus(7)), - ]).to_string(), "7"); + ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Bonus(7)),]) + .to_string(), + "7" + ); assert_eq!( - ElementExpressionRoll(vec![ - SignedElementRoll::Negative(ElementRoll::Bonus(7)), - ]).to_string(), "-7"); + ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Bonus(7)),]) + .to_string(), + "-7" + ); assert_eq!( ElementExpressionRoll(vec![ SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Positive(ElementRoll::Bonus(4)), SignedElementRoll::Negative(ElementRoll::Bonus(7)), - ]).to_string(), "2 (8 (1 + 3 + 4) - 3 (1 + 2) + 4 - 7)"); + ]) + .to_string(), + "2 (8 (1 + 3 + 4) - 3 (1 + 2) + 4 - 7)" + ); assert_eq!( ElementExpressionRoll(vec![ SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Negative(ElementRoll::Bonus(4)), SignedElementRoll::Positive(ElementRoll::Bonus(7)), - ]).to_string(), "-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)"); + ]) + .to_string(), + "-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)" + ); } }