This commit is contained in:
		
							parent
							
								
									f4c7519658
								
							
						
					
					
						commit
						7e07a35472
					
				
					 4 changed files with 67 additions and 42 deletions
				
			
		|  | @ -15,12 +15,28 @@ pub enum CompressionCode { | |||
| 
 | ||||
| impl From<servicepoint::CompressionCode> 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<CompressionCode> 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, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -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<String, ServicePointError> { | ||||
|         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::<Vec<_>>()) | ||||
|             .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::<Vec<_>>()) | ||||
|             .map_err(CharGridError::from) | ||||
|             .map_err(ServicePointError::from) | ||||
|     } | ||||
| 
 | ||||
|     pub fn get_row(&self, y: u64) -> Result<String, CharGridError> { | ||||
|     pub fn get_row(&self, y: u64) -> Result<String, ServicePointError> { | ||||
|         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<String, CharGridError> { | ||||
|     pub fn get_col(&self, x: u64) -> Result<String, ServicePointError> { | ||||
|         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<Cp437Grid> { | ||||
|  | @ -97,9 +96,9 @@ impl CharGrid { | |||
| } | ||||
| 
 | ||||
| impl CharGrid { | ||||
|     fn str_to_char(value: String) -> Result<char, CharGridError> { | ||||
|     fn str_to_char(value: String) -> Result<char, ServicePointError> { | ||||
|         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<SetValueSeriesError> for CharGridError { | ||||
| impl From<SetValueSeriesError> 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, | ||||
|                 } | ||||
|  |  | |||
|  | @ -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, | ||||
| } | ||||
|  |  | |||
|  | @ -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<u8> { | ||||
|         Vec::from(&*self.read()) | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter