rename BrightnessCommand to GlobalBrightnessCommand, name fields in error struct
Some checks failed
Rust / build (pull_request) Failing after 1m17s

This commit is contained in:
Vinzenz Schroeter 2025-03-27 18:43:09 +01:00
parent b3bf57301a
commit 739675a9f5
11 changed files with 151 additions and 75 deletions

View file

@ -4,8 +4,8 @@
use clap::Parser;
use rand::Rng;
use servicepoint::{
Bitmap, BitmapCommand, Brightness, BrightnessCommand, BrightnessGrid,
BrightnessGridCommand, Connection, Grid, Origin, UdpConnection,
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
Connection, GlobalBrightnessCommand, Grid, Origin, UdpConnection,
TILE_HEIGHT, TILE_WIDTH,
};
use std::time::Duration;
@ -38,7 +38,7 @@ fn main() {
// set all pixels to the same random brightness
let mut rng = rand::thread_rng();
let command: BrightnessCommand = rng.r#gen::<Brightness>().into();
let command: GlobalBrightnessCommand = rng.r#gen::<Brightness>().into();
connection.send(command).unwrap();
// continuously update random windows to new random brightness

View file

@ -15,7 +15,7 @@ use rand::{
///
/// let b = Brightness::try_from(7).unwrap();
/// # let connection = FakeConnection;
/// let result = connection.send(BrightnessCommand::from(b));
/// let result = connection.send(GlobalBrightnessCommand::from(b));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[repr(transparent)]

View file

@ -80,7 +80,7 @@ impl TryFrom<Packet> for BitVecCommand {
Header {
command_code,
a: offset,
b: length,
b: expected_len,
c: sub,
d: reserved,
..
@ -106,11 +106,11 @@ impl TryFrom<Packet> for BitVecCommand {
None => return Err(TryFromPacketError::DecompressionFailed),
Some(value) => value,
};
if payload.len() != length as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
if payload.len() != expected_len as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected: expected_len as usize,
actual: payload.len(),
});
}
Ok(Self {
offset: offset as Offset,
@ -324,10 +324,10 @@ mod tests {
};
assert_eq!(
TypedCommand::try_from(p),
Err(TryFromPacketError::UnexpectedPayloadSize(
420,
length as usize,
))
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 420,
actual: length as usize,
})
);
}

View file

@ -54,10 +54,10 @@ impl TryFrom<Packet> for BrightnessGridCommand {
let expected_size = width as usize * height as usize;
if payload.len() != expected_size {
return Err(TryFromPacketError::UnexpectedPayloadSize(
payload.len(),
expected_size,
));
return Err(TryFromPacketError::UnexpectedPayloadSize {
actual: payload.len(),
expected: expected_size,
});
}
let grid = ByteGrid::from_raw_parts_unchecked(
@ -86,9 +86,10 @@ impl From<BrightnessGridCommand> for TypedCommand {
#[cfg(test)]
mod tests {
use crate::{
commands,
commands::errors::TryFromPacketError,
commands::tests::{round_trip, TestImplementsCommand},
commands::{
errors::TryFromPacketError,
tests::{round_trip, TestImplementsCommand},
},
Brightness, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
TypedCommand,
};
@ -109,7 +110,7 @@ mod tests {
#[test]
fn packet_into_char_brightness_invalid() {
let grid = BrightnessGrid::new(2, 2);
let command = commands::BrightnessGridCommand {
let command = BrightnessGridCommand {
origin: Origin::ZERO,
grid,
};
@ -137,4 +138,21 @@ mod tests {
},
)
}
#[test]
fn invalid_size() {
let command: BrightnessGridCommand = BrightnessGrid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: packet.payload[..5].to_vec(),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
BrightnessGridCommand::try_from(packet)
);
}
}

View file

@ -57,12 +57,12 @@ impl TryFrom<Packet> for CharGridCommand {
let payload: Vec<_> =
String::from_utf8(payload.clone())?.chars().collect();
let expected_size = width as usize * height as usize;
if payload.len() != expected_size {
return Err(TryFromPacketError::UnexpectedPayloadSize(
expected_size,
payload.len(),
));
let expected = width as usize * height as usize;
if payload.len() != expected {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: payload.len(),
});
}
Ok(Self {
@ -96,7 +96,7 @@ mod tests {
use crate::{
commands::tests::{round_trip, TestImplementsCommand},
cp437::cp437_to_char,
CharGrid, CharGridCommand, Origin,
CharGrid, CharGridCommand, Origin, Packet, TryFromPacketError,
};
impl TestImplementsCommand for CharGridCommand {}
@ -127,4 +127,21 @@ mod tests {
},
)
}
#[test]
fn invalid_size() {
let command: CharGridCommand = CharGrid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: packet.payload[..5].to_vec(),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
CharGridCommand::try_from(packet)
);
}
}

View file

@ -65,12 +65,12 @@ impl TryFrom<Packet> for Cp437GridCommand {
check_command_code(command_code, CommandCode::Cp437Data)?;
let expected_size = width as usize * height as usize;
if payload.len() != expected_size {
return Err(TryFromPacketError::UnexpectedPayloadSize(
expected_size,
payload.len(),
));
let expected = width as usize * height as usize;
if payload.len() != expected {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: payload.len(),
});
}
Ok(Self {
@ -132,4 +132,21 @@ mod tests {
},
)
}
#[test]
fn invalid_size() {
let command: Cp437GridCommand = Cp437Grid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: packet.payload[..5].to_vec(),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
Cp437GridCommand::try_from(packet)
);
}
}

