use core::fmt;
use core::str::{FromStr, Split};
use crate::error::{Error, ErrorKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ImageDimensions {
pub width: u64,
pub height: u64,
}
impl ImageDimensions {
#[inline]
pub fn new(width: u64, height: u64) -> Self {
Self { width, height }
}
}
impl fmt::Display for ImageDimensions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.width, self.height)
}
}
impl FromStr for ImageDimensions {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut spitted: Split<char> = s.split('x');
if let (Some(width), Some(height)) = (spitted.next(), spitted.next()) {
Ok(Self::new(
width.parse().map_err(Error::malformed)?,
height.parse().map_err(Error::malformed)?,
))
} else {
Err(Error::with_static_message(
ErrorKind::Invalid,
"invalid dimensions",
))
}
}
}