From 362426c758aa4c05123d3b8cef42d07a61db522d Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 11 May 2024 23:28:08 +0200 Subject: [PATCH] reformat with max width --- examples/announce/src/main.rs | 5 +- examples/game_of_life/src/main.rs | 18 ++- examples/moving_line/src/main.rs | 8 +- examples/random_brightness/src/main.rs | 12 +- examples/wiping_clear/src/main.rs | 11 +- rustfmt.toml | 1 + src/bit_vec.rs | 8 +- src/byte_grid.rs | 2 +- src/command.rs | 179 ++++++++++++++++--------- src/compression.rs | 68 +++++----- src/connection.rs | 7 +- src/lib.rs | 24 ++-- src/packet.rs | 4 +- 13 files changed, 225 insertions(+), 122 deletions(-) create mode 100644 rustfmt.toml diff --git a/examples/announce/src/main.rs b/examples/announce/src/main.rs index 2dd90b5..075151e 100644 --- a/examples/announce/src/main.rs +++ b/examples/announce/src/main.rs @@ -1,4 +1,5 @@ use clap::Parser; + use servicepoint2::{ByteGrid, Command, Connection, Origin}; #[derive(Parser, Debug)] @@ -44,5 +45,7 @@ fn main() { } } - connection.send(Command::Cp437Data(Origin::top_left(), chars)).unwrap(); + connection + .send(Command::Cp437Data(Origin::top_left(), chars)) + .unwrap(); } diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index b2a2a66..219ff2e 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -1,9 +1,11 @@ use std::thread; use std::time::Duration; -use rand::{distributions, Rng}; + use clap::Parser; -use servicepoint2::{Connection, Origin, PixelGrid}; +use rand::{distributions, Rng}; + use servicepoint2::Command::BitmapLinearWin; +use servicepoint2::{Connection, Origin, PixelGrid}; #[derive(Parser, Debug)] struct Cli { @@ -23,7 +25,9 @@ fn main() { let mut field = make_random_field(cli.probability); loop { - connection.send(BitmapLinearWin(Origin::top_left(), field.clone())).expect("could not send"); + connection + .send(BitmapLinearWin(Origin::top_left(), field.clone())) + .expect("could not send"); thread::sleep(Duration::from_millis(14)); field = iteration(field); } @@ -39,7 +43,7 @@ fn iteration(field: PixelGrid) -> PixelGrid { (true, 2) => true, (true, 3) => true, (false, 3) => true, - _ => false + _ => false, }; next.set(x, y, new_state); } @@ -55,7 +59,11 @@ fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 { continue; // the cell itself does not count } - if nx < 0 || ny < 0 || nx >= field.width as i32 || ny >= field.height as i32 { + if nx < 0 + || ny < 0 + || nx >= field.width as i32 + || ny >= field.height as i32 + { continue; // pixels outside the grid do not count } diff --git a/examples/moving_line/src/main.rs b/examples/moving_line/src/main.rs index a7ec100..8f0d3cd 100644 --- a/examples/moving_line/src/main.rs +++ b/examples/moving_line/src/main.rs @@ -1,8 +1,10 @@ use std::thread; use std::time::Duration; + use clap::Parser; -use servicepoint2::{Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid}; + use servicepoint2::Command::BitmapLinearWin; +use servicepoint2::{Connection, Origin, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH}; #[derive(Parser, Debug)] struct Cli { @@ -24,7 +26,9 @@ fn main() { for y in 0..PIXEL_HEIGHT as usize { pixels.set((y + x_offset) % PIXEL_WIDTH as usize, y, true); } - connection.send(BitmapLinearWin(Origin::top_left(), pixels.clone())).unwrap(); + connection + .send(BitmapLinearWin(Origin::top_left(), pixels.clone())) + .unwrap(); thread::sleep(Duration::from_millis(14)); } } diff --git a/examples/random_brightness/src/main.rs b/examples/random_brightness/src/main.rs index 112edc2..4ea272e 100644 --- a/examples/random_brightness/src/main.rs +++ b/examples/random_brightness/src/main.rs @@ -1,8 +1,12 @@ use std::time::Duration; + use clap::Parser; use rand::Rng; -use servicepoint2::{ByteGrid, Connection, Origin, PixelGrid, TILE_HEIGHT, TILE_WIDTH}; -use servicepoint2::Command::{Brightness, CharBrightness, BitmapLinearWin}; + +use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness}; +use servicepoint2::{ + ByteGrid, Connection, Origin, PixelGrid, TILE_HEIGHT, TILE_WIDTH, +}; #[derive(Parser, Debug)] struct Cli { @@ -27,7 +31,9 @@ fn main() { if cli.enable_all { let mut filled_grid = PixelGrid::max_sized(); filled_grid.fill(true); - connection.send(BitmapLinearWin(Origin::top_left(), filled_grid)).unwrap(); + connection + .send(BitmapLinearWin(Origin::top_left(), filled_grid)) + .unwrap(); } // set all pixels to the same random brightness diff --git a/examples/wiping_clear/src/main.rs b/examples/wiping_clear/src/main.rs index 67f4c1a..069f06d 100644 --- a/examples/wiping_clear/src/main.rs +++ b/examples/wiping_clear/src/main.rs @@ -3,8 +3,10 @@ use std::time::Duration; use clap::Parser; -use servicepoint2::{BitVec, CompressionCode, Connection, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid}; use servicepoint2::Command::BitmapLinearAnd; +use servicepoint2::{ + BitVec, CompressionCode, Connection, PixelGrid, PIXEL_HEIGHT, PIXEL_WIDTH, +}; #[derive(Parser, Debug)] struct Cli { @@ -21,7 +23,8 @@ fn main() { let connection = Connection::open(cli.destination).unwrap(); let sleep_duration = Duration::from_millis(cli.time / PIXEL_WIDTH as u64); - let mut enabled_pixels = PixelGrid::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize); + let mut enabled_pixels = + PixelGrid::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize); enabled_pixels.fill(true); for x_offset in 0..PIXEL_WIDTH as usize { @@ -33,7 +36,9 @@ fn main() { let pixel_data: Vec = enabled_pixels.clone().into(); let bit_vec = BitVec::load(&*pixel_data); - connection.send(BitmapLinearAnd(0, bit_vec, CompressionCode::Gz)).unwrap(); + connection + .send(BitmapLinearAnd(0, bit_vec, CompressionCode::Gz)) + .unwrap(); thread::sleep(sleep_duration); } } diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..df99c69 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 80 diff --git a/src/bit_vec.rs b/src/bit_vec.rs index 524fbec..4637de9 100644 --- a/src/bit_vec.rs +++ b/src/bit_vec.rs @@ -7,11 +7,15 @@ pub struct BitVec { impl BitVec { pub fn new(size: usize) -> BitVec { assert_eq!(size % 8, 0); - Self { data: vec!(0; size / 8) } + Self { + data: vec![0; size / 8], + } } pub fn load(data: &[u8]) -> BitVec { - Self { data: Vec::from(data) } + Self { + data: Vec::from(data), + } } pub fn set(&mut self, index: usize, value: bool) -> bool { diff --git a/src/byte_grid.rs b/src/byte_grid.rs index fd702bf..61301ff 100644 --- a/src/byte_grid.rs +++ b/src/byte_grid.rs @@ -31,7 +31,7 @@ impl ByteGrid { self.data[x + y * self.width] = value; } - pub fn fill(&mut self, value: u8){ + pub fn fill(&mut self, value: u8) { self.data.fill(value) } } diff --git a/src/command.rs b/src/command.rs index f126d0b..6ed8b76 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,7 +1,6 @@ -use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE}; use crate::command_codes::{CommandCode, CompressionCode}; use crate::compression::{into_compressed, into_decompressed}; - +use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE}; /// An origin marks the top left position of the /// data sent to the display. @@ -46,45 +45,77 @@ impl Into for Command { Command::FadeOut => command_code_only(CommandCode::FadeOut), Command::HardReset => command_code_only(CommandCode::HardReset), #[allow(deprecated)] - Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy), - Command::CharBrightness(origin, grid) => { - origin_size_payload(CommandCode::CharBrightness, - origin, - Size(grid.width as u16, grid.height as u16), - grid.into()) - } - Command::Brightness(brightness) => { - Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness)) + Command::BitmapLegacy => { + command_code_only(CommandCode::BitmapLegacy) } + Command::CharBrightness(origin, grid) => origin_size_payload( + CommandCode::CharBrightness, + origin, + Size(grid.width as u16, grid.height as u16), + grid.into(), + ), + Command::Brightness(brightness) => Packet( + Header( + CommandCode::Brightness.to_primitive(), + 0x00000, + 0x0000, + 0x0000, + 0x0000, + ), + vec![brightness], + ), Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => { debug_assert_eq!(pixel_x % 8, 0); debug_assert_eq!(pixels.width % 8, 0); Packet( - Header(CommandCode::BitmapLinearWin.to_primitive(), - pixel_x / TILE_SIZE, - pixel_y, - pixels.width as u16 / TILE_SIZE, - pixels.height as u16), - pixels.into()) + Header( + CommandCode::BitmapLinearWin.to_primitive(), + pixel_x / TILE_SIZE, + pixel_y, + pixels.width as u16 / TILE_SIZE, + pixels.height as u16, + ), + pixels.into(), + ) } Command::BitmapLinear(offset, bits, compression) => { - bitmap_linear_into_packet(CommandCode::BitmapLinear, offset, compression, bits.into()) + bitmap_linear_into_packet( + CommandCode::BitmapLinear, + offset, + compression, + bits.into(), + ) } Command::BitmapLinearAnd(offset, bits, compression) => { - bitmap_linear_into_packet(CommandCode::BitmapLinearAnd, offset, compression, bits.into()) + bitmap_linear_into_packet( + CommandCode::BitmapLinearAnd, + offset, + compression, + bits.into(), + ) } Command::BitmapLinearOr(offset, bits, compression) => { - bitmap_linear_into_packet(CommandCode::BitmapLinearOr, offset, compression, bits.into()) + bitmap_linear_into_packet( + CommandCode::BitmapLinearOr, + offset, + compression, + bits.into(), + ) } Command::BitmapLinearXor(offset, bits, compression) => { - bitmap_linear_into_packet(CommandCode::BitmapLinearXor, offset, compression, bits.into()) - } - Command::Cp437Data(origin, grid) => { - origin_size_payload(CommandCode::Cp437Data, - origin, - Size(grid.width as u16, grid.height as u16), - grid.into()) + bitmap_linear_into_packet( + CommandCode::BitmapLinearXor, + offset, + compression, + bits.into(), + ) } + Command::Cp437Data(origin, grid) => origin_size_payload( + CommandCode::Cp437Data, + origin, + Size(grid.width as u16, grid.height as u16), + grid.into(), + ), } } } @@ -104,21 +135,24 @@ impl TryFrom for Command { fn try_from(value: Packet) -> Result { let Packet(Header(command_u16, a, b, c, d), _) = value; let command_code = match CommandCode::from_primitive(command_u16) { - None => return Err(TryFromPacketError::InvalidCommand(command_u16)), - Some(value) => value + None => { + return Err(TryFromPacketError::InvalidCommand(command_u16)) + } + Some(value) => value, }; match command_code { - CommandCode::Clear => { - match check_command_only(value) { - Some(err) => Err(err), - None => Ok(Command::Clear), - } - } + CommandCode::Clear => match check_command_only(value) { + Some(err) => Err(err), + None => Ok(Command::Clear), + }, CommandCode::Brightness => { let Packet(header, payload) = value; if payload.len() != 1 { - return Err(TryFromPacketError::UnexpectedPayloadSize(1, payload.len())); + return Err(TryFromPacketError::UnexpectedPayloadSize( + 1, + payload.len(), + )); } match check_empty_header(header) { @@ -126,18 +160,14 @@ impl TryFrom for Command { None => Ok(Command::Brightness(payload[0])), } } - CommandCode::HardReset => { - match check_command_only(value) { - Some(err) => Err(err), - None => Ok(Command::HardReset), - } - } - CommandCode::FadeOut => { - match check_command_only(value) { - Some(err) => Err(err), - None => Ok(Command::FadeOut), - } - } + CommandCode::HardReset => match check_command_only(value) { + Some(err) => Err(err), + None => Ok(Command::HardReset), + }, + CommandCode::FadeOut => match check_command_only(value) { + Some(err) => Err(err), + None => Ok(Command::FadeOut), + }, CommandCode::Cp437Data => { let Packet(_, payload) = value; Ok(Command::Cp437Data( @@ -153,14 +183,16 @@ impl TryFrom for Command { )) } #[allow(deprecated)] - CommandCode::BitmapLegacy => { - Ok(Command::BitmapLegacy) - } + CommandCode::BitmapLegacy => Ok(Command::BitmapLegacy), CommandCode::BitmapLinearWin => { let Packet(_, payload) = value; Ok(Command::BitmapLinearWin( Origin(a * TILE_SIZE, b), - PixelGrid::load(c as usize * TILE_SIZE as usize, d as usize, &payload), + PixelGrid::load( + c as usize * TILE_SIZE as usize, + d as usize, + &payload, + ), )) } CommandCode::BitmapLinear => { @@ -183,20 +215,42 @@ impl TryFrom for Command { } } -fn bitmap_linear_into_packet(command: CommandCode, offset: Offset, compression: CompressionCode, payload: Vec) -> Packet { +fn bitmap_linear_into_packet( + command: CommandCode, + offset: Offset, + compression: CompressionCode, + payload: Vec, +) -> Packet { let payload = into_compressed(compression, payload); let compression = CompressionCode::to_primitive(&compression); - Packet(Header(command.to_primitive(), offset, payload.len() as u16, compression, 0), payload) + Packet( + Header( + command.to_primitive(), + offset, + payload.len() as u16, + compression, + 0, + ), + payload, + ) } -fn origin_size_payload(command: CommandCode, origin: Origin, size: Size, payload: Vec) -> Packet { +fn origin_size_payload( + command: CommandCode, + origin: Origin, + size: Size, + payload: Vec, +) -> Packet { let Origin(x, y) = origin; let Size(w, h) = size; Packet(Header(command.to_primitive(), x, y, w, h), payload.into()) } fn command_code_only(code: CommandCode) -> Packet { - Packet(Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000), vec!()) + Packet( + Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000), + vec![], + ) } fn check_empty_header(header: Header) -> Option { @@ -219,21 +273,26 @@ fn check_command_only(packet: Packet) -> Option { } } -fn packet_into_linear_bitmap(packet: Packet) -> Result<(BitVec, CompressionCode), TryFromPacketError> { +fn packet_into_linear_bitmap( + packet: Packet, +) -> Result<(BitVec, CompressionCode), TryFromPacketError> { let Packet(Header(_, _, length, sub, reserved), payload) = packet; if reserved != 0 { return Err(TryFromPacketError::ExtraneousHeaderValues); } if payload.len() != length as usize { - return Err(TryFromPacketError::UnexpectedPayloadSize(length as usize, payload.len())); + return Err(TryFromPacketError::UnexpectedPayloadSize( + length as usize, + payload.len(), + )); } let sub = match CompressionCode::from_primitive(sub) { None => return Err(TryFromPacketError::InvalidCompressionCode(sub)), - Some(value) => value + Some(value) => value, }; let payload = match into_decompressed(sub, payload) { None => return Err(TryFromPacketError::DecompressionFailed), - Some(value) => value + Some(value) => value, }; Ok((BitVec::load(&payload), sub)) } diff --git a/src/compression.rs b/src/compression.rs index 3798575..4cfe277 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -1,70 +1,78 @@ -use crate::{CompressionCode, Payload}; use std::io::{Read, Write}; #[cfg(feature = "compression-bz")] -use bzip2::read::{BzEncoder, BzDecoder}; +use bzip2::read::{BzDecoder, BzEncoder}; #[cfg(feature = "compression-gz")] -use flate2::read::{GzEncoder, GzDecoder}; +use flate2::read::{GzDecoder, GzEncoder}; #[cfg(feature = "compression-lz")] -use lz4::{EncoderBuilder as Lz4EncoderBuilder, Decoder as Lz4Decoder}; +use lz4::{Decoder as Lz4Decoder, EncoderBuilder as Lz4EncoderBuilder}; #[cfg(feature = "compression-zs")] -use zstd::{Encoder as ZstdEncoder, Decoder as ZstdDecoder}; +use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder}; -pub(crate) fn into_decompressed(kind: CompressionCode, payload: Payload) -> Option { +use crate::{CompressionCode, Payload}; + +pub(crate) fn into_decompressed( + kind: CompressionCode, + payload: Payload, +) -> Option { match kind { CompressionCode::None => Some(payload), #[cfg(feature = "compression-gz")] CompressionCode::Gz => { let mut decoder = GzDecoder::new(&*payload); - let mut decompressed = vec!(); + let mut decompressed = vec![]; match decoder.read_to_end(&mut decompressed) { Err(_) => None, - Ok(_) => Some(decompressed) + Ok(_) => Some(decompressed), } } #[cfg(feature = "compression-bz")] CompressionCode::Bz => { let mut decoder = BzDecoder::new(&*payload); - let mut decompressed = vec!(); + let mut decompressed = vec![]; match decoder.read_to_end(&mut decompressed) { Err(_) => None, - Ok(_) => Some(decompressed) + Ok(_) => Some(decompressed), } } #[cfg(feature = "compression-lz")] CompressionCode::Lz => { let mut decoder = match Lz4Decoder::new(&*payload) { Err(_) => return None, - Ok(value) => value + Ok(value) => value, }; - let mut decompressed = vec!(); + let mut decompressed = vec![]; match decoder.read_to_end(&mut decompressed) { Err(_) => None, - Ok(_) => Some(decompressed) + Ok(_) => Some(decompressed), } } #[cfg(feature = "compression-zs")] CompressionCode::Zs => { let mut decoder = match ZstdDecoder::new(&*payload) { Err(_) => return None, - Ok(value) => value + Ok(value) => value, }; - let mut decompressed = vec!(); + let mut decompressed = vec![]; match decoder.read_to_end(&mut decompressed) { Err(_) => None, - Ok(_) => Some(decompressed) + Ok(_) => Some(decompressed), } } } } -pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payload { +pub(crate) fn into_compressed( + kind: CompressionCode, + payload: Payload, +) -> Payload { match kind { CompressionCode::None => payload, #[cfg(feature = "compression-gz")] CompressionCode::Gz => { - let mut encoder = GzEncoder::new(&*payload, flate2::Compression::fast()); - let mut compressed = vec!(); + let mut encoder = + GzEncoder::new(&*payload, flate2::Compression::fast()); + let mut compressed = vec![]; match encoder.read_to_end(&mut compressed) { Err(err) => panic!("could not compress payload: {}", err), Ok(_) => compressed, @@ -72,8 +80,9 @@ pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payloa } #[cfg(feature = "compression-bz")] CompressionCode::Bz => { - let mut encoder = BzEncoder::new(&*payload, bzip2::Compression::fast()); - let mut compressed = vec!(); + let mut encoder = + BzEncoder::new(&*payload, bzip2::Compression::fast()); + let mut compressed = vec![]; match encoder.read_to_end(&mut compressed) { Err(err) => panic!("could not compress payload: {}", err), Ok(_) => compressed, @@ -82,22 +91,21 @@ pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payloa #[cfg(feature = "compression-lz")] CompressionCode::Lz => { let mut encoder = Lz4EncoderBuilder::new() - .build(vec!()) + .build(vec![]) .expect("could not create encoder"); - encoder.write(&*payload) - .expect("could not write payload"); + encoder.write(&*payload).expect("could not write payload"); let (payload, _) = encoder.finish(); payload } #[cfg(feature = "compression-zs")] CompressionCode::Zs => { - let mut encoder = ZstdEncoder::new(vec!(), zstd::DEFAULT_COMPRESSION_LEVEL) - .expect("could not create encoder"); - encoder.write(&*payload) + let mut encoder = + ZstdEncoder::new(vec![], zstd::DEFAULT_COMPRESSION_LEVEL) + .expect("could not create encoder"); + encoder + .write(&*payload) .expect("could not compress payload"); - encoder.finish() - .expect("could not finish encoding") + encoder.finish().expect("could not finish encoding") } } } - diff --git a/src/connection.rs b/src/connection.rs index ef9bbc5..e3728e4 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,6 +1,8 @@ use std::fmt::Debug; use std::net::{ToSocketAddrs, UdpSocket}; + use log::{debug, info}; + use crate::Packet; pub struct Connection { @@ -17,7 +19,10 @@ impl Connection { } /// Send a command to the display - pub fn send(&self, packet: impl Into + Debug) -> Result<(), std::io::Error> { + pub fn send( + &self, + packet: impl Into + Debug, + ) -> Result<(), std::io::Error> { debug!("sending {packet:?}"); let packet: Packet = packet.into(); let data: Vec = packet.into(); diff --git a/src/lib.rs b/src/lib.rs index 3242a21..c843249 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,19 @@ -mod connection; -mod pixel_grid; +pub use crate::bit_vec::BitVec; +pub use crate::byte_grid::ByteGrid; +pub use crate::command::{Command, Origin, Size}; +pub use crate::command_codes::{CommandCode, CompressionCode}; +pub use crate::connection::Connection; +pub use crate::packet::{Header, Packet, Payload}; +pub use crate::pixel_grid::PixelGrid; + mod bit_vec; -mod packet; +mod byte_grid; mod command; mod command_codes; -mod byte_grid; mod compression; - -pub use crate::connection::Connection; -pub use crate::pixel_grid::PixelGrid; -pub use crate::bit_vec::BitVec; -pub use crate::packet::{Packet, Header, Payload}; -pub use crate::command::{Command, Size, Origin}; -pub use crate::command_codes::{CommandCode, CompressionCode}; -pub use crate::byte_grid::ByteGrid; +mod connection; +mod packet; +mod pixel_grid; pub const TILE_SIZE: u16 = 8; pub const TILE_WIDTH: u16 = 56; diff --git a/src/packet.rs b/src/packet.rs index 5bda2c2..3e88f6f 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -10,7 +10,7 @@ impl Into> for Packet { fn into(self) -> Vec { let Packet(Header(mode, a, b, c, d), payload) = self; - let mut packet = vec!(0u8; 10 + payload.len()); + let mut packet = vec![0u8; 10 + payload.len()]; packet[0..=1].copy_from_slice(&u16::to_be_bytes(mode)); packet[2..=3].copy_from_slice(&u16::to_be_bytes(a)); packet[4..=5].copy_from_slice(&u16::to_be_bytes(b)); @@ -41,4 +41,4 @@ impl From> for Packet { Packet(Header(mode, a, b, c, d), payload) } -} \ No newline at end of file +}