fix: make clippy happy
This commit is contained in:
parent
eeb030e68a
commit
dec36867d6
5 changed files with 152 additions and 108 deletions
|
|
@ -4,18 +4,19 @@ use atoi_radix10::parse_from_str;
|
|||
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWriteExt};
|
||||
|
||||
use crate::{
|
||||
Canvas, Color, Command, Coordinate, Parser, Protocol, Responder, Response, GRID_LENGTH,
|
||||
HELP_TEXT, MEEHHEH,
|
||||
Canvas, Color, Command, Coordinate, IOProtocol, Parser, Protocol, Responder, Response,
|
||||
GRID_LENGTH, HELP_TEXT,
|
||||
};
|
||||
|
||||
pub struct TextParser {
|
||||
canvas: Canvas,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn parse_coordinate(string: &str) -> io::Result<Coordinate> {
|
||||
match parse_from_str(string) {
|
||||
Ok(coord) => return Ok(coord),
|
||||
Err(_) => return Err(Error::from(ErrorKind::InvalidInput)),
|
||||
Ok(coord) => Ok(coord),
|
||||
Err(_) => Err(Error::from(ErrorKind::InvalidInput)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,12 +29,12 @@ fn parse_color(color: &str) -> io::Result<Color> {
|
|||
_ => return Err(Error::from(ErrorKind::InvalidInput)),
|
||||
}
|
||||
}
|
||||
return Err(Error::from(ErrorKind::InvalidInput));
|
||||
Err(Error::from(ErrorKind::InvalidInput))
|
||||
}
|
||||
|
||||
impl TextParser {
|
||||
pub fn new(canvas: Canvas) -> TextParser {
|
||||
return TextParser { canvas };
|
||||
TextParser { canvas }
|
||||
}
|
||||
|
||||
fn parse_pixel(&self, line: &str) -> io::Result<Command> {
|
||||
|
|
@ -44,16 +45,14 @@ impl TextParser {
|
|||
let y_coordinate = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||
if let (Ok(horizontal), Ok(vertical)) = (x_coordinate.parse(), y_coordinate.parse()) {
|
||||
match split.next() {
|
||||
None => return Ok(Command::GetPixel(self.canvas, horizontal, vertical)),
|
||||
None => Ok(Command::GetPixel(self.canvas, horizontal, vertical)),
|
||||
Some(color) => match parse_color(color) {
|
||||
Ok(color) => {
|
||||
return Ok(Command::SetPixel(self.canvas, horizontal, vertical, color))
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
Ok(color) => Ok(Command::SetPixel(self.canvas, horizontal, vertical, color)),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return Err(Error::from(ErrorKind::InvalidInput));
|
||||
Err(Error::from(ErrorKind::InvalidInput))
|
||||
}
|
||||
}
|
||||
fn parse_canvas(&self, line: &str) -> io::Result<Command> {
|
||||
|
|
@ -62,9 +61,9 @@ impl TextParser {
|
|||
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||
let canvas = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||
if let Ok(canvas) = canvas.parse() {
|
||||
return Ok(Command::ChangeCanvas(canvas));
|
||||
Ok(Command::ChangeCanvas(canvas))
|
||||
} else {
|
||||
return Err(Error::from(ErrorKind::InvalidInput));
|
||||
Err(Error::from(ErrorKind::InvalidInput))
|
||||
}
|
||||
}
|
||||
fn parse_protocol(&self, line: &str) -> io::Result<Command> {
|
||||
|
|
@ -73,9 +72,9 @@ impl TextParser {
|
|||
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||
let protocol = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||
match protocol {
|
||||
"binary" => return Ok(Command::ChangeProtocol(Protocol::Binary)),
|
||||
"text" => return Ok(Command::ChangeProtocol(Protocol::Text)),
|
||||
_ => return Err(Error::from(ErrorKind::InvalidInput)),
|
||||
"binary" => Ok(Command::ChangeProtocol(Protocol::Binary)),
|
||||
"text" => Ok(Command::ChangeProtocol(Protocol::Text)),
|
||||
_ => Err(Error::from(ErrorKind::InvalidInput)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -83,7 +82,7 @@ impl TextParser {
|
|||
impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for TextParser {
|
||||
async fn parse(&self, reader: &mut R) -> io::Result<Command> {
|
||||
let mut line = "".to_string();
|
||||
if let Ok(_) = reader.read_line(&mut line).await {
|
||||
if reader.read_line(&mut line).await.is_ok() {
|
||||
if line.starts_with("HELP") {
|
||||
return Ok(Command::Help);
|
||||
} else if line.starts_with("SIZE") {
|
||||
|
|
@ -96,17 +95,17 @@ impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for TextP
|
|||
return self.parse_protocol(&line);
|
||||
}
|
||||
}
|
||||
return Err(Error::from(ErrorKind::InvalidInput));
|
||||
Err(Error::from(ErrorKind::InvalidInput))
|
||||
}
|
||||
}
|
||||
|
||||
impl MEEHHEH for TextParser {
|
||||
impl IOProtocol for TextParser {
|
||||
fn change_canvas(&mut self, canvas: Canvas) -> io::Result<()> {
|
||||
if (canvas as usize) < GRID_LENGTH {
|
||||
self.canvas = canvas;
|
||||
return Ok(());
|
||||
Ok(())
|
||||
} else {
|
||||
return Err(Error::from(ErrorKind::InvalidInput));
|
||||
Err(Error::from(ErrorKind::InvalidInput))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -132,9 +131,7 @@ impl<W: AsyncWriteExt + std::marker::Unpin> Responder<W> for TextParser {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::grid::FlutGrid;
|
||||
use tokio::io::BufReader;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_help_parse() {
|
||||
|
|
@ -142,7 +139,7 @@ mod tests {
|
|||
let reader = tokio_test::io::Builder::new().read(b"HELP\n").build();
|
||||
let mut bufreader = BufReader::new(reader);
|
||||
let thingy = parser.parse(&mut bufreader).await;
|
||||
assert_eq!(thingy.unwrap(), Command::Help)
|
||||
assert_eq!(thingy.unwrap(), Command::Help);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -151,7 +148,7 @@ mod tests {
|
|||
let reader = tokio_test::io::Builder::new().read(b"SIZE\n").build();
|
||||
let mut bufreader = BufReader::new(reader);
|
||||
let thingy = parser.parse(&mut bufreader).await;
|
||||
assert_eq!(thingy.unwrap(), Command::Size(0))
|
||||
assert_eq!(thingy.unwrap(), Command::Size(0));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -160,7 +157,7 @@ mod tests {
|
|||
let reader = tokio_test::io::Builder::new().read(b"CANVAS 12\n").build();
|
||||
let mut bufreader = BufReader::new(reader);
|
||||
let thingy = parser.parse(&mut bufreader).await;
|
||||
assert_eq!(thingy.unwrap(), Command::ChangeCanvas(12))
|
||||
assert_eq!(thingy.unwrap(), Command::ChangeCanvas(12));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -174,7 +171,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
thingy.unwrap(),
|
||||
Command::SetPixel(0, 28283, 29991, Color::W8(0x81))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -188,7 +185,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
thingy.unwrap(),
|
||||
Command::SetPixel(0, 28283, 29991, Color::RGB24(0x88, 0x00, 0xff))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -202,7 +199,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
thingy.unwrap(),
|
||||
Command::SetPixel(0, 28283, 29991, Color::RGBA32(0x88, 0x00, 0xff, 0x28))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -213,7 +210,7 @@ mod tests {
|
|||
.build();
|
||||
let mut bufreader = BufReader::new(reader);
|
||||
let thingy = parser.parse(&mut bufreader).await;
|
||||
assert_eq!(thingy.unwrap(), Command::GetPixel(0, 28283, 29991))
|
||||
assert_eq!(thingy.unwrap(), Command::GetPixel(0, 28283, 29991));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue