use core::str::FromStr;
use serde::{
Deserialize,
Serialize,
};
use crate::errors::{
BunsenError,
BunsenErrorKind,
BunsenResult,
ParseError,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GridShape2D {
pub width: usize,
pub height: usize,
}
impl FromStr for GridShape2D {
type Err = BunsenError;
fn from_str(s: &str) -> BunsenResult<Self> {
let parse = |what: &'static str, part: &str| {
part.parse::<usize>().map_err(|e| {
BunsenError::from_cause(
BunsenErrorKind::Policy,
ParseError::new(what).input(part).with_source(e),
)
})
};
if s.contains(",") {
let parts: Vec<&str> = s.split(',').collect();
if parts.len() != 2 {
return Err(BunsenError::from_cause(
BunsenErrorKind::Policy,
ParseError::new("grid shape")
.input(s)
.because("must be WIDTH,HEIGHT or SIZE"),
));
}
let width = parse("grid width", parts[0])?;
let height = parse("grid height", parts[1])?;
Ok(Self { width, height })
} else {
let size = parse("grid size", s)?;
Ok(Self::square(size))
}
}
}
impl GridShape2D {
pub fn square(size: usize) -> Self {
Self {
width: size,
height: size,
}
}
pub fn as_height_width(&self) -> [usize; 2] {
[self.height, self.width]
}
pub fn as_width_height(&self) -> [usize; 2] {
[self.width, self.height]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::testing::ErrorMatcher;
#[test]
fn test_parse_grid_shape() {
assert_eq!(
GridShape2D::from_str("10,10").unwrap(),
GridShape2D {
width: 10,
height: 10
}
);
assert_eq!(
GridShape2D::from_str("10").unwrap(),
GridShape2D {
width: 10,
height: 10
}
);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("cannot parse grid shape \"10,10,10\": must be WIDTH,HEIGHT or SIZE")
.has_cause::<ParseError>()
.assert_err(&GridShape2D::from_str("10,10,10"));
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("cannot parse grid height \"x\": invalid digit found in string")
.assert_err(&GridShape2D::from_str("10,x"));
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_contains("cannot parse grid size \"-1\"")
.assert_err(&GridShape2D::from_str("-1"));
}
}