make char extensions available to window
Some checks failed
Rust / build (pull_request) Failing after 1m6s

This commit is contained in:
Vinzenz Schroeter 2025-07-05 19:54:04 +02:00
parent 62cb3d25ae
commit 60bfd5b425
3 changed files with 76 additions and 32 deletions

View file

@ -1,4 +1,6 @@
use crate::{SetValueSeriesError, TryLoadValueGridError, ValueGrid};
use crate::{
Grid, GridMut, SetValueSeriesError, TryLoadValueGridError, ValueGrid,
};
use std::string::FromUtf8Error;
/// A grid containing UTF-8 characters.
@ -59,6 +61,29 @@ impl CharGrid {
result
}
/// Loads a [`CharGrid`] with the specified dimensions from the provided UTF-8 bytes.
///
/// returns: [`CharGrid`] that contains the provided data, or [`FromUtf8Error`] if the data is invalid.
///
/// # Examples
///
/// ```
/// # use servicepoint::CharGrid;
/// let grid = CharGrid::load_utf8(2, 2, [97u8, 98, 99, 100].to_vec());
/// ```
pub fn load_utf8(
width: usize,
height: usize,
bytes: Vec<u8>,
) -> Result<CharGrid, LoadUtf8Error> {
let s: Vec<char> = String::from_utf8(bytes)?.chars().collect();
CharGrid::load(width, height, &s).ok_or(LoadUtf8Error::TryLoadError(
TryLoadValueGridError::InvalidDimensions,
))
}
}
pub trait CharGridExt: Grid<char> {
/// Copies a column from the grid as a String.
///
/// Returns [None] if x is out of bounds.
@ -71,8 +96,8 @@ impl CharGrid {
/// let col = grid.get_col_str(0).unwrap(); // "ac"
/// ```
#[must_use]
pub fn get_col_str(&self, x: usize) -> Option<String> {
Some(String::from_iter(self.get_col(x)?))
fn get_col_str(&self, x: usize) -> Option<String> {
Some((0..self.height()).map(|y| self.get(x, y)).collect())
}
/// Copies a row from the grid as a String.
@ -86,11 +111,26 @@ impl CharGrid {
/// let grid = CharGrid::from("ab\ncd");
/// let row = grid.get_row_str(0).unwrap(); // "ab"
/// ```
#[must_use]
fn get_row_str(&self, y: usize) -> Option<String> {
Some((0..self.width()).map(|x| self.get(x, y)).collect())
}
}
#[inherent::inherent]
impl CharGridExt for CharGrid {
#[must_use]
pub fn get_col_str(&self, x: usize) -> Option<String> {
Some(String::from_iter(self.get_col(x)?))
}
#[must_use]
pub fn get_row_str(&self, y: usize) -> Option<String> {
Some(String::from_iter(self.get_row(y)?))
}
}
pub trait CharGridMutExt: GridMut<char> {
/// Overwrites a row in the grid with a str.
///
/// Returns [`SetValueSeriesError`] if y is out of bounds or `row` is not of the correct size.
@ -98,11 +138,11 @@ impl CharGrid {
/// # Examples
///
/// ```
/// # use servicepoint::CharGrid;
/// # use servicepoint::{CharGrid, CharGridMutExt};
/// let mut grid = CharGrid::from("ab\ncd");
/// grid.set_row_str(0, "ef").unwrap();
/// ```
pub fn set_row_str(
fn set_row_str(
&mut self,
y: usize,
value: &str,
@ -144,7 +184,7 @@ impl CharGrid {
/// let mut grid = CharGrid::from("ab\ncd");
/// grid.set_col_str(0, "ef").unwrap();
/// ```
pub fn set_col_str(
fn set_col_str(
&mut self,
x: usize,
value: &str,
@ -174,27 +214,21 @@ impl CharGrid {
Ok(())
}
}
/// Loads a [`CharGrid`] with the specified dimensions from the provided UTF-8 bytes.
///
/// returns: [`CharGrid`] that contains the provided data, or [`FromUtf8Error`] if the data is invalid.
///
/// # Examples
///
/// ```
/// # use servicepoint::CharGrid;
/// let grid = CharGrid::load_utf8(2, 2, [97u8, 98, 99, 100].to_vec());
/// ```
pub fn load_utf8(
width: usize,
height: usize,
bytes: Vec<u8>,
) -> Result<CharGrid, LoadUtf8Error> {
let s: Vec<char> = String::from_utf8(bytes)?.chars().collect();
CharGrid::load(width, height, &s).ok_or(LoadUtf8Error::TryLoadError(
TryLoadValueGridError::InvalidDimensions,
))
}
#[inherent::inherent]
impl CharGridMutExt for CharGrid {
pub fn set_col_str(
&mut self,
x: usize,
value: &str,
) -> Result<(), SetValueSeriesError>;
pub fn set_row_str(
&mut self,
y: usize,
value: &str,
) -> Result<(), SetValueSeriesError>;
}
#[derive(Debug, thiserror::Error)]

View file

@ -10,14 +10,15 @@ mod value_grid;
mod window;
pub use bit_vec::{bitvec, DisplayBitVec};
pub use bitmap::*;
pub use bitmap::{Bitmap, LoadBitmapError};
pub use brightness_grid::BrightnessGrid;
pub use byte_grid::ByteGrid;
pub use char_grid::CharGrid;
pub use cp437_grid::Cp437Grid;
pub use char_grid::{CharGrid, CharGridExt, CharGridMutExt, LoadUtf8Error};
pub use cp437_grid::{Cp437Grid, InvalidCharError};
pub use data_ref::DataRef;
pub use grid::{Grid, GridMut};
pub use value_grid::{
IterGridRows, SetValueSeriesError, TryLoadValueGridError, Value, ValueGrid,
EnumerateGrid, IterGridRows, SetValueSeriesError, TryLoadValueGridError,
Value, ValueGrid,
};
pub use window::{Window, WindowMut};

View file

@ -1,4 +1,7 @@
use crate::{Grid, GridMut};
use crate::{
containers::char_grid::{CharGridExt, CharGridMutExt},
Grid, GridMut,
};
use std::marker::PhantomData;
macro_rules! define_window {
@ -70,6 +73,9 @@ macro_rules! define_window {
self.height
}
}
#[inherent::inherent]
impl<TGrid: Grid<char>> CharGridExt for $name<'_, char, TGrid> {}
};
}
@ -95,6 +101,9 @@ impl<TElement: Copy, TGrid: GridMut<TElement>> GridMut<TElement>
}
}
#[inherent::inherent]
impl<TGrid: GridMut<char>> CharGridMutExt for WindowMut<'_, char, TGrid> {}
#[cfg(test)]
mod tests {
use crate::containers::window::{Window, WindowMut};
@ -132,7 +141,7 @@ mod tests {
let mut view = WindowMut::new(&mut grid, 1, 1, 1, 3).unwrap();
view.fill('#');
view.set(0,0, '!');
view.set(0, 0, '!');
assert_eq!(
grid.data_ref(),