move command to module, first separate types

This commit is contained in:
Vinzenz Schroeter 2025-06-13 11:47:23 +02:00
commit c29482ac56
5 changed files with 47 additions and 22 deletions

23
src/commands/cc_only.rs Normal file
View file

@ -0,0 +1,23 @@
use std::sync::Arc;
macro_rules! command_code_only_command {
($command_struct:ident) => {
#[derive(uniffi::Object)]
pub struct $command_struct;
#[uniffi::export]
impl $command_struct {
#[uniffi::constructor]
pub fn new() -> Arc<Self> {
const _: () =
assert!(size_of::<servicepoint::$command_struct>() == 0);
Arc::new($command_struct)
}
}
};
}
command_code_only_command!(ClearCommand);
command_code_only_command!(HardResetCommand);
command_code_only_command!(BitmapLegacyCommand);
command_code_only_command!(FadeOutCommand);

141
src/commands/command.rs Normal file
View file

@ -0,0 +1,141 @@
use crate::{
compression_code::CompressionCode,
containers::{
bitmap::Bitmap, bitvec::BitVec, brightness_grid::BrightnessGrid,
char_grid::CharGrid, cp437_grid::Cp437Grid,
},
errors::ServicePointError,
macros::wrap_uniffi_object,
};
use servicepoint::{
BitVecCommand, BrightnessGridCommand, CharGridCommand, ClearCommand,
Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand,
HardResetCommand, Origin,
};
use std::sync::Arc;
wrap_uniffi_object!(TypedCommand, Command);
#[uniffi::export]
impl Command {
#[uniffi::constructor]
pub fn clear() -> Arc<Self> {
Self::internal_new(ClearCommand.into())
}
#[uniffi::constructor]
pub fn brightness(brightness: u8) -> Result<Arc<Self>, ServicePointError> {
servicepoint::Brightness::try_from(brightness)
.map_err(move |value| ServicePointError::InvalidBrightness {
value,
})
.map(GlobalBrightnessCommand::from)
.map(servicepoint::TypedCommand::Brightness)
.map(Self::internal_new)
}
#[uniffi::constructor]
pub fn fade_out() -> Arc<Self> {
Self::internal_new(FadeOutCommand.into())
}
#[uniffi::constructor]
pub fn hard_reset() -> Arc<Self> {
Self::internal_new(HardResetCommand.into())
}
#[uniffi::constructor]
pub fn bitmap_linear_win(
offset_x: u64,
offset_y: u64,
bitmap: &Arc<Bitmap>,
compression: CompressionCode,
) -> Arc<Self> {
let origin = Origin::new(offset_x as usize, offset_y as usize);
let bitmap = bitmap.actual.read().unwrap().clone();
let actual = servicepoint::BitmapCommand {
origin,
bitmap,
compression: servicepoint::CompressionCode::try_from(
compression as u16,
)
.unwrap(),
};
Self::internal_new(actual.into())
}
#[uniffi::constructor]
pub fn char_brightness(
offset_x: u64,
offset_y: u64,
grid: &Arc<BrightnessGrid>,
) -> Arc<Self> {
let origin = Origin::new(offset_x as usize, offset_y as usize);
let grid = grid.actual.read().unwrap().clone();
let actual = BrightnessGridCommand { origin, grid };
Self::internal_new(actual.into())
}
#[uniffi::constructor]
pub fn bitmap_linear(
offset: u64,
bitvec: &Arc<BitVec>,
compression: CompressionCode,
operation: BinaryOperation,
) -> Arc<Self> {
let bitvec = bitvec.actual.read().unwrap().clone();
let actual = BitVecCommand {
offset: offset as usize,
bitvec,
compression: servicepoint::CompressionCode::try_from(
compression as u16,
)
.unwrap(),
operation: match operation {
BinaryOperation::Overwrite => {
servicepoint::BinaryOperation::Overwrite
}
BinaryOperation::And => servicepoint::BinaryOperation::And,
BinaryOperation::Or => servicepoint::BinaryOperation::Or,
BinaryOperation::Xor => servicepoint::BinaryOperation::Xor,
},
};
Self::internal_new(actual.into())
}
#[uniffi::constructor]
pub fn cp437_data(
offset_x: u64,
offset_y: u64,
grid: &Arc<Cp437Grid>,
) -> Arc<Self> {
let origin = Origin::new(offset_x as usize, offset_y as usize);
let grid = grid.actual.read().unwrap().clone();
let actual = Cp437GridCommand { origin, grid };
Self::internal_new(actual.into())
}
#[uniffi::constructor]
pub fn utf8_data(
offset_x: u64,
offset_y: u64,
grid: &Arc<CharGrid>,
) -> Arc<Self> {
let origin = Origin::new(offset_x as usize, offset_y as usize);
let grid = grid.actual.read().unwrap().clone();
let actual = CharGridCommand { origin, grid };
Self::internal_new(actual.into())
}
pub fn equals(&self, other: &Command) -> bool {
*self.actual.read().unwrap() == *other.actual.read().unwrap()
}
}
#[derive(uniffi::Enum)]
pub enum BinaryOperation {
Overwrite,
And,
Or,
Xor,
}

3
src/commands/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod command;
pub mod cc_only;

View file

@ -1,12 +1,6 @@
use std::{
net::UdpSocket,
sync::Arc
};
use crate::{commands::command::Command, errors::ServicePointError};
use servicepoint::UdpSocketExt;
use crate::{
command::Command,
errors::ServicePointError
};
use std::{net::UdpSocket, sync::Arc};
#[derive(uniffi::Object)]
pub struct Connection {
@ -25,10 +19,10 @@ impl Connection {
}
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
self.actual.send_command(command.actual.read().unwrap().clone()).ok_or_else(|| {
ServicePointError::IoError {
self.actual
.send_command(command.actual.read().unwrap().clone())
.ok_or_else(|| ServicePointError::IoError {
error: "send failed".to_string(),
}
})
})
}
}

View file

@ -1,7 +1,6 @@
uniffi::setup_scaffolding!();
mod brightness;
mod command;
mod commands;
mod compression_code;
mod connection;