remove dependency on num

This commit is contained in:
Vinzenz Schroeter 2024-05-12 13:11:42 +02:00
parent 3b9586a69e
commit 62ca9037b6
11 changed files with 124 additions and 314 deletions

View file

@ -1,5 +1,4 @@
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
use crate::command_codes::{CommandCode, CompressionCode};
use crate::{BitVec, ByteGrid, CommandCode, CompressionCode, Header, Packet, PixelGrid, TILE_SIZE};
use crate::compression::{into_compressed, into_decompressed};
/// An origin marks the top left position of a window sent to the display.
@ -20,6 +19,7 @@ type Offset = u16;
type Brightness = u8;
/// A command to send to the display.
#[derive(Debug)]
pub enum Command {
/// Set all pixels to the off state
@ -69,7 +69,7 @@ impl Into<Packet> for Command {
),
Command::Brightness(brightness) => Packet(
Header(
CommandCode::Brightness.to_primitive(),
CommandCode::Brightness.into(),
0x00000,
0x0000,
0x0000,
@ -82,7 +82,7 @@ impl Into<Packet> for Command {
debug_assert_eq!(pixels.width % 8, 0);
Packet(
Header(
CommandCode::BitmapLinearWin.to_primitive(),
CommandCode::BitmapLinearWin.into(),
pixel_x / TILE_SIZE,
pixel_y,
pixels.width as u16 / TILE_SIZE,
@ -154,11 +154,11 @@ impl TryFrom<Packet> for Command {
fn try_from(value: Packet) -> Result<Self, Self::Error> {
let Packet(Header(command_u16, a, b, c, d), _) = value;
let command_code = match CommandCode::from_primitive(command_u16) {
None => {
let command_code = match CommandCode::try_from(command_u16) {
Err(_) => {
return Err(TryFromPacketError::InvalidCommand(command_u16));
}
Some(value) => value,
Ok(value) => value,
};
match command_code {
@ -242,13 +242,12 @@ fn bitmap_linear_into_packet(
payload: Vec<u8>,
) -> Packet {
let payload = into_compressed(compression, payload);
let compression = CompressionCode::to_primitive(&compression);
Packet(
Header(
command.to_primitive(),
command.into(),
offset,
payload.len() as u16,
compression,
compression.into(),
0,
),
payload,
@ -263,12 +262,12 @@ fn origin_size_payload(
) -> Packet {
let Origin(x, y) = origin;
let Size(w, h) = size;
Packet(Header(command.to_primitive(), x, y, w, h), payload.into())
Packet(Header(command.into(), x, y, w, h), payload.into())
}
fn command_code_only(code: CommandCode) -> Packet {
Packet(
Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000),
Header(code.into(), 0x0000, 0x0000, 0x0000, 0x0000),
vec![],
)
}
@ -306,9 +305,9 @@ fn packet_into_linear_bitmap(
payload.len(),
));
}
let sub = match CompressionCode::from_primitive(sub) {
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Some(value) => value,
let sub = match CompressionCode::try_from(sub) {
Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Ok(value) => value,
};
let payload = match into_decompressed(sub, payload) {
None => return Err(TryFromPacketError::DecompressionFailed),

49
src/command_code.rs Normal file
View file

@ -0,0 +1,49 @@
use CommandCode::*;
/// The codes used for the commands. See the documentation on the corresponding commands.
#[repr(u16)]
#[derive(Debug, Copy, Clone)]
pub enum CommandCode {
Clear = 0x0002,
Cp437Data = 0x0003,
CharBrightness = 0x0005,
Brightness = 0x0007,
HardReset = 0x000b,
FadeOut = 0x000d,
#[deprecated]
BitmapLegacy = 0x0010,
BitmapLinear = 0x0012,
BitmapLinearWin = 0x0013,
BitmapLinearAnd = 0x0014,
BitmapLinearOr = 0x0015,
BitmapLinearXor = 0x0016,
}
impl Into<u16> for CommandCode {
fn into(self) -> u16 {
self as u16
}
}
impl TryFrom<u16> for CommandCode {
type Error = ();
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
value if value == Clear as u16 => Ok(Clear),
value if value == Cp437Data as u16 => Ok(Cp437Data),
value if value == CharBrightness as u16 => Ok(CharBrightness),
value if value == Brightness as u16 => Ok(Brightness),
value if value == HardReset as u16 => Ok(HardReset),
value if value == FadeOut as u16 => Ok(FadeOut),
#[allow(deprecated)]
value if value == BitmapLegacy as u16 => Ok(BitmapLegacy),
value if value == BitmapLinear as u16 => Ok(BitmapLinear),
value if value == BitmapLinearWin as u16 => Ok(BitmapLinearWin),
value if value == BitmapLinearAnd as u16 => Ok(BitmapLinearAnd),
value if value == BitmapLinearOr as u16 => Ok(BitmapLinearOr),
value if value == BitmapLinearXor as u16 => Ok(BitmapLinearXor),
_ => Err(())
}
}
}

View file

@ -1,62 +0,0 @@
use num::{FromPrimitive, ToPrimitive};
use num_derive::{FromPrimitive, ToPrimitive};
/// The codes used for the commands. See the documentation on the corresponding commands.
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive, Copy, Clone)]
pub enum CommandCode {
Clear = 0x0002,
Cp437Data = 0x0003,
CharBrightness = 0x0005,
Brightness = 0x0007,
HardReset = 0x000b,
FadeOut = 0x000d,
#[deprecated]
BitmapLegacy = 0x0010,
BitmapLinear = 0x0012,
BitmapLinearWin = 0x0013,
BitmapLinearAnd = 0x0014,
BitmapLinearOr = 0x0015,
BitmapLinearXor = 0x0016,
}
impl CommandCode {
/// convert u16 to CommandCode enum
pub fn from_primitive(value: u16) -> Option<Self> {
FromPrimitive::from_u16(value)
}
/// convert CommandCode enum to u16
pub fn to_primitive(&self) -> u16 {
ToPrimitive::to_u16(self).unwrap()
}
}
/// Specifies the kind of compression to use. Availability depends on features.
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive, Clone, Copy)]
pub enum CompressionCode {
None = 0x0,
#[cfg(feature = "compression-gz")]
Gz = 0x677a,
#[cfg(feature = "compression-bz")]
Bz = 0x627a,
#[cfg(feature = "compression-lz")]
Lz = 0x6c7a,
#[cfg(feature = "compression-zs")]
Zs = 0x7a73,
}
impl CompressionCode {
/// convert CompressionCode enum to u16
pub fn to_primitive(&self) -> u16 {
ToPrimitive::to_u16(self).unwrap()
}
/// Convert u16 to CommandCode enum.
///
/// returns: None if the provided value is not one of the valid enum values.
pub fn from_primitive(value: u16) -> Option<Self> {
FromPrimitive::from_u16(value)
}
}

View file

@ -1,5 +1,5 @@
#[cfg(feature = "compression")]
use std::io::{Read, Write};
#[cfg(feature = "compression-bz")]
use bzip2::read::{BzDecoder, BzEncoder};
#[cfg(feature = "compression-gz")]
@ -16,7 +16,7 @@ pub(crate) fn into_decompressed(
payload: Payload,
) -> Option<Payload> {
match kind {
CompressionCode::None => Some(payload),
CompressionCode::Uncompressed => Some(payload),
#[cfg(feature = "compression-gz")]
CompressionCode::Gz => {
let mut decoder = GzDecoder::new(&*payload);
@ -67,7 +67,7 @@ pub(crate) fn into_compressed(
payload: Payload,
) -> Payload {
match kind {
CompressionCode::None => payload,
CompressionCode::Uncompressed => payload,
#[cfg(feature = "compression-gz")]
CompressionCode::Gz => {
let mut encoder =

41
src/compression_code.rs Normal file
View file

@ -0,0 +1,41 @@
use CompressionCode::*;
/// Specifies the kind of compression to use. Availability depends on features.
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
pub enum CompressionCode {
Uncompressed = 0x0,
#[cfg(feature = "compression-gz")]
Gz = 0x677a,
#[cfg(feature = "compression-bz")]
Bz = 0x627a,
#[cfg(feature = "compression-lz")]
Lz = 0x6c7a,
#[cfg(feature = "compression-zs")]
Zs = 0x7a73,
}
impl Into<u16> for CompressionCode {
fn into(self) -> u16 {
self as u16
}
}
impl TryFrom<u16> for CompressionCode {
type Error = ();
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
value if value == Uncompressed as u16 => Ok(Uncompressed),
#[cfg(feature = "compression-gz")]
value if value == Gz as u16 => Ok(Gz),
#[cfg(feature = "compression-bz")]
value if value == Bz as u16 => Ok(Bz),
#[cfg(feature = "compression-lz")]
value if value == Lz as u16 => Ok(Lz),
#[cfg(feature = "compression-zs")]
value if value == Zs as u16 => Ok(Zs),
_ => Err(())
}
}
}

View file

@ -1,19 +1,21 @@
pub use crate::bit_vec::BitVec;
pub use crate::byte_grid::ByteGrid;
pub use crate::command::{Command, Origin, Size};
pub use crate::command_codes::{CommandCode, CompressionCode};
pub use crate::connection::Connection;
pub use crate::packet::{Header, Packet, Payload};
pub use crate::pixel_grid::PixelGrid;
pub use crate::command_code::CommandCode;
pub use crate::compression_code::CompressionCode;
mod bit_vec;
mod byte_grid;
mod command;
mod command_codes;
mod command_code;
mod compression;
mod connection;
mod packet;
mod pixel_grid;
mod compression_code;
/// size of a single tile in one dimension
pub const TILE_SIZE: u16 = 8;

View file

@ -9,7 +9,7 @@ pub type Payload = Vec<u8>;
#[derive(Debug)]
pub struct Packet(pub Header, pub Payload);
impl Into<Vec<u8>> for Packet {
impl Into<Payload> for Packet {
fn into(self) -> Vec<u8> {
let Packet(Header(mode, a, b, c, d), payload) = self;
@ -33,7 +33,7 @@ fn u16_from_be_slice(slice: &[u8]) -> u16 {
u16::from_be_bytes(bytes)
}
impl From<Vec<u8>> for Packet {
impl From<Payload> for Packet {
fn from(value: Vec<u8>) -> Self {
let mode = u16_from_be_slice(&value[0..=1]);
let a = u16_from_be_slice(&value[2..=3]);