From a1774d1f73c7941b4ea4f10596ea00e242af6ab8 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Fri, 13 Dec 2024 17:28:32 -0800 Subject: [PATCH] grid: impl FromStr for Grid --- utils/grid/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index d09e769..f27f5ab 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -1,9 +1,10 @@ use std::{ fmt::{Debug, Display, Formatter, Write}, - io::BufRead, + io::{BufRead, Cursor}, iter::repeat, mem::swap, ops::{Add, Sub}, + str::FromStr, }; #[derive(Clone, Debug)] @@ -107,7 +108,7 @@ impl AsCoord2d for (u64, u64) { } } -#[derive(Clone)] +#[derive(Clone, Eq, PartialEq)] pub struct Grid { pub data: Vec, width: i64, @@ -245,6 +246,14 @@ impl From for Grid { } } +// Should be Grid? +impl FromStr for Grid { + type Err = Box; + fn from_str(s: &str) -> Result { + Ok(Cursor::new(s).into()) + } +} + // impl> Display for Grid { // fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { // for y in 0..self.height() { @@ -276,6 +285,11 @@ mod tests { static TEST_VECTOR: &[u8] = b"ABCD EFGH IJKL +FBCG"; + + static TEST_VECTOR_S: &str = "ABCD +EFGH +IJKL FBCG"; fn unchecked_load() -> Grid { @@ -286,6 +300,7 @@ FBCG"; fn from_string() { let grid = unchecked_load(); assert_eq!(grid.data, "ABCDEFGHIJKLFBCG".as_bytes()); + assert_eq!(TEST_VECTOR_S.parse::>().unwrap().data, "ABCDEFGHIJKLFBCG".as_bytes()); } #[test]