use regex::Regex; use std::convert::TryFrom; use std::error::Error; use std::fmt; #[derive(Debug, Clone)] pub struct RegionParseError; impl fmt::Display for RegionParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Unable to parse region file name") } } impl Error for RegionParseError {} /// Struct to store information about the region pub struct Region { /// x position of the region pub x: i64, /// y position of the region pub y: i64, } impl TryFrom for Region { type Error = RegionParseError; /// Try from string fn try_from(value: String) -> Result { let re = Regex::new(r"r\.(?P-?[0-9]*)+\.(?P-?[0-9]*)").unwrap(); if re.is_match(&value) { let captures = re.captures(value.as_str()).unwrap(); return Ok(Region { x: captures["x"].parse::().unwrap(), y: captures["y"].parse::().unwrap(), }); } Err(RegionParseError) } }