move containers into own mod

This commit is contained in:
Vinzenz Schroeter 2025-03-08 11:32:12 +01:00
parent e3fc56c200
commit 8022b65991
11 changed files with 49 additions and 54 deletions

View file

@ -136,12 +136,6 @@ pub enum TryFromPacketError {
InvalidUtf8(#[from] std::string::FromUtf8Error),
}
macro_rules! packet_to_command_case {
($T:tt, $packet:ident) => {
paste! {}
};
}
impl TryFrom<Packet> for TypedCommand {
type Error = TryFromPacketError;

View file

@ -1,9 +1,5 @@
use crate::data_ref::DataRef;
use crate::BitVec;
use crate::*;
use ::bitvec::order::Msb0;
use ::bitvec::prelude::BitSlice;
use ::bitvec::slice::IterMut;
use ::bitvec::{order::Msb0, prelude::BitSlice, slice::IterMut};
/// A fixed-size 2D grid of booleans.
///
@ -72,12 +68,16 @@ impl Bitmap {
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
pub fn load(width: usize, height: usize, data: &[u8]) -> Result<Self, LoadBitmapError> {
pub fn load(
width: usize,
height: usize,
data: &[u8],
) -> Result<Self, LoadBitmapError> {
if width % 8 != 0 {
return Err(LoadBitmapError::InvalidWidth)
return Err(LoadBitmapError::InvalidWidth);
}
if data.len() != height * width / 8 {
return Err(LoadBitmapError::InvalidDataSize)
return Err(LoadBitmapError::InvalidDataSize);
}
Ok(Self {
width,
@ -94,16 +94,19 @@ impl Bitmap {
///
/// In those cases, an Err is returned.
/// Otherwise, this returns a [Bitmap] that contains the provided data.
pub fn from_bitvec(width: usize, bit_vec: BitVec) -> Result<Self, LoadBitmapError> {
pub fn from_bitvec(
width: usize,
bit_vec: BitVec,
) -> Result<Self, LoadBitmapError> {
if width % 8 != 0 {
return Err(LoadBitmapError::InvalidWidth)
return Err(LoadBitmapError::InvalidWidth);
}
let len = bit_vec.len();
let height = len / width;
if len % width != 0 {
return Err(LoadBitmapError::InvalidDataSize)
return Err(LoadBitmapError::InvalidDataSize);
}
Ok(Self {
width,
height,
@ -236,8 +239,7 @@ impl TryFrom<&ValueGrid<bool>> for Bitmap {
///
/// Returns Err if the width of `value` is not dividable by 8
fn try_from(value: &ValueGrid<bool>) -> Result<Self, Self::Error> {
let mut result = Self::new(value.width(), value.height())
.ok_or(())?;
let mut result = Self::new(value.width(), value.height()).ok_or(())?;
for (mut to, from) in result.iter_mut().zip(value.iter()) {
*to = *from;
}
@ -280,8 +282,10 @@ impl<'t> Iterator for IterRows<'t> {
pub enum LoadBitmapError {
#[error("The provided width is not divisible by 8.")]
InvalidWidth,
#[error("The provided data has an incorrect size for the provided dimensions.")]
InvalidDataSize
#[error(
"The provided data has an incorrect size for the provided dimensions."
)]
InvalidDataSize,
}
#[cfg(test)]

View file

@ -1,7 +1,4 @@
use crate::brightness::Brightness;
use crate::grid::Grid;
use crate::value_grid::ValueGrid;
use crate::ByteGrid;
use crate::{brightness::Brightness, grid::Grid, ByteGrid, ValueGrid};
/// A grid containing brightness values.
///
@ -65,8 +62,7 @@ impl TryFrom<ByteGrid> for BrightnessGrid {
#[cfg(test)]
mod tests {
use crate::value_grid::ValueGrid;
use crate::{Brightness, BrightnessGrid, DataRef, Grid};
use crate::{Brightness, BrightnessGrid, DataRef, Grid, ValueGrid};
#[test]
fn to_u8_grid() {

View file

@ -1,9 +1,9 @@
use crate::Grid;
use crate::{Grid, ValueGrid};
/// A grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
pub type Cp437Grid = crate::value_grid::ValueGrid<u8>;
pub type Cp437Grid = ValueGrid<u8>;
/// The error occurring when loading an invalid character
#[derive(Debug, PartialEq, thiserror::Error)]

21
src/containers/mod.rs Normal file
View file

@ -0,0 +1,21 @@
mod bitmap;
mod bit_vec;
mod brightness_grid;
mod byte_grid;
mod char_grid;
mod cp437_grid;
mod data_ref;
mod value_grid;
pub use bit_vec::{bitvec, BitVec};
pub use bitmap::Bitmap;
pub use cp437_grid::Cp437Grid;
pub use data_ref::DataRef;
pub use brightness_grid::BrightnessGrid;
pub use byte_grid::ByteGrid;
pub use char_grid::CharGrid;
pub use value_grid::{
IterGridRows, SetValueSeriesError, TryLoadValueGridError, Value, ValueGrid,
};

View file

@ -447,10 +447,7 @@ impl<'t, T: Value> Iterator for EnumerateGrid<'t, T> {
#[cfg(test)]
mod tests {
use crate::{
value_grid::{SetValueSeriesError, ValueGrid},
*,
};
use crate::{SetValueSeriesError, ValueGrid, *};
#[test]
fn fill() {

View file

@ -55,47 +55,30 @@
//! connection.send(command).expect("send failed");
//! ```
pub use crate::bit_vec::{bitvec, BitVec};
pub use crate::bitmap::Bitmap;
pub use crate::brightness::Brightness;
pub use crate::brightness_grid::BrightnessGrid;
pub use crate::byte_grid::ByteGrid;
pub use crate::char_grid::CharGrid;
pub use crate::commands::*;
pub use crate::compression_code::CompressionCode;
pub use crate::connections::*;
pub use crate::constants::*;
pub use crate::cp437_grid::Cp437Grid;
pub use crate::data_ref::DataRef;
pub use crate::containers::*;
pub use crate::grid::Grid;
pub use crate::origin::{Origin, Pixels, Tiles};
pub use crate::packet::{Header, Packet, Payload};
pub use crate::value_grid::{
IterGridRows, SetValueSeriesError, TryLoadValueGridError, Value, ValueGrid,
};
mod bit_vec;
mod bitmap;
mod brightness;
mod brightness_grid;
mod byte_grid;
mod char_grid;
mod commands;
mod command_code;
mod compression;
mod compression_code;
mod connections;
mod constants;
mod cp437_grid;
mod data_ref;
mod containers;
mod grid;
mod origin;
mod packet;
mod value_grid;
mod parser;
#[cfg(feature = "cp437")]
mod cp437;
mod parser;
#[cfg(feature = "cp437")]
pub use crate::cp437::Cp437Converter;