cargo fmt, clippy

This commit is contained in:
Vinzenz Schroeter 2024-05-17 23:56:20 +02:00
parent 46c9174d3d
commit 314bec36f3
5 changed files with 127 additions and 58 deletions

View file

@ -24,10 +24,7 @@ fn main() {
connection.send(Command::Clear.into()).unwrap(); connection.send(Command::Clear.into()).unwrap();
} }
let max_width = cli.text.iter() let max_width = cli.text.iter().map(|t| t.len()).max().unwrap();
.map(|t| t.len())
.max()
.unwrap();
let mut chars = ByteGrid::new(max_width, cli.text.len()); let mut chars = ByteGrid::new(max_width, cli.text.len());
for y in 0..cli.text.len() { for y in 0..cli.text.len() {

View file

@ -91,7 +91,10 @@ impl BitVec {
/// Calculates the byte index and bitmask for a specific bit in the vector /// Calculates the byte index and bitmask for a specific bit in the vector
fn get_indexes(&self, bit_index: usize) -> (usize, u8) { fn get_indexes(&self, bit_index: usize) -> (usize, u8) {
if bit_index >= self.size { 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; let byte_index = bit_index / 8;
@ -296,10 +299,10 @@ mod tests {
#[test] #[test]
fn get_returns_old() { fn get_returns_old() {
let mut vec = BitVec::new(8); let mut vec = BitVec::new(8);
assert_eq!(vec.set(1, true), false); assert!(!vec.set(1, true));
assert_eq!(vec.set(1, true), true); assert!(vec.set(1, true));
assert_eq!(vec.set(1, false), true); assert!(vec.set(1, false));
assert_eq!(vec.set(1, false), false); assert!(!vec.set(1, false));
} }
#[test] #[test]

View file

@ -1,8 +1,8 @@
use crate::command_code::CommandCode;
use crate::compression::{into_compressed, into_decompressed};
use crate::{ use crate::{
BitVec, ByteGrid, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE, 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. /// An origin marks the top left position of a window sent to the display.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@ -521,9 +521,12 @@ pub mod c_api {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet, PixelGrid};
use crate::command::TryFromPacketError; use crate::command::TryFromPacketError;
use crate::command_code::CommandCode; use crate::command_code::CommandCode;
use crate::{
BitVec, ByteGrid, Command, CompressionCode, Header, Origin, Packet,
PixelGrid,
};
fn round_trip(original: Command) { fn round_trip(original: Command) {
let packet: Packet = original.clone().into(); let packet: Packet = original.clone().into();
@ -609,55 +612,93 @@ mod tests {
#[test] #[test]
fn error_invalid_command() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::InvalidCommand(0xFF)))) assert!(matches!(
result,
Err(TryFromPacketError::InvalidCommand(0xFF))
))
} }
#[test] #[test]
fn error_extraneous_header_values_clear() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
} }
#[test] #[test]
fn error_extraneous_header_values_brightness() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
} }
#[test] #[test]
fn error_extraneous_header_hard_reset() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
} }
#[test] #[test]
fn error_extraneous_header_fade_out() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::ExtraneousHeaderValues))) assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
} }
#[test] #[test]
fn error_unexpected_payload() { 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); let result = Command::try_from(p);
assert!(matches!(result, Err(TryFromPacketError::UnexpectedPayloadSize(0, 2)))) assert!(matches!(
result,
Err(TryFromPacketError::UnexpectedPayloadSize(0, 2))
))
} }
#[test] #[test]
fn error_decompression_failed_win() { fn error_decompression_failed_win() {
for compression in all_compressions() { 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; let Packet(header, mut payload) = p;
// mangle it // mangle it
for i in 0..payload.len() { for byte in payload.iter_mut() {
payload[i] -= payload[i] / 2; *byte -= *byte / 2;
} }
let p = Packet(header, payload); let p = Packet(header, payload);
@ -665,7 +706,7 @@ mod tests {
if compression != CompressionCode::Uncompressed { if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed)) assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else { } else {
assert!(matches!(result, Ok(_))); assert!(result.is_ok());
} }
} }
} }
@ -673,12 +714,13 @@ mod tests {
#[test] #[test]
fn error_decompression_failed_and() { fn error_decompression_failed_and() {
for compression in all_compressions() { 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; let Packet(header, mut payload) = p;
// mangle it // mangle it
for i in 0..payload.len() { for byte in payload.iter_mut() {
payload[i] -= payload[i] / 2; *byte -= *byte / 2;
} }
let p = Packet(header, payload); let p = Packet(header, payload);
@ -686,7 +728,7 @@ mod tests {
if compression != CompressionCode::Uncompressed { if compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed)) assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
} else { } else {
assert!(matches!(result, Ok(_))); assert!(result.is_ok());
} }
} }
} }
@ -694,44 +736,73 @@ mod tests {
#[test] #[test]
fn unexpected_payload_size_brightness() { fn unexpected_payload_size_brightness() {
assert_eq!( assert_eq!(
Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!())), Command::try_from(Packet(
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))); Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!()
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))
);
assert_eq!( assert_eq!(
Command::try_from(Packet(Header(CommandCode::Brightness.into(), 0, 0, 0, 0), vec!(0, 0))), Command::try_from(Packet(
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))); Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!(0, 0)
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))
);
} }
#[test] #[test]
fn error_reserved_used() { fn error_reserved_used() {
let Packet(header, payload) let Packet(header, payload) = Command::BitmapLinear(
= Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); 0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
let Header(command, offset, length, sub, _reserved) = header; let Header(command, offset, length, sub, _reserved) = header;
let p = Packet(Header(command, offset, length, sub, 69), payload); let p = Packet(Header(command, offset, length, sub, 69), payload);
assert_eq!( assert_eq!(
Command::try_from(p), Command::try_from(p),
Err(TryFromPacketError::ExtraneousHeaderValues)); Err(TryFromPacketError::ExtraneousHeaderValues)
);
} }
#[test] #[test]
fn error_invalid_compression() { fn error_invalid_compression() {
let Packet(header, payload) let Packet(header, payload) = Command::BitmapLinear(
= Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); 0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
let Header(command, offset, length, _sub, reserved) = header; let Header(command, offset, length, _sub, reserved) = header;
let p = Packet(Header(command, offset, length, 42, reserved), payload); let p = Packet(Header(command, offset, length, 42, reserved), payload);
assert_eq!( assert_eq!(
Command::try_from(p), Command::try_from(p),
Err(TryFromPacketError::InvalidCompressionCode(42))); Err(TryFromPacketError::InvalidCompressionCode(42))
);
} }
#[test] #[test]
fn error_unexpected_size() { fn error_unexpected_size() {
let Packet(header, payload) let Packet(header, payload) = Command::BitmapLinear(
= Command::BitmapLinear(0, BitVec::new(8), CompressionCode::Uncompressed).into(); 0,
BitVec::new(8),
CompressionCode::Uncompressed,
)
.into();
let Header(command, offset, length, compression, reserved) = header; 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!( assert_eq!(
Command::try_from(p), Command::try_from(p),
Err(TryFromPacketError::UnexpectedPayloadSize(420, length as usize))); Err(TryFromPacketError::UnexpectedPayloadSize(
420,
length as usize
))
);
} }
} }

