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