move freestanding functions into impl

This commit is contained in:
Vinzenz Schroeter 2024-05-18 11:24:13 +02:00
parent d5e08faeb1
commit e615f3f949

View file

@ -57,12 +57,16 @@ impl From<Command> for Packet {
/// Move the `Command` into a `Packet` instance for sending.
fn from(value: Command) -> Self {
match value {
Command::Clear => command_code_only(CommandCode::Clear),
Command::FadeOut => command_code_only(CommandCode::FadeOut),
Command::HardReset => command_code_only(CommandCode::HardReset),
Command::Clear => Command::command_code_only(CommandCode::Clear),
Command::FadeOut => {
Command::command_code_only(CommandCode::FadeOut)
}
Command::HardReset => {
Command::command_code_only(CommandCode::HardReset)
}
#[allow(deprecated)]
Command::BitmapLegacy => {
command_code_only(CommandCode::BitmapLegacy)
Command::command_code_only(CommandCode::BitmapLegacy)
}
Command::CharBrightness(Origin(x, y), grid) => Packet(
Header(
@ -112,7 +116,7 @@ impl From<Command> for Packet {
)
}
Command::BitmapLinear(offset, bits, compression) => {
bitmap_linear_into_packet(
Command::bitmap_linear_into_packet(
CommandCode::BitmapLinear,
offset,
compression,
@ -120,7 +124,7 @@ impl From<Command> for Packet {
)
}
Command::BitmapLinearAnd(offset, bits, compression) => {
bitmap_linear_into_packet(
Command::bitmap_linear_into_packet(
CommandCode::BitmapLinearAnd,
offset,
compression,
@ -128,7 +132,7 @@ impl From<Command> for Packet {
)
}
Command::BitmapLinearOr(offset, bits, compression) => {
bitmap_linear_into_packet(
Command::bitmap_linear_into_packet(
CommandCode::BitmapLinearOr,
offset,
compression,
@ -136,7 +140,7 @@ impl From<Command> for Packet {
)
}
Command::BitmapLinearXor(offset, bits, compression) => {
bitmap_linear_into_packet(
Command::bitmap_linear_into_packet(
CommandCode::BitmapLinearXor,
offset,
compression,
@ -189,7 +193,7 @@ impl TryFrom<Packet> for Command {
};
match command_code {
CommandCode::Clear => match check_command_only(packet) {
CommandCode::Clear => match Self::check_command_only(packet) {
Some(err) => Err(err),
None => Ok(Command::Clear),
},
@ -209,11 +213,11 @@ impl TryFrom<Packet> for Command {
Ok(Command::Brightness(payload[0]))
}
}
CommandCode::HardReset => match check_command_only(packet) {
CommandCode::HardReset => match Self::check_command_only(packet) {
Some(err) => Err(err),
None => Ok(Command::HardReset),
},
CommandCode::FadeOut => match check_command_only(packet) {
CommandCode::FadeOut => match Self::check_command_only(packet) {
Some(err) => Err(err),
None => Ok(Command::FadeOut),
},
@ -234,45 +238,54 @@ impl TryFrom<Packet> for Command {
#[allow(deprecated)]
CommandCode::BitmapLegacy => Ok(Command::BitmapLegacy),
CommandCode::BitmapLinear => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
let (vec, compression) =
Self::packet_into_linear_bitmap(packet)?;
Ok(Command::BitmapLinear(a, vec, compression))
}
CommandCode::BitmapLinearAnd => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
let (vec, compression) =
Self::packet_into_linear_bitmap(packet)?;
Ok(Command::BitmapLinearAnd(a, vec, compression))
}
CommandCode::BitmapLinearOr => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
let (vec, compression) =
Self::packet_into_linear_bitmap(packet)?;
Ok(Command::BitmapLinearOr(a, vec, compression))
}
CommandCode::BitmapLinearXor => {
let (vec, compression) = packet_into_linear_bitmap(packet)?;
let (vec, compression) =
Self::packet_into_linear_bitmap(packet)?;
Ok(Command::BitmapLinearXor(a, vec, compression))
}
CommandCode::BitmapLinearWinUncompressed => {
packet_into_bitmap_win(packet, CompressionCode::Uncompressed)
Self::packet_into_bitmap_win(
packet,
CompressionCode::Uncompressed,
)
}
CommandCode::BitmapLinearWinZlib => {
packet_into_bitmap_win(packet, CompressionCode::Zlib)
Self::packet_into_bitmap_win(packet, CompressionCode::Zlib)
}
CommandCode::BitmapLinearWinBzip2 => {
packet_into_bitmap_win(packet, CompressionCode::Bzip2)
Self::packet_into_bitmap_win(packet, CompressionCode::Bzip2)
}
CommandCode::BitmapLinearWinLzma => {
packet_into_bitmap_win(packet, CompressionCode::Lzma)
Self::packet_into_bitmap_win(packet, CompressionCode::Lzma)
}
CommandCode::BitmapLinearWinZstd => {
packet_into_bitmap_win(packet, CompressionCode::Zstd)
Self::packet_into_bitmap_win(packet, CompressionCode::Zstd)
}
}
}
}
impl Command {
fn packet_into_bitmap_win(
packet: Packet,
compression: CompressionCode,
) -> Result<Command, TryFromPacketError> {
let Packet(Header(_, tiles_x, pixels_y, tile_w, pixel_h), payload) = packet;
let Packet(Header(_, tiles_x, pixels_y, tile_w, pixel_h), payload) =
packet;
let payload = match into_decompressed(compression, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),
@ -331,7 +344,9 @@ fn packet_into_linear_bitmap(
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
let sub = match CompressionCode::try_from(sub) {
Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Err(_) => {
return Err(TryFromPacketError::InvalidCompressionCode(sub));
}
Ok(value) => value,
};
let payload = match into_decompressed(sub, payload) {
@ -346,6 +361,7 @@ fn packet_into_linear_bitmap(
}
Ok((BitVec::from(&*payload), sub))
}
}
#[cfg(feature = "c_api")]
pub mod c_api {
@ -740,7 +756,7 @@ mod tests {
assert_eq!(
Command::try_from(Packet(
Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!()
vec!(),
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))
);
@ -748,7 +764,7 @@ mod tests {
assert_eq!(
Command::try_from(Packet(
Header(CommandCode::Brightness.into(), 0, 0, 0, 0),
vec!(0, 0)
vec!(0, 0),
)),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))
);
@ -803,7 +819,7 @@ mod tests {
Command::try_from(p),
Err(TryFromPacketError::UnexpectedPayloadSize(
420,
length as usize
length as usize,
))
);
}