View file

@ -11,8 +11,15 @@ pub enum TryFromPacketError {
#[error(transparent)]
InvalidCommand(#[from] InvalidCommandCodeError),
/// the expected payload size was n, but size m was found
#[error("the expected payload size was {0}, but size {1} was found")]
UnexpectedPayloadSize(usize, usize),
#[error(
"the expected payload size was {actual}, but size {expected} was found"
)]
UnexpectedPayloadSize {
/// size of the provided payload
actual: usize,
/// expected size according to command or header values
expected: usize,
},
/// Header fields not needed for the command have been used.
///
/// Note that these commands would usually still work on the actual display.

View file

@ -44,10 +44,14 @@ impl From<FadeOutCommand> for TypedCommand {
#[cfg(test)]
mod tests {
use crate::command_code::CommandCode;
use crate::commands::errors::TryFromPacketError;
use crate::commands::tests::{round_trip, TestImplementsCommand};
use crate::{FadeOutCommand, Header, Packet, TypedCommand};
use crate::{
command_code::CommandCode,
commands::{
errors::TryFromPacketError,
tests::{round_trip, TestImplementsCommand},
},
FadeOutCommand, Header, Packet, TypedCommand,
};
impl TestImplementsCommand for FadeOutCommand {}
@ -90,7 +94,10 @@ mod tests {
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::UnexpectedPayloadSize(0, 2))
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 0,
actual: 2
})
));
}
}

View file

