move freestanding functions into impl
This commit is contained in:
		
							parent
							
								
									d5e08faeb1
								
							
						
					
					
						commit
						e615f3f949
					
				
					 1 changed files with 110 additions and 94 deletions
				
			
		| 
						 | 
					@ -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)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn packet_into_bitmap_win(
 | 
					impl Command {
 | 
				
			||||||
 | 
					    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),
 | 
				
			||||||
| 
						 | 
					@ -288,30 +301,30 @@ fn packet_into_bitmap_win(
 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
            compression,
 | 
					            compression,
 | 
				
			||||||
        ))
 | 
					        ))
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Helper method for BitMapLinear*-Commands into Packet
 | 
					    /// Helper method for BitMapLinear*-Commands into Packet
 | 
				
			||||||
fn bitmap_linear_into_packet(
 | 
					    fn bitmap_linear_into_packet(
 | 
				
			||||||
        command: CommandCode,
 | 
					        command: CommandCode,
 | 
				
			||||||
        offset: Offset,
 | 
					        offset: Offset,
 | 
				
			||||||
        compression: CompressionCode,
 | 
					        compression: CompressionCode,
 | 
				
			||||||
        payload: Vec<u8>,
 | 
					        payload: Vec<u8>,
 | 
				
			||||||
) -> Packet {
 | 
					    ) -> Packet {
 | 
				
			||||||
        let length = payload.len() as u16;
 | 
					        let length = payload.len() as u16;
 | 
				
			||||||
        let payload = into_compressed(compression, payload);
 | 
					        let payload = into_compressed(compression, payload);
 | 
				
			||||||
        Packet(
 | 
					        Packet(
 | 
				
			||||||
            Header(command.into(), offset, length, compression.into(), 0),
 | 
					            Header(command.into(), offset, length, compression.into(), 0),
 | 
				
			||||||
            payload,
 | 
					            payload,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Helper method for creating empty packets only containing the command code
 | 
					    /// Helper method for creating empty packets only containing the command code
 | 
				
			||||||
fn command_code_only(code: CommandCode) -> Packet {
 | 
					    fn command_code_only(code: CommandCode) -> Packet {
 | 
				
			||||||
        Packet(Header(code.into(), 0x0000, 0x0000, 0x0000, 0x0000), vec![])
 | 
					        Packet(Header(code.into(), 0x0000, 0x0000, 0x0000, 0x0000), vec![])
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Helper method for checking that a packet is empty and only contains a command code
 | 
					    /// Helper method for checking that a packet is empty and only contains a command code
 | 
				
			||||||
fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
 | 
					    fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
 | 
				
			||||||
        let Packet(Header(_, a, b, c, d), payload) = packet;
 | 
					        let Packet(Header(_, a, b, c, d), payload) = packet;
 | 
				
			||||||
        if !payload.is_empty() {
 | 
					        if !payload.is_empty() {
 | 
				
			||||||
            Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
 | 
					            Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
 | 
				
			||||||
| 
						 | 
					@ -320,18 +333,20 @@ fn check_command_only(packet: Packet) -> Option<TryFromPacketError> {
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            None
 | 
					            None
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Helper method for Packets into BitMapLinear*-Commands
 | 
					    /// Helper method for Packets into BitMapLinear*-Commands
 | 
				
			||||||
fn packet_into_linear_bitmap(
 | 
					    fn packet_into_linear_bitmap(
 | 
				
			||||||
        packet: Packet,
 | 
					        packet: Packet,
 | 
				
			||||||
) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
 | 
					    ) -> Result<(BitVec, CompressionCode), TryFromPacketError> {
 | 
				
			||||||
        let Packet(Header(_, _, length, sub, reserved), payload) = packet;
 | 
					        let Packet(Header(_, _, length, sub, reserved), payload) = packet;
 | 
				
			||||||
        if reserved != 0 {
 | 
					        if reserved != 0 {
 | 
				
			||||||
            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) {
 | 
				
			||||||
| 
						 | 
					@ -345,6 +360,7 @@ fn packet_into_linear_bitmap(
 | 
				
			||||||
            ));
 | 
					            ));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Ok((BitVec::from(&*payload), sub))
 | 
					        Ok((BitVec::from(&*payload), sub))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(feature = "c_api")]
 | 
					#[cfg(feature = "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,
 | 
				
			||||||
            ))
 | 
					            ))
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue