servicepoint-binding-uniffi/src/command.rs
2025-09-28 23:13:20 +02:00

130 lines
4.1 KiB
Rust

use crate::bitvec::BitVec;
use crate::brightness_grid::BrightnessGrid;
use crate::char_grid::CharGrid;
use crate::compression_code::CompressionCode;
use crate::cp437_grid::Cp437Grid;
use crate::errors::ServicePointError;
use servicepoint::{BitVecCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, HardResetCommand, Origin};
use std::sync::Arc;
use crate::bitmap::Bitmap;
use crate::macros::wrap_uniffi_object;
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,
}