-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7b226b
commit 2032d7e
Showing
9 changed files
with
755 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CharGridTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace ServicePoint.Tests; | ||
|
||
public class CharGridTests | ||
{ | ||
[Fact] | ||
public void BasicFunctions() | ||
{ | ||
var grid = new CharGrid(8, 2); | ||
Assert.Equal("\0", grid.Get(0, 0)); | ||
Assert.Equal("\0", grid.Get(grid.Width() - 1, grid.Height() - 1)); | ||
grid.Fill(" "); | ||
Assert.Equal(" ", grid.Get(1, 1)); | ||
grid.Set(1, 1, "-"); | ||
Assert.Equal("-", grid.Get(1, 1)); | ||
Assert.Throws<PanicException>(() => grid.Get(8, 2)); | ||
} | ||
|
||
[Fact] | ||
public void RowAndCol() | ||
{ | ||
var grid = new CharGrid(3, 2); | ||
Assert.Equal("\0\0\0", grid.GetRow(0)); | ||
grid.Fill(" "); | ||
Assert.Equal(" ", grid.GetCol(1)); | ||
Assert.Throws<CharGridException.OutOfBounds>(() => grid.GetCol(3)); | ||
Assert.Throws<CharGridException.InvalidSeriesLength>(() => grid.SetRow(1, "Text")); | ||
grid.SetRow(1, "Foo"); | ||
Assert.Equal("Foo", grid.GetRow(1)); | ||
Assert.Equal(" o", grid.GetCol(2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
543 changes: 543 additions & 0 deletions
543
.../servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use servicepoint::{Grid, SeriesError}; | ||
use std::convert::Into; | ||
use std::sync::{Arc, RwLock}; | ||
use crate::cp437_grid::Cp437Grid; | ||
|
||
#[derive(uniffi::Object)] | ||
pub struct CharGrid { | ||
pub(crate) actual: RwLock<servicepoint::CharGrid>, | ||
} | ||
|
||
#[derive(uniffi::Error, thiserror::Error, Debug)] | ||
pub enum CharGridError { | ||
#[error("Exactly one character was expected, but {value:?} was provided")] | ||
StringNotOneChar { value: String }, | ||
#[error("The provided series was expected to have a length of {expected}, but was {actual}")] | ||
InvalidSeriesLength { actual: u64, expected: u64 }, | ||
#[error("The index {index} was out of bounds for size {size}")] | ||
OutOfBounds { index: u64, size: u64 }, | ||
} | ||
|
||
#[uniffi::export] | ||
impl CharGrid { | ||
#[uniffi::constructor] | ||
pub fn new(width: u64, height: u64) -> Arc<Self> { | ||
Self::internal_new(servicepoint::CharGrid::new( | ||
width as usize, | ||
height as usize, | ||
)) | ||
} | ||
|
||
#[uniffi::constructor] | ||
pub fn load(data: String) -> Arc<Self> { | ||
Self::internal_new(servicepoint::CharGrid::from(&*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: String, | ||
) -> Result<(), CharGridError> { | ||
let value = Self::str_to_char(value)?; | ||
self.actual | ||
.write() | ||
.unwrap() | ||
.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() | ||
} | ||
|
||
pub fn fill(&self, value: String) -> Result<(), CharGridError> { | ||
let value = Self::str_to_char(value)?; | ||
self.actual.write().unwrap().fill(value); | ||
Ok(()) | ||
} | ||
|
||
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: &CharGrid) -> bool { | ||
let a = self.actual.read().unwrap(); | ||
let b = other.actual.read().unwrap(); | ||
*a == *b | ||
} | ||
|
||
pub fn as_string(&self) -> String { | ||
let grid = self.actual.read().unwrap(); | ||
String::from(&*grid) | ||
} | ||
|
||
pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> { | ||
self.actual | ||
.write() | ||
.unwrap() | ||
.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() | ||
.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() | ||
.get_row(y as usize) | ||
.map(move |vec| String::from_iter(vec)) | ||
.ok_or(CharGridError::OutOfBounds {index: y, size: self.height()}) | ||
} | ||
|
||
pub fn get_col(&self, x: u64) -> Result<String, CharGridError> { | ||
self.actual | ||
.read() | ||
.unwrap() | ||
.get_col(x as usize) | ||
.map(move |vec| String::from_iter(vec)) | ||
.ok_or(CharGridError::OutOfBounds {index: x, size: self.width()}) | ||
} | ||
|
||
pub fn to_cp437(&self) -> Arc<Cp437Grid> { | ||
Cp437Grid::internal_new(servicepoint::Cp437Grid::from(&*self.actual.read().unwrap())) | ||
} | ||
} | ||
|
||
impl CharGrid { | ||
pub(crate) fn internal_new(actual: servicepoint::CharGrid) -> Arc<Self> { | ||
Arc::new(Self { | ||
actual: RwLock::new(actual), | ||
}) | ||
} | ||
|
||
fn str_to_char(value: String) -> Result<char, CharGridError> { | ||
if value.len() != 1 { | ||
return Err(CharGridError::StringNotOneChar { | ||
value, | ||
}); | ||
} | ||
|
||
let value = value.chars().nth(0).unwrap(); | ||
Ok(value) | ||
} | ||
} | ||
|
||
impl From<SeriesError> for CharGridError { | ||
fn from(e: SeriesError) -> Self { | ||
match e { | ||
SeriesError::OutOfBounds { index, size } => { | ||
CharGridError::OutOfBounds { | ||
index: index as u64, | ||
size: size as u64, | ||
} | ||
} | ||
SeriesError::InvalidLength { actual, expected } => { | ||
CharGridError::InvalidSeriesLength { | ||
actual: actual as u64, | ||
expected: expected as u64, | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters