diff --git a/src/compression_code.rs b/src/compression_code.rs index a3d332d..bf530f1 100644 --- a/src/compression_code.rs +++ b/src/compression_code.rs @@ -15,12 +15,28 @@ pub enum CompressionCode { impl From for CompressionCode { fn from(value: servicepoint::CompressionCode) -> Self { - value.into() + match value { + servicepoint::CompressionCode::Uncompressed => { + CompressionCode::Uncompressed + } + servicepoint::CompressionCode::Zlib => CompressionCode::Zlib, + servicepoint::CompressionCode::Bzip2 => CompressionCode::Bzip2, + servicepoint::CompressionCode::Lzma => CompressionCode::Lzma, + servicepoint::CompressionCode::Zstd => CompressionCode::Zstd, + } } } impl From for servicepoint::CompressionCode { fn from(value: CompressionCode) -> Self { - servicepoint::CompressionCode::try_from(value).unwrap() + match value { + CompressionCode::Uncompressed => { + servicepoint::CompressionCode::Uncompressed + } + CompressionCode::Zlib => servicepoint::CompressionCode::Zlib, + CompressionCode::Bzip2 => servicepoint::CompressionCode::Bzip2, + CompressionCode::Lzma => servicepoint::CompressionCode::Lzma, + CompressionCode::Zstd => servicepoint::CompressionCode::Zstd, + } } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 387344c..28712be 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,23 +1,14 @@ use crate::{ containers::{cp437_grid::Cp437Grid, wrap_width_height}, + errors::ServicePointError, macros::wrap_object, }; use servicepoint::{Grid, SetValueSeriesError}; -use std::{convert::Into, sync::Arc}; +use std::sync::Arc; wrap_object!(CharGrid); wrap_width_height!(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] @@ -38,17 +29,23 @@ impl CharGrid { x: u64, y: u64, value: String, - ) -> Result<(), CharGridError> { + ) -> Result<(), ServicePointError> { let value = Self::str_to_char(value)?; - self.write().set(x as usize, y as usize, value); - Ok(()) + if self.write().set_optional(x as isize, y as isize, value) { + Ok(()) + } else { + Err(ServicePointError::OutOfBounds) + } } - pub fn get(&self, x: u64, y: u64) -> String { - self.read().get(x as usize, y as usize).into() + pub fn get(&self, x: u64, y: u64) -> Result { + self.read() + .get_optional(x as isize, y as isize) + .ok_or(ServicePointError::OutOfBounds) + .map(String::from) } - pub fn fill(&self, value: String) -> Result<(), CharGridError> { + pub fn fill(&self, value: String) -> Result<(), ServicePointError> { let value = Self::str_to_char(value)?; self.write().fill(value); Ok(()) @@ -59,36 +56,38 @@ impl CharGrid { String::from(&*grid) } - pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> { + pub fn set_row( + &self, + y: u64, + row: String, + ) -> Result<(), ServicePointError> { self.write() .set_row(y as usize, &row.chars().collect::>()) - .map_err(CharGridError::from) + .map_err(ServicePointError::from) } - pub fn set_col(&self, x: u64, col: String) -> Result<(), CharGridError> { + pub fn set_col( + &self, + x: u64, + col: String, + ) -> Result<(), ServicePointError> { self.write() .set_row(x as usize, &col.chars().collect::>()) - .map_err(CharGridError::from) + .map_err(ServicePointError::from) } - pub fn get_row(&self, y: u64) -> Result { + pub fn get_row(&self, y: u64) -> Result { self.read() .get_row(y as usize) .map(String::from_iter) - .ok_or(CharGridError::OutOfBounds { - index: y, - size: self.height(), - }) + .ok_or(ServicePointError::OutOfBounds) } - pub fn get_col(&self, x: u64) -> Result { + pub fn get_col(&self, x: u64) -> Result { self.read() .get_col(x as usize) .map(String::from_iter) - .ok_or(CharGridError::OutOfBounds { - index: x, - size: self.width(), - }) + .ok_or(ServicePointError::OutOfBounds) } pub fn to_cp437(&self) -> Arc { @@ -97,9 +96,9 @@ impl CharGrid { } impl CharGrid { - fn str_to_char(value: String) -> Result { + fn str_to_char(value: String) -> Result { if value.len() != 1 { - return Err(CharGridError::StringNotOneChar { value }); + return Err(ServicePointError::StringNotOneChar { value }); } let value = value.chars().nth(0).unwrap(); @@ -107,17 +106,14 @@ impl CharGrid { } } -impl From for CharGridError { +impl From for ServicePointError { fn from(e: SetValueSeriesError) -> Self { match e { - SetValueSeriesError::OutOfBounds { index, size } => { - CharGridError::OutOfBounds { - index: index as u64, - size: size as u64, - } + SetValueSeriesError::OutOfBounds { .. } => { + ServicePointError::OutOfBounds } SetValueSeriesError::InvalidLength { actual, expected } => { - CharGridError::InvalidSeriesLength { + ServicePointError::InvalidSeriesLength { actual: actual as u64, expected: expected as u64, } diff --git a/src/errors.rs b/src/errors.rs index 03d05ba..f234278 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,4 +6,10 @@ pub enum ServicePointError { InvalidBrightness { value: u8 }, #[error("The provided packet is invalid or a conversion to packet failed")] InvalidPacket, + #[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 was out of bounds")] + OutOfBounds, } diff --git a/src/packet.rs b/src/packet.rs index ceaf052..bdfd0a8 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -4,3 +4,10 @@ wrap_object!(Packet); wrap_object!(Header); wrap_object_attr_wrapped!(Packet, header, Header); + +#[uniffi::export] +impl Packet { + pub fn as_bytes(&self) -> Vec { + Vec::from(&*self.read()) + } +}