From 314bec36f3ededc5027e0fe925b7794c44c28aba Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 17 May 2024 23:56:20 +0200 Subject: [PATCH] cargo fmt, clippy --- examples/announce/src/main.rs | 5 +- servicepoint2/src/bit_vec.rs | 13 +-- servicepoint2/src/command.rs | 145 +++++++++++++++++++++++-------- servicepoint2/src/compression.rs | 10 +-- servicepoint2/src/pixel_grid.rs | 12 +-- 5 files changed, 127 insertions(+), 58 deletions(-) diff --git a/examples/announce/src/main.rs b/examples/announce/src/main.rs index 2916f50..9c6be95 100644 --- a/examples/announce/src/main.rs +++ b/examples/announce/src/main.rs @@ -24,10 +24,7 @@ fn main() { connection.send(Command::Clear.into()).unwrap(); } - let max_width = cli.text.iter() - .map(|t| t.len()) - .max() - .unwrap(); + let max_width = cli.text.iter().map(|t| t.len()).max().unwrap(); let mut chars = ByteGrid::new(max_width, cli.text.len()); for y in 0..cli.text.len() { diff --git a/servicepoint2/src/bit_vec.rs b/servicepoint2/src/bit_vec.rs index 455123f..099704e 100644 --- a/servicepoint2/src/bit_vec.rs +++ b/servicepoint2/src/bit_vec.rs @@ -91,7 +91,10 @@ impl BitVec { /// Calculates the byte index and bitmask for a specific bit in the vector fn get_indexes(&self, bit_index: usize) -> (usize, u8) { if bit_index >= self.size { - panic!("bit index {bit_index} is outside of range 0..<{}", self.size) + panic!( + "bit index {bit_index} is outside of range 0..<{}", + self.size + ) } let byte_index = bit_index / 8; @@ -296,10 +299,10 @@ mod tests { #[test] fn get_returns_old() { let mut vec = BitVec::new(8); - assert_eq!(vec.set(1, true), false); - assert_eq!(vec.set(1, true), true); - assert_eq!(vec.set(1, false), true); - assert_eq!(vec.set(1, false), false); + assert!(!vec.set(1, true)); + assert!(vec.set(1, true)); + assert!(vec.set(1, false)); + assert!(!vec.set(1, false)); } #[test] diff --git a/servicepoint2/src/command.rs b/servicepoint2/src/command.rs index 27d1cd0..5eb0c59 100644 --- a/servicepoint2/src/command.rs +++ b/servicepoint2/src/command.rs @@ -1,8 +1,8 @@ +use crate::command_code::CommandCode; +use crate::compression::{into_compressed, into_decompressed}; use crate::{ BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE, }; -use crate::command_code::CommandCode; -use crate::compression::{into_compressed, into_decompressed}; /// An origin marks the top left position of a window sent to the display. #[derive(Debug, Clone, Copy, PartialEq)] @@ -521,9 +521,12 @@ pub mod c_api { #[cfg(test)] mod tests { - use crate::{BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet, PixelGrid}; use crate::command::TryFromPacketError; use crate::command_code::CommandCode; + use crate::{ + BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet, + PixelGrid, + }; fn round_trip(original: Command) { let packet: Packet = original.clone().into(); @@ -609,55 +612,93 @@ mod tests { #[test] fn error_invalid_command() { - let p = Packet(Header(0xFF, 0x00, 0x00, 0x00, 0x00), vec!()); + let p = Packet(Header(0xFF, 0x00, 0x00, 0x00, 0x00), vec![]); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::InvalidCommand(0xFF)))) + assert!(matches!( + result, + Err(TryFromPacketError::InvalidCommand(0xFF)) + )) } #[test] fn error_extraneous_header_values_clear() { - let p = Packet(Header(CommandCode::Clear.into(), 0x05, 0x00, 0x00, 0x00), vec!()); + let p = Packet( + Header(CommandCode::Clear.into(), 0x05, 0x00, 0x00, 0x00), + vec![], + ); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) + assert!(matches!( + result, + Err(TryFromPacketError::ExtraneousHeaderValues) + )) } #[test] fn error_extraneous_header_values_brightness() { - let p = Packet(Header(CommandCode::Brightness.into(), 0x00, 0x13, 0x37, 0x00), vec!(5)); + let p = Packet( + Header(CommandCode::Brightness.into(), 0x00, 0x13, 0x37, 0x00), + vec![5], + ); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) + assert!(matches!( + result, + Err(TryFromPacketError::ExtraneousHeaderValues) + )) } #[test] fn error_extraneous_header_hard_reset() { - let p = Packet(Header(CommandCode::HardReset.into(), 0x00, 0x00, 0x00, 0x01), vec!()); + let p = Packet( + Header(CommandCode::HardReset.into(), 0x00, 0x00, 0x00, 0x01), + vec![], + ); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) + assert!(matches!( + result, + Err(TryFromPacketError::ExtraneousHeaderValues) + )) } #[test] fn error_extraneous_header_fade_out() { - let p = Packet(Header(CommandCode::FadeOut.into(), 0x10, 0x00, 0x00, 0x01), vec!()); + let p = Packet( + Header(CommandCode::FadeOut.into(), 0x10, 0x00, 0x00, 0x01), + vec![], + ); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) + assert!(matches!( + result, + Err(TryFromPacketError::ExtraneousHeaderValues) + )) } #[test] fn error_unexpected_payload() { - let p = Packet(Header(CommandCode::FadeOut.into(), 0x00, 0x00, 0x00, 0x00), vec!(5, 7)); + let p = Packet( + Header(CommandCode::FadeOut.into(), 0x00, 0x00, 0x00, 0x00), + vec![5, 7], + ); let result = Command::try_from(p); - assert!(matches!(result, Err(TryFromPacketError::UnexpectedPayloadSize(0, 2)))) + assert!(matches!( + result, + Err(TryFromPacketError::UnexpectedPayloadSize(0, 2)) + )) } #[test] fn error_decompression_failed_win() { for compression in all_compressions() { - let p: Packet = Command::BitmapLinearWin(Origin(16, 8), PixelGrid::new(8, 8), compression).into(); + let p: Packet = Command::BitmapLinearWin( + Origin(16, 8), + PixelGrid::new(8, 8), + compression, + ) + .into(); let Packet(header, mut payload) = p; // mangle it - for i in 0..payload.len() { - payload[i] -= payload[i] / 2; + for byte in payload.iter_mut() { + *byte -= *byte / 2; } let p = Packet(header, payload); @@ -665,7 +706,7 @@ mod tests { if compression != CompressionCode::Uncompressed { assert_eq!(result, Err(TryFromPacketError::DecompressionFailed)) } else { - assert!(matches!(result, Ok(_))); + assert!(result.is_ok()); } } } @@ -673,12 +714,13 @@ mod tests { #[test] fn error_decompression_failed_and() { for compression in all_compressions() { - let p: Packet = Command::BitmapLinearAnd(0, BitVec::new(8), compression).into(); + let p: Packet = + Command::BitmapLinearAnd(0, BitVec::new(8), compression).into(); let Packet(header, mut payload) = p; // mangle it - for i in 0..payload.len() { - payload[i] -= payload[i] / 2; + for byte in payload.iter_mut() { + *byte -= *byte / 2; } let p = Packet(header, payload); @@ -686,7 +728,7 @@ mod tests { if compression != CompressionCode::Uncompressed { assert_eq!(result, Err(TryFromPacketError::DecompressionFailed)) } else { - assert!(matches!(result, Ok(_))); + assert!(result.is_ok()); } } } @@ -694,44 +736,73 @@ mod tests { #[test] fn unexpected_payload_size_brightness() { assert_eq!( - Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!())), - Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))); + Command::try_from(Packet( + Header(CommandCode::Brightness.into(), 0, 0, 0, 0), + vec!() + )), + Err(TryFromPacketError::UnexpectedPayloadSize(1, 0)) + ); assert_eq!( - Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!(0, 0))), - Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))); + Command::try_from(Packet( + Header(CommandCode::Brightness.into(), 0, 0, 0, 0), + vec!(0, 0) + )), + Err(TryFromPacketError::UnexpectedPayloadSize(1, 2)) + ); } #[test] fn error_reserved_used() { - let Packet(header, payload) - = Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); + let Packet(header, payload) = Command::BitmapLinear( + 0, + BitVec::new(8), + CompressionCode::Uncompressed, + ) + .into(); let Header(command, offset, length, sub, _reserved) = header; let p = Packet(Header(command, offset, length, sub, 69), payload); assert_eq!( Command::try_from(p), - Err(TryFromPacketError::ExtraneousHeaderValues)); + Err(TryFromPacketError::ExtraneousHeaderValues) + ); } #[test] fn error_invalid_compression() { - let Packet(header, payload) - = Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); + let Packet(header, payload) = Command::BitmapLinear( + 0, + BitVec::new(8), + CompressionCode::Uncompressed, + ) + .into(); let Header(command, offset, length, _sub, reserved) = header; let p = Packet(Header(command, offset, length, 42, reserved), payload); assert_eq!( Command::try_from(p), - Err(TryFromPacketError::InvalidCompressionCode(42))); + Err(TryFromPacketError::InvalidCompressionCode(42)) + ); } #[test] fn error_unexpected_size() { - let Packet(header, payload) - = Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); + let Packet(header, payload) = Command::BitmapLinear( + 0, + BitVec::new(8), + CompressionCode::Uncompressed, + ) + .into(); let Header(command, offset, length, compression, reserved) = header; - let p = Packet(Header(command, offset, 420, compression, reserved), payload); + let p = Packet( + Header(command, offset, 420, compression, reserved), + payload, + ); assert_eq!( Command::try_from(p), - Err(TryFromPacketError::UnexpectedPayloadSize(420, length as usize))); + Err(TryFromPacketError::UnexpectedPayloadSize( + 420, + length as usize + )) + ); } } diff --git a/servicepoint2/src/compression.rs b/servicepoint2/src/compression.rs index 3ed90d4..50af136 100644 --- a/servicepoint2/src/compression.rs +++ b/servicepoint2/src/compression.rs @@ -48,12 +48,10 @@ pub(crate) fn into_decompressed( } } #[cfg(feature = "compression_lzma")] - CompressionCode::Lzma => { - match lzma::decompress(&payload) { - Err(_) => None, - Ok(decompressed) => Some(decompressed), - } - } + CompressionCode::Lzma => match lzma::decompress(&payload) { + Err(_) => None, + Ok(decompressed) => Some(decompressed), + }, #[cfg(feature = "compression_zstd")] CompressionCode::Zstd => { let mut decoder = match ZstdDecoder::new(&*payload) { diff --git a/servicepoint2/src/pixel_grid.rs b/servicepoint2/src/pixel_grid.rs index 386b465..2acbabe 100644 --- a/servicepoint2/src/pixel_grid.rs +++ b/servicepoint2/src/pixel_grid.rs @@ -238,16 +238,16 @@ mod tests { #[test] fn get_set() { let mut grid = PixelGrid::new(8, 2); - assert_eq!(grid.get(0, 0), false); - assert_eq!(grid.get(1, 1), false); + assert!(!grid.get(0, 0)); + assert!(!grid.get(1, 1)); grid.set(5, 0, true); grid.set(1, 1, true); assert_eq!(grid.mut_data_ref(), [0x04, 0x40]); - assert_eq!(grid.get(5, 0), true); - assert_eq!(grid.get(1, 1), true); - assert_eq!(grid.get(1, 0), false); + assert!(grid.get(5, 0)); + assert!(grid.get(1, 1)); + assert!(!grid.get(1, 0)); } #[test] @@ -266,4 +266,4 @@ mod tests { let mut grid = PixelGrid::load(8, 3, &data); assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]); } -} \ No newline at end of file +}