Skip to content

Commit

Permalink
add uniffi CP437Grid, equals
Browse files Browse the repository at this point in the history
add equals method

add uniffi copy_raw method
  • Loading branch information
kaesaecracker committed Nov 13, 2024
1 parent 03aa432 commit 9553d9f
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 2 deletions.

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion crates/servicepoint_binding_uniffi/src/bitmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use servicepoint::Grid;
use servicepoint::{DataRef, Grid};
use std::sync::{Arc, RwLock};

#[derive(uniffi::Object)]
Expand Down Expand Up @@ -64,4 +64,14 @@ impl Bitmap {
pub fn height(&self) -> u64 {
self.actual.read().unwrap().height() as u64
}

pub fn equals(&self, other: &Bitmap) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();
*a == *b
}

pub fn copy_raw(&self) -> Vec<u8> {
self.actual.read().unwrap().data_ref().to_vec()
}
}
10 changes: 10 additions & 0 deletions crates/servicepoint_binding_uniffi/src/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ impl BitVec {
pub fn len(&self) -> u64 {
self.actual.read().unwrap().len() as u64
}

pub fn equals(&self, other: &BitVec) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();
*a == *b
}

pub fn copy_raw(&self) -> Vec<u8> {
self.actual.read().unwrap().clone().into_vec()
}
}
12 changes: 11 additions & 1 deletion crates/servicepoint_binding_uniffi/src/brightness_grid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use servicepoint::{Brightness, Grid};
use servicepoint::{Brightness, DataRef, Grid};
use std::sync::{Arc, RwLock};

#[derive(uniffi::Object)]
Expand Down Expand Up @@ -67,4 +67,14 @@ impl BrightnessGrid {
pub fn height(&self) -> u64 {
self.actual.read().unwrap().height() as u64
}

pub fn equals(&self, other: &BrightnessGrid) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();
*a == *b
}

pub fn copy_raw(&self) -> Vec<u8> {
self.actual.read().unwrap().data_ref().iter().map(u8::from).collect()
}
}
17 changes: 17 additions & 0 deletions crates/servicepoint_binding_uniffi/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::bitmap::Bitmap;
use crate::bitvec::BitVec;
use crate::brightness_grid::BrightnessGrid;
use crate::compression_code::CompressionCode;
use crate::cp437_grid::Cp437Grid;
use crate::errors::ServicePointError;
use servicepoint::Origin;
use std::sync::Arc;
Expand Down Expand Up @@ -138,8 +139,24 @@ impl Command {
Self::internal_new(actual)
}

#[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 = servicepoint::Command::Cp437Data(origin, grid);
Self::internal_new(actual)
}

#[uniffi::constructor]
pub fn clone(other: &Arc<Self>) -> Arc<Self> {
Self::internal_new(other.actual.clone())
}

pub fn equals(&self, other: &Command) -> bool {
self.actual == other.actual
}
}
76 changes: 76 additions & 0 deletions crates/servicepoint_binding_uniffi/src/cp437_grid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use servicepoint::{DataRef, Grid};
use std::sync::{Arc, RwLock};

#[derive(uniffi::Object)]
pub struct Cp437Grid {
pub(crate) actual: RwLock<servicepoint::Cp437Grid>,
}

impl Cp437Grid {
fn internal_new(actual: servicepoint::Cp437Grid) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}

#[uniffi::export]
impl Cp437Grid {
#[uniffi::constructor]
pub fn new(width: u64, height: u64) -> Arc<Self> {
Self::internal_new(servicepoint::Cp437Grid::new(
width as usize,
height as usize,
))
}

#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::Cp437Grid::load(
width as usize,
height as usize,
&data,
))
}

#[uniffi::constructor]
pub fn clone(other: &Arc<Self>) -> Arc<Self> {
Self::internal_new(other.actual.read().unwrap().clone())
}

pub fn set(&self, x: u64, y: u64, value: u8) {
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}

pub fn get(&self, x: u64, y: u64) -> u8 {
self.actual
.read()
.unwrap()
.get(x as usize, y as usize)
.into()
}

pub fn fill(&self, value: u8) {
self.actual.write().unwrap().fill(value)
}
pub fn width(&self) -> u64 {
self.actual.read().unwrap().width() as u64
}

pub fn height(&self) -> u64 {
self.actual.read().unwrap().height() as u64
}

pub fn equals(&self, other: &Cp437Grid) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();
*a == *b
}

pub fn copy_raw(&self) -> Vec<u8> {
self.actual.read().unwrap().data_ref().to_vec()
}
}
1 change: 1 addition & 0 deletions crates/servicepoint_binding_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod brightness_grid;
mod command;
mod compression_code;
mod connection;
mod cp437_grid;
mod errors;

0 comments on commit 9553d9f

Please sign in to comment.