View file

@ -48,12 +48,10 @@ pub(crate) fn into_decompressed(
} }
} }
#[cfg(feature = "compression_lzma")] #[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => { CompressionCode::Lzma => match lzma::decompress(&payload) {
match lzma::decompress(&payload) { Err(_) => None,
Err(_) => None, Ok(decompressed) => Some(decompressed),
Ok(decompressed) => Some(decompressed), },
}
}
#[cfg(feature = "compression_zstd")] #[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => { CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(&*payload) { let mut decoder = match ZstdDecoder::new(&*payload) {

View file

@ -238,16 +238,16 @@ mod tests {
#[test] #[test]
fn get_set() { fn get_set() {
let mut grid = PixelGrid::new(8, 2); let mut grid = PixelGrid::new(8, 2);
assert_eq!(grid.get(0, 0), false); assert!(!grid.get(0, 0));
assert_eq!(grid.get(1, 1), false); assert!(!grid.get(1, 1));
grid.set(5, 0, true); grid.set(5, 0, true);
grid.set(1, 1, true); grid.set(1, 1, true);
assert_eq!(grid.mut_data_ref(), [0x04, 0x40]); assert_eq!(grid.mut_data_ref(), [0x04, 0x40]);
assert_eq!(grid.get(5, 0), true); assert!(grid.get(5, 0));
assert_eq!(grid.get(1, 1), true); assert!(grid.get(1, 1));
assert_eq!(grid.get(1, 0), false); assert!(!grid.get(1, 0));
} }
#[test] #[test]