Add ascii matrix macro
This commit is contained in:
parent
908fa77d8a
commit
91ee995c71
|
@ -5,7 +5,8 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["ascii"]
|
||||||
|
ascii = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# tracing = { version = "0.1", default-features = false}
|
# tracing = { version = "0.1", default-features = false}
|
||||||
|
|
|
@ -3,3 +3,28 @@
|
||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
|
|
||||||
pub use matrix::Mat;
|
pub use matrix::Mat;
|
||||||
|
|
||||||
|
/// Allows constructing a `Mat` constant with string literals:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use mino::{mat, Mat};
|
||||||
|
/// # const MAT: &Mat =
|
||||||
|
/// mat! {
|
||||||
|
/// ".......xxx"; // top row (y=2)
|
||||||
|
/// "x.xxxxxxxx"; // middle row (y=1)
|
||||||
|
/// "xxxx.xxxxx"; // bottom row (y=0)
|
||||||
|
/// }
|
||||||
|
/// # ;
|
||||||
|
/// ```
|
||||||
|
#[cfg(any(feature = "ascii", test))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! mat {
|
||||||
|
($($row:literal);* $(;)?) => {
|
||||||
|
({
|
||||||
|
const MAT: &$crate::matrix::Mat = $crate::matrix::Mat::new(
|
||||||
|
&$crate::matrix::__ascii::parse([$($row),*]),
|
||||||
|
);
|
||||||
|
MAT
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -73,9 +73,38 @@ impl Mat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "ascii", test))]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod __ascii {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub const fn parse<const N: usize>(strs: [&str; N]) -> [u16; N] {
|
||||||
|
let mut data = [EMPTY_ROW; N];
|
||||||
|
let mut i = 0;
|
||||||
|
while i < N {
|
||||||
|
let row = strs[i].as_bytes();
|
||||||
|
if row.len() != COLUMNS as usize {
|
||||||
|
panic!("wrong number of columns in ascii row");
|
||||||
|
}
|
||||||
|
let y = N - i - 1;
|
||||||
|
let mut x = 0i16;
|
||||||
|
while x < COLUMNS {
|
||||||
|
match row[x as usize] {
|
||||||
|
b'.' | b'_' | b' ' => {}
|
||||||
|
_ => data[y] |= 1 << x,
|
||||||
|
}
|
||||||
|
x += 1;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::mat;
|
||||||
|
|
||||||
use core::ops::RangeInclusive;
|
use core::ops::RangeInclusive;
|
||||||
|
|
||||||
|
@ -93,23 +122,17 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const M1: &Mat = Mat::new(&[
|
const M1: &Mat = mat! {
|
||||||
// xx..xxxx.x y=0
|
"...x....x.";
|
||||||
0b1011110011 | EMPTY_ROW,
|
"xx..xxxx.x";
|
||||||
// ...x....x. y=1
|
};
|
||||||
0b0100001000 | EMPTY_ROW,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const M2: &Mat = Mat::new(&[
|
const M2: &Mat = mat! {
|
||||||
// xxxx.xxxxx y=0
|
".........x";
|
||||||
0b1111101111 | EMPTY_ROW,
|
".........x";
|
||||||
// x.......xx y=1
|
"x.......xx";
|
||||||
0b1100000001 | EMPTY_ROW,
|
"xxxx.xxxxx";
|
||||||
// .........x y=2
|
};
|
||||||
0b1000000000 | EMPTY_ROW,
|
|
||||||
// .........x y=3
|
|
||||||
0b1000000000 | EMPTY_ROW,
|
|
||||||
]);
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dims() {
|
fn test_dims() {
|
||||||
|
|
Loading…
Reference in New Issue