Skip to content

Commit ba9f784

Browse files
committed
fix: Remove SVG panic paths
1 parent ad8ae57 commit ba9f784

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

support/src/svg_manage.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn build_svg(
4141
return Err(svg_error(svg_path));
4242
};
4343

44-
let svg_id_to_title = correlate_id_to_title(&contents);
44+
let svg_id_to_title = correlate_id_to_title(&contents)?;
4545

4646
let tree = usvg::Tree::from_str(&contents, &usvg::Options::default()).unwrap();
4747

@@ -199,12 +199,14 @@ fn parse_title(title: &str) -> Option<u16> {
199199

200200
let row_h = row_h as u16;
201201

202-
assert_eq!(sections.next(), None, "Title contained too many groups");
202+
if sections.next() != None {
203+
println!("Title contained too many groups");
204+
}
203205

204206
return Some((segment << 6) | (column << 2) | row_h);
205207
}
206208

207-
fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
209+
fn correlate_id_to_title(contents: &String) -> Result<HashMap<String, u16>, String> {
208210
let mut svg_id_to_title: HashMap<String, u16> = HashMap::new();
209211

210212
#[derive(PartialEq, Debug)]
@@ -237,15 +239,16 @@ fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
237239
svg::parser::Event::Tag("path", Type::Start, attributes) => {
238240
let id: Option<String> = attributes.get("id").map(|v| v.clone().into());
239241

240-
assert_eq!(
241-
active_path, None,
242-
"SVG contains invalid nested paths at {id:?}",
243-
);
242+
if active_path != None {
243+
return Err(format!("SVG contains invalid nested paths at {id:?}"));
244+
}
244245

245246
active_path = Some(ActivePath { id, title: None });
246247
}
247248
svg::parser::Event::Tag("title", Type::Start, _) => {
248-
assert_eq!(inside_title, false, "SVG contains invalid nested titles");
249+
if inside_title {
250+
return Err("SVG contains invalid nested titles".into());
251+
}
249252

250253
inside_title = true;
251254
}
@@ -263,7 +266,9 @@ fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
263266
}
264267
}
265268
svg::parser::Event::Tag("title", Type::End, _) => {
266-
assert_eq!(inside_title, true, "SVG contains an invalid end title tag");
269+
if !inside_title {
270+
return Err("SVG contains an invalid end title tag".into());
271+
}
267272

268273
inside_title = false;
269274
}
@@ -279,7 +284,7 @@ fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
279284
}
280285

281286
guard!(let Some(path) = &active_path else {
282-
panic!("SVG contains an invalid unterminated path");
287+
return Err("SVG contains an invalid unterminated path".into());
283288
});
284289

285290
guard!(let Some(id) = &path.id else {
@@ -301,7 +306,7 @@ fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
301306
}
302307
svg::parser::Event::Tag("g", Type::End, _) => {
303308
guard!(let Some(group) = group_stack.last() else {
304-
panic!("SVG contains an invalid end group tag");
309+
return Err("SVG contains an invalid end group tag".into());
305310
});
306311

307312
if let Some(ancestor_or_self) = group_stack.iter().rev().find(|g| g.title.is_some())
@@ -323,7 +328,7 @@ fn correlate_id_to_title(contents: &String) -> HashMap<String, u16> {
323328
}
324329
}
325330

326-
svg_id_to_title
331+
Ok(svg_id_to_title)
327332
}
328333

329334
fn keep_usvg_node(node: &usvg::Node, svg_id_to_title: &HashMap<String, u16>) -> bool {

0 commit comments

Comments
 (0)