use core::fmt;
use core::num::ParseIntError;
use core::str::{FromStr, Split};
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
ParseIntError(ParseIntError),
InvalidDimensions,
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ParseIntError(e) => e.fmt(f),
Self::InvalidDimensions => f.write_str("Invalid dimensions"),
}
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Self::ParseIntError(e)
}
}
#[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()?, height.parse()?))
} else {
Err(Error::InvalidDimensions)
}
}
}