export some builtin traits,

brightness conversion fails for invalid values,

macros for wrapping attrs
This commit is contained in:
Vinzenz Schroeter 2025-06-16 14:56:24 +02:00
parent b218bd6474
commit 0894b15c65
21 changed files with 210 additions and 133 deletions

4
Cargo.lock generated
View file

@ -426,8 +426,7 @@ dependencies = [
[[package]] [[package]]
name = "servicepoint" name = "servicepoint"
version = "0.15.0" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint.git?branch=next#5b6f88b1682925b7669b25a7fb08806c4009bbfd"
checksum = "91a33bff7f9db5008748b23ca0c906c276fe00694390b681f004a55968a42cfe"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"bzip2", "bzip2",
@ -443,6 +442,7 @@ dependencies = [
name = "servicepoint_binding_uniffi" name = "servicepoint_binding_uniffi"
version = "0.15.0" version = "0.15.0"
dependencies = [ dependencies = [
"paste",
"servicepoint", "servicepoint",
"thiserror 2.0.11", "thiserror 2.0.11",
"uniffi", "uniffi",

View file

@ -19,10 +19,13 @@ uniffi = { version = "0.28.3", features = ["build"] }
[dependencies] [dependencies]
uniffi = { version = "0.28.3" } uniffi = { version = "0.28.3" }
thiserror = "2.0" thiserror = "2.0"
paste = "1.0.15"
[dependencies.servicepoint] [dependencies.servicepoint]
version = "0.15.0" version = "0.15.0"
features = ["all_compressions"] features = ["all_compressions"]
git = "https://git.berlin.ccc.de/servicepoint/servicepoint.git"
branch = "next"
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true

View file

@ -1,3 +1,4 @@
use crate::errors::ServicePointError;
use crate::UniffiCustomTypeConverter; use crate::UniffiCustomTypeConverter;
use servicepoint::Brightness; use servicepoint::Brightness;
@ -10,7 +11,8 @@ impl UniffiCustomTypeConverter for Brightness {
where where
Self: Sized, Self: Sized,
{ {
Ok(Brightness::saturating_from(val)) Ok(Brightness::try_from(val)
.map_err(|value| ServicePointError::InvalidBrightness { value })?)
} }
fn from_custom(obj: Self) -> Self::Builtin { fn from_custom(obj: Self) -> Self::Builtin {

View file

@ -1,13 +1,14 @@
use crate::{ use crate::{
commands::wrap_command, compression_code::CompressionCode, commands::wrap_command, compression_code::CompressionCode,
containers::bitmap::Bitmap, macros::wrap_object, containers::bitmap::Bitmap, macros::wrap_object_attr_wrapped,
}; };
use servicepoint::Origin; use servicepoint::Origin;
use std::sync::Arc; use std::sync::Arc;
wrap_object!(BitmapCommand);
wrap_command!(BitmapCommand); wrap_command!(BitmapCommand);
wrap_object_attr_wrapped!(BitmapCommand, bitmap, Bitmap);
#[uniffi::export] #[uniffi::export]
impl BitmapCommand { impl BitmapCommand {
#[uniffi::constructor] #[uniffi::constructor]

View file

@ -1,11 +1,11 @@
use crate::{ use crate::{
commands::wrap_command, compression_code::CompressionCode, commands::wrap_command, compression_code::CompressionCode,
containers::bitvec::BitVec, macros::wrap_object, containers::bitvec::BitVec, macros::wrap_object_attr_wrapped,
}; };
use std::sync::Arc; use std::sync::Arc;
wrap_object!(BitVecCommand);
wrap_command!(BitVecCommand); wrap_command!(BitVecCommand);
wrap_object_attr_wrapped!(BitVecCommand, bitvec, BitVec);
#[uniffi::export] #[uniffi::export]
impl BitVecCommand { impl BitVecCommand {

View file

@ -1,12 +1,12 @@
use crate::{ use crate::{
commands::wrap_command, containers::brightness_grid::BrightnessGrid, commands::wrap_command, containers::brightness_grid::BrightnessGrid,
macros::wrap_object, macros::wrap_object_attr_wrapped,
}; };
use servicepoint::Origin; use servicepoint::Origin;
use std::sync::Arc; use std::sync::Arc;
wrap_object!(BrightnessGridCommand);
wrap_command!(BrightnessGridCommand); wrap_command!(BrightnessGridCommand);
wrap_object_attr_wrapped!(BrightnessGridCommand, grid, BrightnessGrid);
#[uniffi::export] #[uniffi::export]
impl BrightnessGridCommand { impl BrightnessGridCommand {

View file

@ -1,10 +1,8 @@
use crate::{commands::wrap_command, macros::wrap_object};
use std::sync::Arc; use std::sync::Arc;
macro_rules! command_code_only_command { macro_rules! command_code_only_command {
($command_struct:ident) => { ($command_struct:ident) => {
wrap_object!($command_struct); crate::commands::wrap_command!($command_struct);
wrap_command!($command_struct);
#[uniffi::export] #[uniffi::export]
impl $command_struct { impl $command_struct {
@ -18,5 +16,4 @@ macro_rules! command_code_only_command {
command_code_only_command!(ClearCommand); command_code_only_command!(ClearCommand);
command_code_only_command!(HardResetCommand); command_code_only_command!(HardResetCommand);
command_code_only_command!(BitmapLegacyCommand);
command_code_only_command!(FadeOutCommand); command_code_only_command!(FadeOutCommand);

View file

@ -1,12 +1,12 @@
use crate::{ use crate::{
commands::wrap_command, containers::char_grid::CharGrid, commands::wrap_command, containers::char_grid::CharGrid,
macros::wrap_object, macros::wrap_object_attr_wrapped,
}; };
use servicepoint::Origin; use servicepoint::Origin;
use std::sync::Arc; use std::sync::Arc;
wrap_object!(CharGridCommand);
wrap_command!(CharGridCommand); wrap_command!(CharGridCommand);
wrap_object_attr_wrapped!(CharGridCommand, grid, CharGrid);
#[uniffi::export] #[uniffi::export]
impl CharGridCommand { impl CharGridCommand {

View file

@ -1,12 +1,12 @@
use crate::{ use crate::{
commands::wrap_command, containers::cp437_grid::Cp437Grid, commands::wrap_command, containers::cp437_grid::Cp437Grid,
macros::wrap_object, macros::wrap_object_attr_wrapped,
}; };
use servicepoint::Origin; use servicepoint::Origin;
use std::sync::Arc; use std::sync::Arc;
wrap_object!(Cp437GridCommand);
wrap_command!(Cp437GridCommand); wrap_command!(Cp437GridCommand);
wrap_object_attr_wrapped!(Cp437GridCommand, grid, Cp437Grid);
#[uniffi::export] #[uniffi::export]
impl Cp437GridCommand { impl Cp437GridCommand {

View file

@ -1,8 +1,7 @@
use crate::{commands::wrap_command, macros::wrap_object}; use crate::commands::wrap_command;
use servicepoint::Brightness; use servicepoint::Brightness;
use std::sync::Arc; use std::sync::Arc;
wrap_object!(GlobalBrightnessCommand);
wrap_command!(GlobalBrightnessCommand); wrap_command!(GlobalBrightnessCommand);
#[uniffi::export] #[uniffi::export]

View file

@ -8,35 +8,59 @@ mod global_brightness;
use std::sync::Arc; use std::sync::Arc;
use crate::packet::Packet;
pub use bitmap::BitmapCommand; pub use bitmap::BitmapCommand;
pub use bitvec::{BinaryOperation, BitVecCommand}; pub use bitvec::{BinaryOperation, BitVecCommand};
pub use brightness_grid::BrightnessGridCommand; pub use brightness_grid::BrightnessGridCommand;
pub use cc_only::{ pub use cc_only::{ClearCommand, FadeOutCommand, HardResetCommand};
BitmapLegacyCommand, ClearCommand, FadeOutCommand, HardResetCommand,
};
pub use char_grid::CharGridCommand; pub use char_grid::CharGridCommand;
pub use cp437::Cp437GridCommand; pub use cp437::Cp437GridCommand;
#[uniffi::export] #[uniffi::export]
pub trait Command: Sync + Send { pub trait Command: Sync + Send {
fn as_packet(&self) -> Option<Arc<Packet>>; fn as_packet(
&self,
) -> Result<Arc<crate::packet::Packet>, crate::errors::ServicePointError>;
} }
macro_rules! wrap_command { macro_rules! wrap_command {
($command:ident) => { ($command:ident) => {
#[uniffi::export] crate::macros::wrap_object!($command);
impl $command {}
impl crate::commands::Command for $command { #[uniffi::export]
fn as_packet(&self) -> Option<Arc<crate::packet::Packet>> { impl $command {
fn as_packet(
&self,
) -> Result<
Arc<crate::packet::Packet>,
crate::errors::ServicePointError,
> {
self.read() self.read()
.clone() .clone()
.try_into() .try_into()
.map(crate::packet::Packet::internal_new) .map(crate::packet::Packet::internal_new)
.ok() .map_err(|_| {
crate::errors::ServicePointError::InvalidPacket
})
}
fn as_generic(
&self,
) -> ::std::sync::Arc<dyn crate::commands::Command> {
::std::sync::Arc::new(self.clone())
}
}
impl crate::commands::Command for $command {
fn as_packet(
&self,
) -> Result<
Arc<crate::packet::Packet>,
crate::errors::ServicePointError,
> {
$command::as_packet(&self)
} }
} }
}; };
} }
pub(crate) use wrap_command; pub(crate) use wrap_command;

View file

@ -1,3 +1,4 @@
use crate::commands::Command;
use crate::{errors::ServicePointError, packet::Packet}; use crate::{errors::ServicePointError, packet::Packet};
use servicepoint::UdpSocketExt; use servicepoint::UdpSocketExt;
use std::{net::UdpSocket, sync::Arc}; use std::{net::UdpSocket, sync::Arc};
@ -28,4 +29,11 @@ impl Connection {
error: "send failed".to_string(), error: "send failed".to_string(),
}) })
} }
pub fn send_command(
&self,
command: Arc<dyn Command>,
) -> Result<(), ServicePointError> {
self.send_packet(command.as_packet()?)
}
} }

View file

@ -1,4 +1,7 @@
use crate::macros::*; use crate::{
containers::{wrap_get_set_fill_2d, wrap_width_height},
macros::wrap_object,
};
use servicepoint::Grid; use servicepoint::Grid;
use std::{ops::Deref, sync::Arc}; use std::{ops::Deref, sync::Arc};
@ -22,18 +25,13 @@ impl Bitmap {
#[uniffi::constructor] #[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> { pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
// TODO: throw exception
Self::internal_new( Self::internal_new(
servicepoint::Bitmap::load(width as usize, height as usize, &data) servicepoint::Bitmap::load(width as usize, height as usize, &data)
.unwrap(), .unwrap(),
) )
} }
pub fn equals(&self, other: &Bitmap) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn copy_raw(&self) -> Vec<u8> { pub fn copy_raw(&self) -> Vec<u8> {
self.read().deref().into() self.read().deref().into()
} }

View file

@ -18,31 +18,21 @@ impl BitVec {
} }
pub fn set(&self, index: u64, value: bool) { pub fn set(&self, index: u64, value: bool) {
self.actual.write().unwrap().set(index as usize, value) self.write().set(index as usize, value)
} }
pub fn get(&self, index: u64) -> bool { pub fn get(&self, index: u64) -> bool {
self.actual self.read().get(index as usize).is_some_and(move |bit| *bit)
.read()
.unwrap()
.get(index as usize)
.is_some_and(move |bit| *bit)
} }
pub fn fill(&self, value: bool) { pub fn fill(&self, value: bool) {
self.actual.write().unwrap().fill(value) self.write().fill(value)
} }
pub fn len(&self) -> u64 { pub fn len(&self) -> u64 {
self.read().len() as u64 self.read().len() as u64
} }
pub fn equals(&self, other: &BitVec) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn copy_raw(&self) -> Vec<u8> { pub fn copy_raw(&self) -> Vec<u8> {
self.read().clone().into_vec() self.read().clone().into_vec()
} }

View file

@ -1,4 +1,7 @@
use crate::macros::*; use crate::{
containers::{wrap_get_set_fill_2d, wrap_width_height},
macros::wrap_object,
};
use servicepoint::{Brightness, Grid}; use servicepoint::{Brightness, Grid};
use std::{ops::Deref, sync::Arc}; use std::{ops::Deref, sync::Arc};
@ -28,12 +31,6 @@ impl BrightnessGrid {
) )
} }
pub fn equals(&self, other: &BrightnessGrid) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn copy_raw(&self) -> Vec<u8> { pub fn copy_raw(&self) -> Vec<u8> {
self.read().deref().into() self.read().deref().into()
} }

View file

@ -1,4 +1,7 @@
use crate::{containers::cp437_grid::Cp437Grid, macros::*}; use crate::{
containers::{cp437_grid::Cp437Grid, wrap_width_height},
macros::wrap_object,
};
use servicepoint::{Grid, SetValueSeriesError}; use servicepoint::{Grid, SetValueSeriesError};
use std::{convert::Into, sync::Arc}; use std::{convert::Into, sync::Arc};
@ -37,58 +40,39 @@ impl CharGrid {
value: String, value: String,
) -> Result<(), CharGridError> { ) -> Result<(), CharGridError> {
let value = Self::str_to_char(value)?; let value = Self::str_to_char(value)?;
self.actual self.write().set(x as usize, y as usize, value);
.write()
.unwrap()
.set(x as usize, y as usize, value);
Ok(()) Ok(())
} }
pub fn get(&self, x: u64, y: u64) -> String { pub fn get(&self, x: u64, y: u64) -> String {
self.actual self.read().get(x as usize, y as usize).into()
.read()
.unwrap()
.get(x as usize, y as usize)
.into()
} }
pub fn fill(&self, value: String) -> Result<(), CharGridError> { pub fn fill(&self, value: String) -> Result<(), CharGridError> {
let value = Self::str_to_char(value)?; let value = Self::str_to_char(value)?;
self.actual.write().unwrap().fill(value); self.write().fill(value);
Ok(()) Ok(())
} }
pub fn equals(&self, other: &CharGrid) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn as_string(&self) -> String { pub fn as_string(&self) -> String {
let grid = self.read(); let grid = self.read();
String::from(&*grid) String::from(&*grid)
} }
pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> { pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> {
self.actual self.write()
.write()
.unwrap()
.set_row(y as usize, &row.chars().collect::<Vec<_>>()) .set_row(y as usize, &row.chars().collect::<Vec<_>>())
.map_err(CharGridError::from) .map_err(CharGridError::from)
} }
pub fn set_col(&self, x: u64, col: String) -> Result<(), CharGridError> { pub fn set_col(&self, x: u64, col: String) -> Result<(), CharGridError> {
self.actual self.write()
.write()
.unwrap()
.set_row(x as usize, &col.chars().collect::<Vec<_>>()) .set_row(x as usize, &col.chars().collect::<Vec<_>>())
.map_err(CharGridError::from) .map_err(CharGridError::from)
} }
pub fn get_row(&self, y: u64) -> Result<String, CharGridError> { pub fn get_row(&self, y: u64) -> Result<String, CharGridError> {
self.actual self.read()
.read()
.unwrap()
.get_row(y as usize) .get_row(y as usize)
.map(String::from_iter) .map(String::from_iter)
.ok_or(CharGridError::OutOfBounds { .ok_or(CharGridError::OutOfBounds {
@ -98,9 +82,7 @@ impl CharGrid {
} }
pub fn get_col(&self, x: u64) -> Result<String, CharGridError> { pub fn get_col(&self, x: u64) -> Result<String, CharGridError> {
self.actual self.read()
.read()
.unwrap()
.get_col(x as usize) .get_col(x as usize)
.map(String::from_iter) .map(String::from_iter)
.ok_or(CharGridError::OutOfBounds { .ok_or(CharGridError::OutOfBounds {

View file

@ -1,4 +1,8 @@
use crate::{containers::char_grid::CharGrid, macros::*}; use crate::{
containers::char_grid::CharGrid,
containers::{wrap_get_set_fill_2d, wrap_width_height},
macros::wrap_object,
};
use servicepoint::Grid; use servicepoint::Grid;
use std::{ops::Deref, sync::Arc}; use std::{ops::Deref, sync::Arc};
@ -28,12 +32,6 @@ impl Cp437Grid {
) )
} }
pub fn equals(&self, other: &Cp437Grid) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn copy_raw(&self) -> Vec<u8> { pub fn copy_raw(&self) -> Vec<u8> {
self.read().deref().into() self.read().deref().into()
} }

View file

@ -3,3 +3,44 @@ pub mod bitvec;
pub mod brightness_grid; pub mod brightness_grid;
pub mod char_grid; pub mod char_grid;
pub mod cp437_grid; pub mod cp437_grid;
macro_rules! wrap_width_height {
($t:ident) => {
#[uniffi::export]
impl $t {
pub fn width(&self) -> u64 {
self.read().width() as u64
}
pub fn height(&self) -> u64 {
self.read().height() as u64
}
}
};
}
pub(crate) use wrap_width_height;
macro_rules! wrap_get_set_fill_2d {
($t:ident, $contained:ident) => {
#[uniffi::export]
impl $t {
pub fn set(&self, x: u64, y: u64, value: $contained) {
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> $contained {
self.read().get(x as usize, y as usize)
}
pub fn fill(&self, value: $contained) {
self.actual.write().unwrap().fill(value)
}
}
};
}
pub(crate) use wrap_get_set_fill_2d;

View file

@ -4,4 +4,6 @@ pub enum ServicePointError {
IoError { error: String }, IoError { error: String },
#[error("The specified brightness value {value} is out of range")] #[error("The specified brightness value {value} is out of range")]
InvalidBrightness { value: u8 }, InvalidBrightness { value: u8 },
#[error("The provided packet is invalid or a conversion to packet failed")]
InvalidPacket,
} }

View file

@ -1,6 +1,7 @@
macro_rules! wrap_object { macro_rules! wrap_object {
($orig_t:ident, $new_t:ident) => { ($orig_t:ident, $new_t:ident) => {
#[derive(uniffi::Object)] #[derive(uniffi::Object)]
#[uniffi::export(Debug, Eq, Hash)]
pub struct $new_t { pub struct $new_t {
actual: ::std::sync::RwLock<::servicepoint::$orig_t>, actual: ::std::sync::RwLock<::servicepoint::$orig_t>,
} }
@ -10,9 +11,15 @@ macro_rules! wrap_object {
pub(crate) fn internal_new( pub(crate) fn internal_new(
actual: ::servicepoint::$orig_t, actual: ::servicepoint::$orig_t,
) -> ::std::sync::Arc<Self> { ) -> ::std::sync::Arc<Self> {
::std::sync::Arc::new(Self { ::std::sync::Arc::new(Self::internal_new_noarc(actual))
}
pub(crate) fn internal_new_noarc(
actual: ::servicepoint::$orig_t,
) -> Self {
Self {
actual: ::std::sync::RwLock::new(actual), actual: ::std::sync::RwLock::new(actual),
}) }
} }
pub(crate) fn read( pub(crate) fn read(
@ -37,51 +44,85 @@ macro_rules! wrap_object {
Self::internal_new(other.read().clone()) Self::internal_new(other.read().clone())
} }
} }
impl Clone for $new_t {
fn clone(&self) -> Self {
Self::internal_new_noarc(self.read().clone())
}
}
impl ::std::fmt::Debug for $new_t {
fn fmt(
&self,
f: &mut ::std::fmt::Formatter,
) -> Result<(), std::fmt::Error> {
f.write_fmt(format_args!("{:?}", self.read()))
}
}
impl PartialEq for $new_t {
fn eq(&self, other: &$new_t) -> bool {
*self.read() == *other.read()
}
}
impl Eq for $new_t {}
impl ::std::hash::Hash for $new_t {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
::std::hash::Hash::hash(&*self.read(), state)
}
}
}; };
($t:ident) => { ($t:ident) => {
wrap_object!($t, $t); crate::macros::wrap_object!($t, $t);
}; };
} }
pub(crate) use wrap_object; macro_rules! wrap_object_attr_wrapped_get {
($object:ident, $attr_name:ident, $attr_type:ident, $fun_name:ident) => {
macro_rules! wrap_width_height {
($t:ident) => {
#[uniffi::export] #[uniffi::export]
impl $t { impl $object {
pub fn width(&self) -> u64 { pub fn $fun_name(&self) -> ::std::sync::Arc<$attr_type> {
self.read().width() as u64 $attr_type::internal_new(self.read().$attr_name.clone())
} }
pub fn height(&self) -> u64 {
self.read().height() as u64
} }
};
($object:ident, $attr_name:ident, $attr_type:ident) => {
paste::paste!{
crate::macros::wrap_object_attr_wrapped_get!($object, $attr_name, $attr_type, [<get_ $attr_name>]);
} }
}; };
} }
pub(crate) use wrap_width_height; macro_rules! wrap_object_attr_wrapped_set {
($object:ident, $attr_name:ident, $attr_type:ident, $fun_name:ident) => {
macro_rules! wrap_get_set_fill_2d {
($t:ident, $contained:ident) => {
#[uniffi::export] #[uniffi::export]
impl $t { impl $object {
pub fn set(&self, x: u64, y: u64, value: $contained) { pub fn $fun_name(&self, $attr_name: ::std::sync::Arc<$attr_type>) {
self.actual self.write().$attr_name = $attr_name.read().clone();
.write()
.unwrap()
.set(x as usize, y as usize, value)
} }
pub fn get(&self, x: u64, y: u64) -> $contained {
self.read().get(x as usize, y as usize)
}
pub fn fill(&self, value: $contained) {
self.actual.write().unwrap().fill(value)
} }
};
($object:ident, $attr_name:ident, $attr_type:ident) => {
paste::paste!{
crate::macros::wrap_object_attr_wrapped_set!($object, $attr_name, $attr_type, [<set_ $attr_name>]);
} }
}; };
} }
pub(crate) use wrap_get_set_fill_2d; macro_rules! wrap_object_attr_wrapped {
($object:ident, $attr_name:ident, $attr_type:ident) => {
crate::macros::wrap_object_attr_wrapped_get!(
$object, $attr_name, $attr_type
);
crate::macros::wrap_object_attr_wrapped_set!(
$object, $attr_name, $attr_type
);
};
}
pub(crate) use {
wrap_object, wrap_object_attr_wrapped, wrap_object_attr_wrapped_get,
wrap_object_attr_wrapped_set,
};

View file

@ -1,12 +1,6 @@
use crate::macros::wrap_object; use crate::macros::{wrap_object, wrap_object_attr_wrapped};
use std::sync::Arc;
wrap_object!(Packet); wrap_object!(Packet);
wrap_object!(Header); wrap_object!(Header);
#[uniffi::export] wrap_object_attr_wrapped!(Packet, header, Header);
impl Packet {
pub fn get_header(&self) -> Arc<Header> {
Header::internal_new(self.read().header)
}
}