From f17dd5b89dd15bc78a7eda889e07f8158cf0d977 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 21 Jan 2023 11:23:32 -0500 Subject: [PATCH] Clean up the read and write operations maps no longer need to be serializable. This allows for both a serializable form and a non-serializable form. --- coordinates/src/hex_map.rs | 76 +++++++++++++++++++------------------- coordinates/src/lib.rs | 8 +++- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/coordinates/src/hex_map.rs b/coordinates/src/hex_map.rs index 1b745ae..1291c69 100644 --- a/coordinates/src/hex_map.rs +++ b/coordinates/src/hex_map.rs @@ -10,7 +10,7 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but You should have received a copy of the GNU General Public License along with Luminescent Dreams Tools. If not, see . */ -use crate::hex::AxialAddr; +use crate::{hex::AxialAddr, Error}; use nom::{ bytes::complete::tag, character::complete::alphanumeric1, @@ -29,10 +29,7 @@ pub struct Map { cells: HashMap, } -impl + Clone> Map -where - String: From, -{ +impl Map { pub fn new_hexagonal(radius: usize) -> Map { let cells = vec![(AxialAddr::origin(), Default::default())] .into_iter() @@ -56,40 +53,46 @@ where pub fn get(&self, addr: &AxialAddr) -> Option<&A> { self.cells.get(addr) } +} - // pub fn from_file(path: PathBuf) -> Map {} +pub fn read_file>(path: PathBuf) -> Result, Error> { + let data = std::fs::read_to_string(path)?; + Ok(parse_data(data.lines())) +} - pub fn to_file(&self, path: PathBuf) -> () {} +pub fn write_file(path: PathBuf, map: Map) -> Result<(), Error> +where + String: From, +{ + std::fs::write(path, write_data(map).join("\n")).map_err(Error::from) +} - pub fn from_data(data: Vec) -> Map { - fn parse_line>( - input: &str, - ) -> Result<(AxialAddr, A), nom::error::Error<&str>> { - fn parse>(input: &str) -> IResult<&str, (AxialAddr, A)> { - let (input, addr) = parse_address(input)?; - let (input, value) = cell_value::(input)?; - Ok((input, (addr, value))) - } - - parse(input).finish().map(|(_, pair)| pair) +fn parse_data<'a, A: Default + From>(data: impl Iterator + 'a) -> Map { + fn parse_line>(input: &str) -> Result<(AxialAddr, A), nom::error::Error<&str>> { + fn parse>(input: &str) -> IResult<&str, (AxialAddr, A)> { + let (input, addr) = parse_address(input)?; + let (input, value) = cell_value::(input)?; + Ok((input, (addr, value))) } - let cells = data - .into_iter() - .map(|line| parse_line::(&line).unwrap()) - .collect::>(); - let cells = cells.into_iter().collect::>(); - Map { cells } + parse(input).finish().map(|(_, pair)| pair) } - pub fn to_data(&self) -> Vec { - self.cells - .iter() - .map(|(addr, val)| { - format!("[{}, {}] {}", addr.q(), addr.r(), String::from(val.clone())) - }) - .collect() - } + let cells = data + .map(|line| parse_line::(&line).unwrap()) + .collect::>(); + let cells = cells.into_iter().collect::>(); + Map { cells } +} + +fn write_data(map: Map) -> Vec +where + String: From, +{ + map.cells + .iter() + .map(|(addr, val)| format!("[{}, {}] {}", addr.q(), addr.r(), String::from(val.clone()))) + .collect() } fn parse_address(input: &str) -> IResult<&str, AxialAddr> { @@ -163,7 +166,7 @@ mod test { expected.insert("[0, -1] .".to_owned()); expected.insert("[1, -1] .".to_owned()); - let map_rows = map.to_data(); + let map_rows = write_data(map); assert_eq!(map_rows.len(), expected.len()); map_rows .iter() @@ -182,12 +185,7 @@ mod test { [-1, 0] . [0, -1] . [1, -1] ."; - let map: Map = Map::from_data( - map_data - .lines() - .map(|l| l.to_owned()) - .collect::>(), - ); + let map: Map = parse_data(map_data.lines()); assert_eq!(map.get(&AxialAddr::new(0, 0)), Some(&MapVal('L'))); assert_eq!(map.get(&AxialAddr::new(1, 0)), Some(&MapVal('A'))); assert_eq!(map.get(&AxialAddr::new(0, 1)), Some(&MapVal('.'))); diff --git a/coordinates/src/lib.rs b/coordinates/src/lib.rs index e90ef28..52c337c 100644 --- a/coordinates/src/lib.rs +++ b/coordinates/src/lib.rs @@ -17,7 +17,13 @@ pub enum Error { IO(std::io::Error), } +impl From for Error { + fn from(err: std::io::Error) -> Self { + Self::IO(err) + } +} + mod hex; -mod hex_map; +pub mod hex_map; pub use hex::AxialAddr;