Skip to content

Commit

Permalink
Replace panic with error return in BezPath::from_svg (#329)
Browse files Browse the repository at this point in the history
* Replace panic with error return in `BezPath::from_svg`
  • Loading branch information
platlas committed Jan 29, 2024
1 parent 32e67f8 commit 6ea9f53
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl BezPath {
let mut implicit_moveto = None;
while let Some(c) = lexer.get_cmd(last_cmd) {
if c != b'm' && c != b'M' {
if path.elements().is_empty() {
return Err(SvgParseError::UninitializedPath);
}

if let Some(pt) = implicit_moveto.take() {
path.move_to(pt);
}
Expand Down Expand Up @@ -242,6 +246,8 @@ pub enum SvgParseError {
UnexpectedEof,
/// Encountered an unknown command letter.
UnknownCommand(char),
/// Encountered a command that precedes expected 'moveto' command.
UninitializedPath,
}

impl Display for SvgParseError {
Expand All @@ -250,6 +256,9 @@ impl Display for SvgParseError {
SvgParseError::Wrong => write!(f, "Unable to parse a number"),
SvgParseError::UnexpectedEof => write!(f, "Unexpected EOF"),
SvgParseError::UnknownCommand(letter) => write!(f, "Unknown command, \"{letter}\""),
SvgParseError::UninitializedPath => {
write!(f, "Unititialized path (missing moveto command)")
}
}
}
}
Expand Down Expand Up @@ -517,6 +526,12 @@ mod tests {
assert_eq!(path.perimeter(1e-6).round(), 168.0);
}

#[test]
fn test_parse_svg_uninitialized() {
let path = BezPath::from_svg("L10 10 100 0 0 100");
assert!(path.is_err());
}

#[test]
fn test_write_svg_single() {
let segments = [CubicBez::new(
Expand Down

0 comments on commit 6ea9f53

Please sign in to comment.