Skip to content

Commit

Permalink
fix(cli): 🐛 improved resize value parsing by trimming input and refin…
Browse files Browse the repository at this point in the history
…ing error handling for invalid formats
  • Loading branch information
SalOne22 committed Aug 20, 2024
1 parent 9bd3ea2 commit 2a43c26
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cli/preprocessors/resize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,23 @@ impl std::str::FromStr for ResizeValue {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim().to_lowercase();

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.contains('w') && s.contains('h') => Err(anyhow!("Invalid resize value")),
s if s.contains('w') => {
let re = Regex::new(r"(?P<width>\d+)").unwrap();
let Some(cap) = re.captures(s) else {
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 {
let Some(cap) = re.captures(&s) else {
return Err(anyhow!("Invalid resize value"));
};

Expand Down Expand Up @@ -199,6 +202,7 @@ mod tests {
);

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

#[test]
Expand Down

0 comments on commit 2a43c26

Please sign in to comment.