@ -11,17 +11,17 @@ use crate::{
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// let command = BrightnessCommand { brightness: Brightness::MAX };
/// let command = GlobalBrightnessCommand { brightness: Brightness::MAX };
/// connection.send(command).unwrap();
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BrightnessCommand {
pub struct GlobalBrightnessCommand {
/// the brightness to set all pixels to
pub brightness: Brightness,
}
impl From<BrightnessCommand> for Packet {
fn from(command: BrightnessCommand) -> Self {
impl From<GlobalBrightnessCommand> for Packet {
fn from(command: GlobalBrightnessCommand) -> Self {
Self {
header: Header {
command_code: CommandCode::Brightness.into(),
@ -35,7 +35,7 @@ impl From<BrightnessCommand> for Packet {
}
}
impl TryFrom<Packet> for BrightnessCommand {
impl TryFrom<Packet> for GlobalBrightnessCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
@ -54,10 +54,10 @@ impl TryFrom<Packet> for BrightnessCommand {
check_command_code(command_code, CommandCode::Brightness)?;
if payload.len() != 1 {
return Err(TryFromPacketError::UnexpectedPayloadSize(
1,
payload.len(),
));
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: payload.len(),
});
}
if a != 0 || b != 0 || c != 0 || d != 0 {
@ -71,15 +71,15 @@ impl TryFrom<Packet> for BrightnessCommand {
}
}
impl From<BrightnessCommand> for TypedCommand {
fn from(command: BrightnessCommand) -> Self {
impl From<GlobalBrightnessCommand> for TypedCommand {
fn from(command: GlobalBrightnessCommand) -> Self {
Self::Brightness(command)
}
}
impl From<Brightness> for BrightnessCommand {
impl From<Brightness> for GlobalBrightnessCommand {
fn from(brightness: Brightness) -> Self {
BrightnessCommand { brightness }
GlobalBrightnessCommand { brightness }
}
}
@ -87,18 +87,19 @@ impl From<Brightness> for BrightnessCommand {
mod tests {
use crate::{
command_code::CommandCode,
commands,
commands::errors::TryFromPacketError,
commands::tests::{round_trip, TestImplementsCommand},
Brightness, BrightnessCommand, Header, Packet, TypedCommand,
commands::{
errors::TryFromPacketError,
tests::{round_trip, TestImplementsCommand},
},
Brightness, GlobalBrightnessCommand, Header, Packet, TypedCommand,
};
impl TestImplementsCommand for BrightnessCommand {}
impl TestImplementsCommand for GlobalBrightnessCommand {}
#[test]
fn brightness_as_command() {
assert_eq!(
BrightnessCommand {
GlobalBrightnessCommand {
brightness: Brightness::MAX
},
Brightness::MAX.into()
@ -108,7 +109,7 @@ mod tests {
#[test]
fn round_trip_brightness() {
round_trip(
BrightnessCommand {
GlobalBrightnessCommand {
brightness: Brightness::try_from(6).unwrap(),
}
.into(),
@ -147,7 +148,10 @@ mod tests {
},
payload: vec!()
}),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 0))
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: 0
})
);
assert_eq!(
@ -161,13 +165,16 @@ mod tests {
},
payload: vec!(0, 0)
}),
Err(TryFromPacketError::UnexpectedPayloadSize(1, 2))
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: 2
})
);
}
#[test]
fn packet_into_brightness_invalid() {
let mut packet: Packet = commands::BrightnessCommand {
let mut packet: Packet = GlobalBrightnessCommand {
brightness: Brightness::MAX,
}
.into();
@ -182,8 +189,8 @@ mod tests {
#[test]
fn into_command() {
assert_eq!(
BrightnessCommand::from(Brightness::MIN),
BrightnessCommand {
GlobalBrightnessCommand::from(Brightness::MIN),
GlobalBrightnessCommand {
brightness: Brightness::MIN,
},
)

View file

@ -1,13 +1,13 @@
mod bitmap;
mod bitmap_legacy;
mod bitvec;
mod brightness;
mod brightness_grid;
mod char_grid;
mod clear;
mod cp437_grid;
mod errors;
mod fade_out;
mod global_brightness;
mod hard_reset;
mod typed;
@ -18,13 +18,13 @@ use std::fmt::Debug;
pub use bitmap::*;
pub use bitmap_legacy::*;
pub use bitvec::*;
pub use brightness::*;
pub use brightness_grid::*;
pub use char_grid::*;
pub use clear::*;
pub use cp437_grid::*;
pub use errors::*;
pub use fade_out::*;
pub use global_brightness::*;
pub use hard_reset::*;
pub use typed::*;
@ -61,7 +61,7 @@ pub use typed::*;
/// use servicepoint::*;
///
/// // create command
/// let command = BrightnessCommand{ brightness: Brightness::MAX };
/// let command = GlobalBrightnessCommand{ brightness: Brightness::MAX };
///
/// // turn command into Packet
/// let packet: Packet = command.clone().into();
@ -101,7 +101,10 @@ fn check_command_code_only(
if packet.header.command_code != u16::from(code) {
Some(InvalidCommandCodeError(packet.header.command_code).into())
} else if !payload.is_empty() {
Some(TryFromPacketError::UnexpectedPayloadSize(0, payload.len()))
Some(TryFromPacketError::UnexpectedPayloadSize {
expected: 0,
actual: payload.len(),
})
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {

View file

@ -1,7 +1,7 @@
use crate::{
command_code::CommandCode, commands::errors::TryFromPacketError,
BitVecCommand, BitmapCommand, BrightnessCommand, BrightnessGridCommand,
CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand,
BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand,
ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand,
HardResetCommand, Packet, TryIntoPacketError,
};
@ -17,7 +17,7 @@ pub enum TypedCommand {
CharGrid(CharGridCommand),
Cp437Grid(Cp437GridCommand),
Bitmap(BitmapCommand),
Brightness(BrightnessCommand),
Brightness(GlobalBrightnessCommand),
BrightnessGrid(BrightnessGridCommand),
BitVec(BitVecCommand),
HardReset(HardResetCommand),
@ -36,7 +36,7 @@ impl TryFrom<Packet> for TypedCommand {
TypedCommand::Clear(crate::ClearCommand::try_from(packet)?)
}
CommandCode::Brightness => TypedCommand::Brightness(
crate::BrightnessCommand::try_from(packet)?,
crate::GlobalBrightnessCommand::try_from(packet)?,
),
CommandCode::HardReset => TypedCommand::HardReset(
crate::HardResetCommand::try_from(packet)?,