cargo fix and fmt

This commit is contained in:
Taylor C. Richberger 2020-04-21 00:09:43 -06:00
parent 198f0bd43f
commit e7458c12ad
9 changed files with 85 additions and 67 deletions

View File

@ -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 axfive_matrix_dicebot::commands::parse_command;
use std::error::Error;
use std::string::ToString;
fn main() -> Result<(), String> { fn main() -> Result<(), String> {
let command = std::env::args().skip(1).collect::<Vec<String>>().join(" "); let command = std::env::args().skip(1).collect::<Vec<String>>().join(" ");

View File

@ -1,4 +1,4 @@
use crate::matrix::{SyncCommand, Event, RoomEvent, MessageContent}; use crate::matrix::{Event, MessageContent, RoomEvent, SyncCommand};
use reqwest::{Client, Url}; use reqwest::{Client, Url};
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use std::fs; use std::fs;
@ -118,19 +118,21 @@ impl DiceBot {
let sync: SyncCommand = serde_json::from_str(&body).unwrap(); let sync: SyncCommand = serde_json::from_str(&body).unwrap();
// First join invited rooms // First join invited rooms
for room in sync.rooms.invite.keys() { for room in sync.rooms.invite.keys() {
let mut join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]); let join_url = self.url(&format!("/_matrix/client/r0/rooms/{}/join", room), &[]);
self.client.post(join_url) self.client
.post(join_url)
.header("user-agent", USER_AGENT) .header("user-agent", USER_AGENT)
.send() .send()
.await?; .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 { for event in &room.timeline.events {
if let Event::Room(RoomEvent{ if let Event::Room(RoomEvent {
content: MessageContent::Text(message), content: MessageContent::Text(message),
.. ..
}) = event { }) = event
{
// TODO: create command parser (maybe command.rs) to parse !roll/!r commands // TODO: create command parser (maybe command.rs) to parse !roll/!r commands
// and reply // and reply
println!("Body: {}", message.body()); println!("Body: {}", message.body());

View File

@ -1,7 +1,6 @@
use crate::dice::ElementExpression; use crate::dice::ElementExpression;
use crate::roll::{Roll, Rolled}; use crate::roll::Roll;
use nom::error::ErrorKind;
use nom::IResult;
pub mod parser; pub mod parser;
pub struct Execution { pub struct Execution {
@ -29,11 +28,11 @@ impl Command for RollCommand {
fn execute(&self) -> Execution { fn execute(&self) -> Execution {
let roll = self.0.roll(); let roll = self.0.roll();
let plain = format!("Dice: {}\nResult: {}", self.0, roll); let plain = format!("Dice: {}\nResult: {}", self.0, roll);
let html = format!("<strong>Dice:</strong> {}<br><strong>Result</strong>: {}", self.0, roll); let html = format!(
Execution { "<strong>Dice:</strong> {}<br><strong>Result</strong>: {}",
plain, self.0, roll
html, );
} Execution { plain, html }
} }
} }

View File

@ -1,18 +1,15 @@
use nom::{ use nom::{
tuple,
switch,
take_while,
bytes::complete::{tag, take_while}, bytes::complete::{tag, take_while},
character::complete::digit1, character::complete::digit1,
character::is_alphabetic, character::is_alphabetic,
complete, many0, named, complete, many0, named,
sequence::tuple, sequence::tuple,
tag, IResult, switch, tag, take_while, tuple, IResult,
}; };
use crate::parser::eat_whitespace;
use crate::commands::{Command, RollCommand}; use crate::commands::{Command, RollCommand};
use crate::dice::parser::parse_element_expression; use crate::dice::parser::parse_element_expression;
use crate::parser::eat_whitespace;
// Parse a roll expression. // Parse a roll expression.
fn parse_roll(input: &str) -> IResult<&str, RollCommand> { fn parse_roll(input: &str) -> IResult<&str, RollCommand> {
@ -29,14 +26,14 @@ pub fn parse_command(original_input: &str) -> IResult<&str, Option<Box<dyn Comma
let (input, command) = match command(input) { let (input, command) = match command(input) {
// Strip the exclamation mark // Strip the exclamation mark
Ok((input, (_, result))) => (input, result), Ok((input, (_, result))) => (input, result),
Err(e) => return Ok((original_input, None)), Err(_e) => return Ok((original_input, None)),
}; };
let (input, command) = match command { let (input, command) = match command {
"r" | "roll" => { "r" | "roll" => {
let (input, command) = parse_roll(input)?; let (input, command) = parse_roll(input)?;
let command: Box<dyn Command> = Box::new(command); let command: Box<dyn Command> = Box::new(command);
(input, command) (input, command)
}, }
// No recognized command, ignore this. // No recognized command, ignore this.
_ => return Ok((original_input, None)), _ => return Ok((original_input, None)),
}; };

View File

@ -83,4 +83,3 @@ impl fmt::Display for ElementExpression {
Ok(()) Ok(())
} }
} }

View File

@ -1,10 +1,6 @@
use nom::{ use nom::{
alt, alt, bytes::complete::tag, character::complete::digit1, complete, many0, named,
bytes::complete::{tag, take_while}, sequence::tuple, tag, IResult,
character::complete::digit1,
complete, many0, named,
sequence::tuple,
tag, IResult,
}; };
use crate::dice::{Dice, Element, ElementExpression, SignedElement}; use crate::dice::{Dice, Element, ElementExpression, SignedElement};

View File

@ -1,7 +1,6 @@
pub mod bot; pub mod bot;
pub mod commands;
pub mod dice; pub mod dice;
pub mod matrix; pub mod matrix;
pub mod roll;
pub mod commands;
mod parser; mod parser;
pub mod roll;

View File

@ -1,7 +1,4 @@
use nom::{ use nom::{bytes::complete::take_while, IResult};
bytes::complete::take_while,
IResult,
};
fn is_whitespace(input: char) -> bool { fn is_whitespace(input: char) -> bool {
input == ' ' || input == '\n' || input == '\t' || input == '\r' input == ' ' || input == '\n' || input == '\t' || input == '\r'

View File

@ -1,8 +1,7 @@
use rand::prelude::*;
use std::ops::{Deref, DerefMut};
use crate::dice; use crate::dice;
use rand::prelude::*;
use std::fmt; use std::fmt;
use std::string::ToString; use std::ops::{Deref, DerefMut};
pub trait Roll { pub trait Roll {
type Output; type Output;
@ -50,11 +49,13 @@ impl fmt::Display for DiceRoll {
} }
impl Roll for dice::Dice { impl Roll for dice::Dice {
type Output = DiceRoll; type Output = DiceRoll;
fn roll(&self) -> DiceRoll { fn roll(&self) -> DiceRoll {
let mut rng = rand::thread_rng(); 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) DiceRoll(rolls)
} }
} }
@ -75,7 +76,7 @@ impl Rolled for ElementRoll {
} }
impl Roll for dice::Element { impl Roll for dice::Element {
type Output = ElementRoll; type Output = ElementRoll;
fn roll(&self) -> ElementRoll { fn roll(&self) -> ElementRoll {
match self { match self {
@ -110,7 +111,7 @@ impl Rolled for SignedElementRoll {
} }
impl Roll for dice::SignedElement { impl Roll for dice::SignedElement {
type Output = SignedElementRoll; type Output = SignedElementRoll;
fn roll(&self) -> SignedElementRoll { fn roll(&self) -> SignedElementRoll {
match self { match self {
@ -153,7 +154,7 @@ impl Rolled for ElementExpressionRoll {
} }
impl Roll for dice::ElementExpression { impl Roll for dice::ElementExpression {
type Output = ElementExpressionRoll; type Output = ElementExpressionRoll;
fn roll(&self) -> ElementExpressionRoll { fn roll(&self) -> ElementExpressionRoll {
ElementExpressionRoll(self.iter().map(Roll::roll).collect()) ElementExpressionRoll(self.iter().map(Roll::roll).collect())
@ -193,54 +194,86 @@ mod tests {
fn dice_roll_display_test() { fn dice_roll_display_test() {
assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)"); assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)");
assert_eq!(DiceRoll(vec![]).to_string(), "0"); 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] #[test]
fn element_roll_display_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"); assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
} }
#[test] #[test]
fn signed_element_roll_display_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!(
assert_eq!(SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), "-8 (1 + 3 + 4)"); SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(),
assert_eq!(SignedElementRoll::Positive(ElementRoll::Bonus(7)).to_string(), "7"); "8 (1 + 3 + 4)"
assert_eq!(SignedElementRoll::Negative(ElementRoll::Bonus(7)).to_string(), "-7"); );
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] #[test]
fn element_expression_roll_display_test() { fn element_expression_roll_display_test() {
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), DiceRoll(vec![1, 3, 4])
]).to_string(), "8 (1 + 3 + 4)"); )),])
.to_string(),
"8 (1 + 3 + 4)"
);
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), DiceRoll(vec![1, 3, 4])
]).to_string(), "-8 (1 + 3 + 4)"); )),])
.to_string(),
"-8 (1 + 3 + 4)"
);
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Bonus(7)),])
SignedElementRoll::Positive(ElementRoll::Bonus(7)), .to_string(),
]).to_string(), "7"); "7"
);
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Bonus(7)),])
SignedElementRoll::Negative(ElementRoll::Bonus(7)), .to_string(),
]).to_string(), "-7"); "-7"
);
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
SignedElementRoll::Positive(ElementRoll::Bonus(4)), SignedElementRoll::Positive(ElementRoll::Bonus(4)),
SignedElementRoll::Negative(ElementRoll::Bonus(7)), 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!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
SignedElementRoll::Negative(ElementRoll::Bonus(4)), SignedElementRoll::Negative(ElementRoll::Bonus(4)),
SignedElementRoll::Positive(ElementRoll::Bonus(7)), 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)"
);
} }
} }