From bdbc9d706ce389bf3edbd1db2cf62c5907439c73 Mon Sep 17 00:00:00 2001 From: platlas Date: Tue, 16 Jan 2024 14:12:49 +0100 Subject: [PATCH 1/2] Replace panic with error return in `BezPath::from_svg` --- src/svg.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/svg.rs b/src/svg.rs index ecdb1901..0b849ae3 100644 --- a/src/svg.rs +++ b/src/svg.rs @@ -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); } @@ -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 { @@ -250,6 +256,7 @@ 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)"), } } } @@ -517,6 +524,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( From c562ee5075cd24e614ce55eb4c8f306855981ddb Mon Sep 17 00:00:00 2001 From: platlas Date: Tue, 16 Jan 2024 14:29:47 +0100 Subject: [PATCH 2/2] forgot cargo fmt --- src/svg.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/svg.rs b/src/svg.rs index 0b849ae3..016a0660 100644 --- a/src/svg.rs +++ b/src/svg.rs @@ -103,9 +103,9 @@ impl BezPath { 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) + return Err(SvgParseError::UninitializedPath); } - + if let Some(pt) = implicit_moveto.take() { path.move_to(pt); } @@ -256,7 +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)"), + SvgParseError::UninitializedPath => { + write!(f, "Unititialized path (missing moveto command)") + } } } }