Skip to content

Commit

Permalink
fix(cli): 🐛 updated parsing logic for width and height to reduce numb…
Browse files Browse the repository at this point in the history
…er of edge cases
  • Loading branch information
SalOne22 committed Aug 20, 2024
1 parent 100afa6 commit 9bd3ea2
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/cli/preprocessors/resize/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use regex::Regex;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResizeValue {
Expand Down Expand Up @@ -62,8 +63,22 @@ impl std::str::FromStr for ResizeValue {
match s {
s if s.starts_with('@') => Ok(Self::Multiplier(s[1..].parse()?)),
s if s.ends_with('%') => Ok(Self::Percentage(s[..s.len() - 1].parse()?)),
s if s.ends_with('w') => Ok(Self::Dimensions(Some(s[..s.len() - 1].parse()?), None)),
s if s.ends_with('h') => Ok(Self::Dimensions(None, Some(s[..s.len() - 1].parse()?))),
s if s.contains('w') => {
let re = Regex::new(r"(?P<width>\d+)").unwrap();
let Some(cap) = re.captures(s) else {
return Err(anyhow!("Invalid resize value"));
};

Ok(Self::Dimensions(Some(cap["width"].parse()?), None))
}
s if s.contains('h') => {
let re = Regex::new(r"(?P<height>\d+)").unwrap();
let Some(cap) = re.captures(s) else {
return Err(anyhow!("Invalid resize value"));
};

Ok(Self::Dimensions(None, Some(cap["height"].parse()?)))
}
s if s.contains('x') => {
let dimensions: Vec<&str> = s.split('x').collect();
if dimensions.len() > 2 {
Expand Down Expand Up @@ -163,6 +178,26 @@ mod tests {
ResizeValue::Dimensions(None, Some(150))
);

assert_eq!(
"200 w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(200), None)
);

assert_eq!(
"150 w".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(Some(150), None)
);

assert_eq!(
"h200".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(200))
);

assert_eq!(
"h150".parse::<ResizeValue>().unwrap(),
ResizeValue::Dimensions(None, Some(150))
);

assert!("_x_".parse::<ResizeValue>().is_err());
}

Expand Down

0 comments on commit 9bd3ea2

Please sign in to comment.