43 lines
1.0 KiB
Rust
43 lines
1.0 KiB
Rust
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<String> for Region {
|
|
type Error = RegionParseError;
|
|
|
|
/// Try from string
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
let re = Regex::new(r"r\.(?P<x>-?[0-9]*)+\.(?P<y>-?[0-9]*)").unwrap();
|
|
if re.is_match(&value) {
|
|
let captures = re.captures(value.as_str()).unwrap();
|
|
|
|
return Ok(Region {
|
|
x: captures["x"].parse::<i64>().unwrap(),
|
|
y: captures["y"].parse::<i64>().unwrap(),
|
|
});
|
|
}
|
|
|
|
Err(RegionParseError)
|
|
}
|
|
}
|