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 bffc905261
commit f4c7519658
21 changed files with 210 additions and 133 deletions

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 std::{ops::Deref, sync::Arc};
@ -22,18 +25,13 @@ impl Bitmap {
#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
// TODO: throw exception
Self::internal_new(
servicepoint::Bitmap::load(width as usize, height as usize, &data)
.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> {
self.read().deref().into()
}

View file

@ -18,31 +18,21 @@ impl BitVec {
}
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 {
self.actual
.read()
.unwrap()
.get(index as usize)
.is_some_and(move |bit| *bit)
self.read().get(index as usize).is_some_and(move |bit| *bit)
}
pub fn fill(&self, value: bool) {
self.actual.write().unwrap().fill(value)
self.write().fill(value)
}
pub fn len(&self) -> 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> {
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 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> {
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 std::{convert::Into, sync::Arc};
@ -37,58 +40,39 @@ impl CharGrid {
value: String,
) -> Result<(), CharGridError> {
let value = Self::str_to_char(value)?;
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value);
self.write().set(x as usize, y as usize, value);
Ok(())
}
pub fn get(&self, x: u64, y: u64) -> String {
self.actual
.read()
.unwrap()
.get(x as usize, y as usize)
.into()
self.read().get(x as usize, y as usize).into()
}
pub fn fill(&self, value: String) -> Result<(), CharGridError> {
let value = Self::str_to_char(value)?;
self.actual.write().unwrap().fill(value);
self.write().fill(value);
Ok(())
}
pub fn equals(&self, other: &CharGrid) -> bool {
let a = self.read();
let b = other.read();
*a == *b
}
pub fn as_string(&self) -> String {
let grid = self.read();
String::from(&*grid)
}
pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> {
self.actual
.write()
.unwrap()
self.write()
.set_row(y as usize, &row.chars().collect::<Vec<_>>())
.map_err(CharGridError::from)
}
pub fn set_col(&self, x: u64, col: String) -> Result<(), CharGridError> {
self.actual
.write()
.unwrap()
self.write()
.set_row(x as usize, &col.chars().collect::<Vec<_>>())
.map_err(CharGridError::from)
}
pub fn get_row(&self, y: u64) -> Result<String, CharGridError> {
self.actual
.read()
.unwrap()
self.read()
.get_row(y as usize)
.map(String::from_iter)
.ok_or(CharGridError::OutOfBounds {
@ -98,9 +82,7 @@ impl CharGrid {
}
pub fn get_col(&self, x: u64) -> Result<String, CharGridError> {
self.actual
.read()
.unwrap()
self.read()
.get_col(x as usize)
.map(String::from_iter)
.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 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> {
self.read().deref().into()
}

View file

@ -3,3 +3,44 @@ pub mod bitvec;
pub mod brightness_grid;
pub mod char_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;