fix: make pedantic clippy happy
This commit is contained in:
parent
f6f7623458
commit
be383ec768
4 changed files with 100 additions and 100 deletions
|
|
@ -23,7 +23,7 @@ impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for Binar
|
||||||
async fn parse(&self, reader: &mut R) -> io::Result<Command> {
|
async fn parse(&self, reader: &mut R) -> io::Result<Command> {
|
||||||
let fst = reader.read_u8().await;
|
let fst = reader.read_u8().await;
|
||||||
match fst {
|
match fst {
|
||||||
Ok(i) => match i {
|
Ok(command) => match command {
|
||||||
HELP_BIN => Ok(Command::Help),
|
HELP_BIN => Ok(Command::Help),
|
||||||
SIZE_BIN => {
|
SIZE_BIN => {
|
||||||
let canvas = reader.read_u8().await?;
|
let canvas = reader.read_u8().await?;
|
||||||
|
|
@ -31,43 +31,58 @@ impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for Binar
|
||||||
}
|
}
|
||||||
GET_PX_BIN => {
|
GET_PX_BIN => {
|
||||||
let canvas = reader.read_u8().await?;
|
let canvas = reader.read_u8().await?;
|
||||||
let x = reader.read_u16_le().await?;
|
let horizontal = reader.read_u16_le().await?;
|
||||||
let y = reader.read_u16_le().await?;
|
let vertical = reader.read_u16_le().await?;
|
||||||
Ok(Command::GetPixel(canvas, x, y))
|
Ok(Command::GetPixel(canvas, horizontal, vertical))
|
||||||
}
|
}
|
||||||
SET_PX_W_BIN => {
|
SET_PX_W_BIN => {
|
||||||
let canvas = reader.read_u8().await?;
|
let canvas = reader.read_u8().await?;
|
||||||
let x = reader.read_u16_le().await?;
|
let horizontal = reader.read_u16_le().await?;
|
||||||
let y = reader.read_u16_le().await?;
|
let vertical = reader.read_u16_le().await?;
|
||||||
let w = reader.read_u8().await?;
|
let white = reader.read_u8().await?;
|
||||||
Ok(Command::SetPixel(canvas, x, y, Color::W8(w)))
|
Ok(Command::SetPixel(
|
||||||
|
canvas,
|
||||||
|
horizontal,
|
||||||
|
vertical,
|
||||||
|
Color::W8(white),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
SET_PX_RGB_BIN => {
|
SET_PX_RGB_BIN => {
|
||||||
let canvas = reader.read_u8().await?;
|
let canvas = reader.read_u8().await?;
|
||||||
let x = reader.read_u16_le().await?;
|
let horizontal = reader.read_u16_le().await?;
|
||||||
let y = reader.read_u16_le().await?;
|
let vertical = reader.read_u16_le().await?;
|
||||||
let r = reader.read_u8().await?;
|
let red = reader.read_u8().await?;
|
||||||
let g = reader.read_u8().await?;
|
let green = reader.read_u8().await?;
|
||||||
let b = reader.read_u8().await?;
|
let blue = reader.read_u8().await?;
|
||||||
Ok(Command::SetPixel(canvas, x, y, Color::RGB24(r, g, b)))
|
Ok(Command::SetPixel(
|
||||||
|
canvas,
|
||||||
|
horizontal,
|
||||||
|
vertical,
|
||||||
|
Color::RGB24(red, green, blue),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
SET_PX_RGBA_BIN => {
|
SET_PX_RGBA_BIN => {
|
||||||
let canvas = reader.read_u8().await?;
|
let canvas = reader.read_u8().await?;
|
||||||
let x = reader.read_u16_le().await?;
|
let horizontal = reader.read_u16_le().await?;
|
||||||
let y = reader.read_u16_le().await?;
|
let vertical = reader.read_u16_le().await?;
|
||||||
let r = reader.read_u8().await?;
|
let red = reader.read_u8().await?;
|
||||||
let g = reader.read_u8().await?;
|
let green = reader.read_u8().await?;
|
||||||
let b = reader.read_u8().await?;
|
let blue = reader.read_u8().await?;
|
||||||
let a = reader.read_u8().await?;
|
let alpha = reader.read_u8().await?;
|
||||||
Ok(Command::SetPixel(canvas, x, y, Color::RGBA32(r, g, b, a)))
|
Ok(Command::SetPixel(
|
||||||
|
canvas,
|
||||||
|
horizontal,
|
||||||
|
vertical,
|
||||||
|
Color::RGBA32(red, green, blue, alpha),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("received illegal command: {}", i);
|
eprintln!("received illegal command: {command}");
|
||||||
Err(Error::from(ErrorKind::InvalidInput))
|
Err(Error::from(ErrorKind::InvalidInput))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}", err);
|
eprintln!("{err}");
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -85,11 +100,10 @@ impl<W: AsyncWriteExt + std::marker::Unpin> Responder<W> for BinaryParser {
|
||||||
let help_text = format!(
|
let help_text = format!(
|
||||||
"
|
"
|
||||||
You found the binary protocol help text
|
You found the binary protocol help text
|
||||||
you can get this by sending ({:02X}) to the server
|
you can get this by sending ({HELP_BIN:02X}) to the server
|
||||||
To get the size of a canvas, send ({:02X}) (u8 canvas) to the server
|
To get the size of a canvas, send ({SIZE_BIN:02X}) (u8 canvas) to the server
|
||||||
To set a pixel using RGB, use ({:02X}) (u8 canvas) (x as u16_le) (y as u16_le) (u8 r) (u8 g) (u8 b)
|
To set a pixel using RGB, use ({SET_PX_RGB_BIN:02X}) (u8 canvas) (x as u16_le) (y as u16_le) (u8 r) (u8 g) (u8 b)
|
||||||
",
|
",
|
||||||
HELP_BIN, SIZE_BIN, SET_PX_RGB_BIN
|
|
||||||
);
|
);
|
||||||
match response {
|
match response {
|
||||||
Response::Help => writer.write_all(help_text.as_bytes()).await,
|
Response::Help => writer.write_all(help_text.as_bytes()).await,
|
||||||
|
|
@ -103,6 +117,7 @@ To set a pixel using RGB, use ({:02X}) (u8 canvas) (x as u16_le) (y as u16_le) (
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
#[allow(clippy::needless_return)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use tokio::io::BufReader;
|
use tokio::io::BufReader;
|
||||||
|
|
@ -113,7 +128,7 @@ mod tests {
|
||||||
let reader = tokio_test::io::Builder::new().read(&[HELP_BIN]).build();
|
let reader = tokio_test::io::Builder::new().read(&[HELP_BIN]).build();
|
||||||
let mut bufreader = BufReader::new(reader);
|
let mut bufreader = BufReader::new(reader);
|
||||||
let thingy = parser.parse(&mut bufreader).await;
|
let thingy = parser.parse(&mut bufreader).await;
|
||||||
assert_eq!(thingy.unwrap(), Command::Help)
|
assert_eq!(thingy.unwrap(), Command::Help);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -122,7 +137,7 @@ mod tests {
|
||||||
let reader = tokio_test::io::Builder::new().read(&[SIZE_BIN, 3]).build();
|
let reader = tokio_test::io::Builder::new().read(&[SIZE_BIN, 3]).build();
|
||||||
let mut bufreader = BufReader::new(reader);
|
let mut bufreader = BufReader::new(reader);
|
||||||
let thingy = parser.parse(&mut bufreader).await;
|
let thingy = parser.parse(&mut bufreader).await;
|
||||||
assert_eq!(thingy.unwrap(), Command::Size(3))
|
assert_eq!(thingy.unwrap(), Command::Size(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -136,7 +151,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
thingy.unwrap(),
|
thingy.unwrap(),
|
||||||
Command::SetPixel(1, 0x4269, 0x6942, Color::W8(0x82))
|
Command::SetPixel(1, 0x4269, 0x6942, Color::W8(0x82))
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -160,7 +175,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
thingy.unwrap(),
|
thingy.unwrap(),
|
||||||
Command::SetPixel(1, 0x4269, 0x6942, Color::RGB24(0x82, 0x00, 0xff))
|
Command::SetPixel(1, 0x4269, 0x6942, Color::RGB24(0x82, 0x00, 0xff))
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -185,7 +200,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
thingy.unwrap(),
|
thingy.unwrap(),
|
||||||
Command::SetPixel(1, 0x4269, 0x6942, Color::RGBA32(0x82, 0x00, 0xff, 0xa0))
|
Command::SetPixel(1, 0x4269, 0x6942, Color::RGBA32(0x82, 0x00, 0xff, 0xa0))
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -196,7 +211,7 @@ mod tests {
|
||||||
.build();
|
.build();
|
||||||
let mut bufreader = BufReader::new(reader);
|
let mut bufreader = BufReader::new(reader);
|
||||||
let thingy = parser.parse(&mut bufreader).await;
|
let thingy = parser.parse(&mut bufreader).await;
|
||||||
assert_eq!(thingy.unwrap(), Command::GetPixel(3, 0x4269, 0x6942))
|
assert_eq!(thingy.unwrap(), Command::GetPixel(3, 0x4269, 0x6942));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
|
||||||
45
src/grid.rs
45
src/grid.rs
|
|
@ -9,19 +9,19 @@ pub trait Grid<I, V> {
|
||||||
fn set(&self, x: I, y: I, value: V);
|
fn set(&self, x: I, y: I, value: V);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FlutGrid<T> {
|
pub struct Flut<T> {
|
||||||
size_x: usize,
|
size_x: usize,
|
||||||
size_y: usize,
|
size_y: usize,
|
||||||
cells: SyncUnsafeCell<Vec<T>>,
|
cells: SyncUnsafeCell<Vec<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> FlutGrid<T> {
|
impl<T: Clone> Flut<T> {
|
||||||
pub fn init(size_x: usize, size_y: usize, value: T) -> FlutGrid<T> {
|
pub fn init(size_x: usize, size_y: usize, value: T) -> Flut<T> {
|
||||||
let mut vec = Vec::with_capacity(size_x * size_y);
|
let mut vec = Vec::with_capacity(size_x * size_y);
|
||||||
for _ in 0..(size_x * size_y) {
|
for _ in 0..(size_x * size_y) {
|
||||||
vec.push(value.clone());
|
vec.push(value.clone());
|
||||||
}
|
}
|
||||||
FlutGrid {
|
Flut {
|
||||||
size_x,
|
size_x,
|
||||||
size_y,
|
size_y,
|
||||||
cells: vec.into(),
|
cells: vec.into(),
|
||||||
|
|
@ -33,7 +33,7 @@ impl<T: Clone> FlutGrid<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> FlutGrid<T> {
|
impl<T> Flut<T> {
|
||||||
fn index(&self, x: Coordinate, y: Coordinate) -> Option<usize> {
|
fn index(&self, x: Coordinate, y: Coordinate) -> Option<usize> {
|
||||||
let x = x as usize;
|
let x = x as usize;
|
||||||
let y = y as usize;
|
let y = y as usize;
|
||||||
|
|
@ -44,7 +44,7 @@ impl<T> FlutGrid<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Grid<Coordinate, T> for FlutGrid<T> {
|
impl<T> Grid<Coordinate, T> for Flut<T> {
|
||||||
fn get(&self, x: Coordinate, y: Coordinate) -> Option<&T> {
|
fn get(&self, x: Coordinate, y: Coordinate) -> Option<&T> {
|
||||||
self.index(x, y)
|
self.index(x, y)
|
||||||
.map(|idx| unsafe { &(*self.cells.get())[idx] })
|
.map(|idx| unsafe { &(*self.cells.get())[idx] })
|
||||||
|
|
@ -64,21 +64,22 @@ impl<T> Grid<Coordinate, T> for FlutGrid<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
#[allow(clippy::needless_return)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::grid::FlutGrid;
|
use crate::grid::Flut;
|
||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_init_values() {
|
async fn test_grid_init_values() {
|
||||||
let grid = FlutGrid::init(3, 3, 0);
|
let grid = Flut::init(3, 3, 0);
|
||||||
|
|
||||||
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 0, 0, 0, 0, 0])
|
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_init_size() {
|
async fn test_grid_init_size() {
|
||||||
let grid = FlutGrid::init(800, 600, 0);
|
let grid = Flut::init(800, 600, 0);
|
||||||
|
|
||||||
assert_eq!(grid.size_x, 800);
|
assert_eq!(grid.size_x, 800);
|
||||||
assert_eq!(grid.size_y, 600);
|
assert_eq!(grid.size_y, 600);
|
||||||
|
|
@ -86,30 +87,30 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_set() {
|
async fn test_grid_set() {
|
||||||
let grid = FlutGrid::init(3, 3, 0);
|
let grid = Flut::init(3, 3, 0);
|
||||||
grid.set(1, 1, 255);
|
grid.set(1, 1, 255);
|
||||||
grid.set(2, 1, 256);
|
grid.set(2, 1, 256);
|
||||||
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 256, 0, 0, 0])
|
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 256, 0, 0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_set_out_of_range() {
|
async fn test_grid_set_out_of_range() {
|
||||||
let grid = FlutGrid::init(3, 3, 0);
|
let grid = Flut::init(3, 3, 0);
|
||||||
grid.set(1, 1, 255);
|
grid.set(1, 1, 255);
|
||||||
grid.set(3, 1, 256);
|
grid.set(3, 1, 256);
|
||||||
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 0, 0, 0, 0])
|
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 0, 0, 0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_get() {
|
async fn test_grid_get() {
|
||||||
let grid = FlutGrid::init(3, 3, 0);
|
let grid = Flut::init(3, 3, 0);
|
||||||
grid.set(1, 2, 222);
|
grid.set(1, 2, 222);
|
||||||
assert_eq!(grid.get(1, 2), Some(&222));
|
assert_eq!(grid.get(1, 2), Some(&222));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_grid_get_out_of_range() {
|
async fn test_grid_get_out_of_range() {
|
||||||
let grid = FlutGrid::init(3, 3, 0);
|
let grid = Flut::init(3, 3, 0);
|
||||||
grid.set(3, 1, 256);
|
grid.set(3, 1, 256);
|
||||||
assert_eq!(grid.get(3, 1), None);
|
assert_eq!(grid.get(3, 1), None);
|
||||||
assert_eq!(grid.get(1, 2), Some(&0));
|
assert_eq!(grid.get(1, 2), Some(&0));
|
||||||
|
|
@ -117,27 +118,27 @@ mod tests {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_init(b: &mut Bencher) {
|
fn bench_init(b: &mut Bencher) {
|
||||||
b.iter(|| FlutGrid::init(800, 600, 0 as u32))
|
b.iter(|| Flut::init(800, 600, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_set(b: &mut Bencher) {
|
fn bench_set(b: &mut Bencher) {
|
||||||
let grid = FlutGrid::init(800, 600, 0 as u32);
|
let grid = Flut::init(800, 600, 0);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let x = test::black_box(293);
|
let x = test::black_box(293);
|
||||||
let y = test::black_box(222);
|
let y = test::black_box(222);
|
||||||
let color = test::black_box(293_923);
|
let color = test::black_box(0x29_39_23);
|
||||||
grid.set(x, y, color);
|
grid.set(x, y, color);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_get(b: &mut Bencher) {
|
fn bench_get(b: &mut Bencher) {
|
||||||
let grid = FlutGrid::init(800, 600, 0 as u32);
|
let grid = Flut::init(800, 600, 0);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let x = test::black_box(293);
|
let x = test::black_box(293);
|
||||||
let y = test::black_box(222);
|
let y = test::black_box(222);
|
||||||
grid.get(x, y)
|
grid.get(x, y)
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
src/main.rs
55
src/main.rs
|
|
@ -12,7 +12,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use binary_protocol::BinaryParser;
|
use binary_protocol::BinaryParser;
|
||||||
use grid::{FlutGrid, Grid};
|
use grid::{Flut, Grid};
|
||||||
use text_protocol::TextParser;
|
use text_protocol::TextParser;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
||||||
|
|
@ -38,19 +38,19 @@ type Canvas = u8;
|
||||||
type Coordinate = u16;
|
type Coordinate = u16;
|
||||||
|
|
||||||
fn set_pixel_rgba(
|
fn set_pixel_rgba(
|
||||||
grids: &[grid::FlutGrid<u32>],
|
grids: &[grid::Flut<u32>],
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
x: Coordinate,
|
x: Coordinate,
|
||||||
y: Coordinate,
|
y: Coordinate,
|
||||||
rgb: u32,
|
rgb: u32,
|
||||||
) {
|
) {
|
||||||
if let Some(grid) = grids.get(canvas as usize) {
|
if let Some(grid) = grids.get(canvas as usize) {
|
||||||
grid.set(x, y, rgb)
|
grid.set(x, y, rgb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pixel(
|
fn get_pixel(
|
||||||
grids: &[grid::FlutGrid<u32>],
|
grids: &[grid::Flut<u32>],
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
x: Coordinate,
|
x: Coordinate,
|
||||||
y: Coordinate,
|
y: Coordinate,
|
||||||
|
|
@ -154,7 +154,7 @@ where
|
||||||
{
|
{
|
||||||
reader: BufReader<R>,
|
reader: BufReader<R>,
|
||||||
writer: BufWriter<W>,
|
writer: BufWriter<W>,
|
||||||
grids: Arc<[FlutGrid<u32>]>,
|
grids: Arc<[Flut<u32>]>,
|
||||||
parser: ParserTypes,
|
parser: ParserTypes,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,36 +196,30 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pixel_command(
|
fn set_pixel_command(&mut self, canvas: Canvas, x: Coordinate, y: Coordinate, color: &Color) {
|
||||||
&mut self,
|
|
||||||
canvas: Canvas,
|
|
||||||
x: Coordinate,
|
|
||||||
y: Coordinate,
|
|
||||||
color: Color,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
let c: u32 = match color {
|
let c: u32 = match color {
|
||||||
Color::RGB24(r, g, b) => u32::from_be_bytes([r, g, b, 0xff]),
|
Color::RGB24(red, green, blue) => u32::from_be_bytes([*red, *green, *blue, 0xff]),
|
||||||
Color::RGBA32(r, g, b, a) => u32::from_be_bytes([r, g, b, a]),
|
Color::RGBA32(red, green, blue, alpha) => {
|
||||||
Color::W8(w) => u32::from_be_bytes([w, w, w, 0xff]),
|
u32::from_be_bytes([*red, *green, *blue, *alpha])
|
||||||
|
}
|
||||||
|
Color::W8(white) => u32::from_be_bytes([*white, *white, *white, 0xff]),
|
||||||
};
|
};
|
||||||
set_pixel_rgba(self.grids.as_ref(), canvas, x, y, c);
|
set_pixel_rgba(self.grids.as_ref(), canvas, x, y, c);
|
||||||
increment_counter();
|
increment_counter();
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_canvas_command(&mut self, canvas: Canvas) -> io::Result<()> {
|
fn change_canvas_command(&mut self, canvas: Canvas) -> io::Result<()> {
|
||||||
match_parser!(parser: self.parser => parser.change_canvas(canvas))
|
match_parser!(parser: self.parser => parser.change_canvas(canvas))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_protocol(&mut self, protocol: Protocol) -> io::Result<()> {
|
fn change_protocol(&mut self, protocol: &Protocol) {
|
||||||
match protocol {
|
match protocol {
|
||||||
Protocol::Text => self.parser = ParserTypes::TextParser(TextParser::new(0)),
|
Protocol::Text => self.parser = ParserTypes::TextParser(TextParser::new(0)),
|
||||||
Protocol::Binary => self.parser = ParserTypes::BinaryParser(BinaryParser::new()),
|
Protocol::Binary => self.parser = ParserTypes::BinaryParser(BinaryParser::new()),
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(reader: R, writer: W, grids: Arc<[grid::FlutGrid<u32>]>) -> FlutClient<R, W> {
|
pub fn new(reader: R, writer: W, grids: Arc<[grid::Flut<u32>]>) -> FlutClient<R, W> {
|
||||||
FlutClient {
|
FlutClient {
|
||||||
reader: BufReader::new(reader),
|
reader: BufReader::new(reader),
|
||||||
writer: BufWriter::new(writer),
|
writer: BufWriter::new(writer),
|
||||||
|
|
@ -246,10 +240,10 @@ where
|
||||||
Ok(Command::Size(canvas)) => self.size_command(canvas).await?,
|
Ok(Command::Size(canvas)) => self.size_command(canvas).await?,
|
||||||
Ok(Command::GetPixel(canvas, x, y)) => self.get_pixel_command(canvas, x, y).await?,
|
Ok(Command::GetPixel(canvas, x, y)) => self.get_pixel_command(canvas, x, y).await?,
|
||||||
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
||||||
self.set_pixel_command(canvas, x, y, color)?;
|
self.set_pixel_command(canvas, x, y, &color);
|
||||||
}
|
}
|
||||||
Ok(Command::ChangeCanvas(canvas)) => self.change_canvas_command(canvas)?,
|
Ok(Command::ChangeCanvas(canvas)) => self.change_canvas_command(canvas)?,
|
||||||
Ok(Command::ChangeProtocol(protocol)) => self.change_protocol(protocol)?,
|
Ok(Command::ChangeProtocol(protocol)) => self.change_protocol(&protocol),
|
||||||
|
|
||||||
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
@ -262,10 +256,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_flut(
|
async fn handle_flut(flut_listener: TcpListener, grids: Arc<[grid::Flut<u32>]>) -> io::Result<()> {
|
||||||
flut_listener: TcpListener,
|
|
||||||
grids: Arc<[grid::FlutGrid<u32>]>,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
let mut handles = Vec::new();
|
let mut handles = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
let (mut socket, _) = flut_listener.accept().await?;
|
let (mut socket, _) = flut_listener.accept().await?;
|
||||||
|
|
@ -286,15 +277,11 @@ async fn handle_flut(
|
||||||
#[allow(clippy::needless_return)]
|
#[allow(clippy::needless_return)]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
println!("created grids");
|
println!("created grids");
|
||||||
let grids: Arc<[FlutGrid<u32>; GRID_LENGTH]> =
|
let grids: Arc<[Flut<u32>; GRID_LENGTH]> = [grid::Flut::init(800, 600, 0xff_00_ff_ff)].into();
|
||||||
[grid::FlutGrid::init(800, 600, 0xff00ffff)].into();
|
|
||||||
|
|
||||||
let flut_listener = match TcpListener::bind(HOST).await {
|
let Ok(flut_listener) = TcpListener::bind(HOST).await else {
|
||||||
Ok(listener) => listener,
|
eprintln!("Was unable to bind to {HOST}, please check if a different process is bound");
|
||||||
Err(_) => {
|
return;
|
||||||
eprintln!("Was unable to bind to {HOST}, please check if a different process is bound");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
println!("bound flut listener");
|
println!("bound flut listener");
|
||||||
|
|
||||||
|
|
@ -306,7 +293,7 @@ async fn main() {
|
||||||
];
|
];
|
||||||
|
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
println!("joined handle had result {:?}", handle.await)
|
println!("joined handle had result {:?}", handle.await);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ impl TextParser {
|
||||||
Err(Error::from(ErrorKind::InvalidInput))
|
Err(Error::from(ErrorKind::InvalidInput))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn parse_canvas(&self, line: &str) -> io::Result<Command> {
|
fn parse_canvas(line: &str) -> io::Result<Command> {
|
||||||
let mut split = line.trim().split(' ');
|
let mut split = line.trim().split(' ');
|
||||||
|
|
||||||
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||||
|
|
@ -66,7 +66,7 @@ impl TextParser {
|
||||||
Err(Error::from(ErrorKind::InvalidInput))
|
Err(Error::from(ErrorKind::InvalidInput))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn parse_protocol(&self, line: &str) -> io::Result<Command> {
|
fn parse_protocol(line: &str) -> io::Result<Command> {
|
||||||
let mut split = line.trim().split(' ');
|
let mut split = line.trim().split(' ');
|
||||||
|
|
||||||
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
let _command = split.next().ok_or(Error::from(ErrorKind::InvalidInput))?;
|
||||||
|
|
@ -81,7 +81,7 @@ impl TextParser {
|
||||||
|
|
||||||
impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for TextParser {
|
impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for TextParser {
|
||||||
async fn parse(&self, reader: &mut R) -> io::Result<Command> {
|
async fn parse(&self, reader: &mut R) -> io::Result<Command> {
|
||||||
let mut line = "".to_string();
|
let mut line = String::new();
|
||||||
if reader.read_line(&mut line).await.is_ok() {
|
if reader.read_line(&mut line).await.is_ok() {
|
||||||
if line.starts_with("HELP") {
|
if line.starts_with("HELP") {
|
||||||
return Ok(Command::Help);
|
return Ok(Command::Help);
|
||||||
|
|
@ -90,9 +90,9 @@ impl<R: AsyncBufRead + AsyncBufReadExt + std::marker::Unpin> Parser<R> for TextP
|
||||||
} else if line.starts_with("PX ") {
|
} else if line.starts_with("PX ") {
|
||||||
return self.parse_pixel(&line);
|
return self.parse_pixel(&line);
|
||||||
} else if line.starts_with("CANVAS ") {
|
} else if line.starts_with("CANVAS ") {
|
||||||
return self.parse_canvas(&line);
|
return TextParser::parse_canvas(&line);
|
||||||
} else if line.starts_with("PROTOCOL ") {
|
} else if line.starts_with("PROTOCOL ") {
|
||||||
return self.parse_protocol(&line);
|
return TextParser::parse_protocol(&line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(Error::from(ErrorKind::InvalidInput))
|
Err(Error::from(ErrorKind::InvalidInput))
|
||||||
|
|
@ -114,14 +114,10 @@ impl<W: AsyncWriteExt + std::marker::Unpin> Responder<W> for TextParser {
|
||||||
async fn unparse(&self, response: Response, writer: &mut W) -> io::Result<()> {
|
async fn unparse(&self, response: Response, writer: &mut W) -> io::Result<()> {
|
||||||
match response {
|
match response {
|
||||||
Response::Help => writer.write_all(HELP_TEXT).await,
|
Response::Help => writer.write_all(HELP_TEXT).await,
|
||||||
Response::Size(x, y) => {
|
Response::Size(x, y) => writer.write_all(format!("SIZE {x} {y}\n").as_bytes()).await,
|
||||||
writer
|
|
||||||
.write_all(format!("SIZE {} {}\n", x, y).as_bytes())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
Response::GetPixel(x, y, color) => {
|
Response::GetPixel(x, y, color) => {
|
||||||
writer
|
writer
|
||||||
.write_all(format!("PX {} {} {}\n", x, y, hex::encode_upper(color)).as_bytes())
|
.write_all(format!("PX {x} {y} {}\n", hex::encode_upper(color)).as_bytes())
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -129,6 +125,7 @@ impl<W: AsyncWriteExt + std::marker::Unpin> Responder<W> for TextParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
#[allow(clippy::needless_return)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use tokio::io::BufReader;
|
use tokio::io::BufReader;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue