From 76c334dce6bd863b847882a9581b1fdba67d2b68 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Mon, 12 Jul 2021 10:48:32 -0600 Subject: [PATCH] default to a nil UUID if the supplied value can't be parsed Fixes #21. --- spec/HeaderVariablesSpec.xml | 4 ++-- src/helper_functions.rs | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/spec/HeaderVariablesSpec.xml b/spec/HeaderVariablesSpec.xml index 2be0f12..6932c73 100644 --- a/spec/HeaderVariablesSpec.xml +++ b/spec/HeaderVariablesSpec.xml @@ -237,8 +237,8 @@ - - + + diff --git a/src/helper_functions.rs b/src/helper_functions.rs index 13983f4..065171f 100644 --- a/src/helper_functions.rs +++ b/src/helper_functions.rs @@ -115,7 +115,7 @@ pub(crate) fn as_duration(d: f64) -> StdDuration { StdDuration::from_secs(d as u64) } -pub(crate) fn as_uuid(s: String, offset: usize) -> DxfResult { +pub(crate) fn as_uuid(s: String) -> Uuid { let mut reconstructed = String::new(); let s = if s.starts_with('{') && s.ends_with('}') { // reconstruct the string without the braces @@ -128,15 +128,20 @@ pub(crate) fn as_uuid(s: String, offset: usize) -> DxfResult { s.as_str() }; match Uuid::parse_str(s) { - Ok(uuid) => Ok(uuid), - Err(_) => Err(DxfError::ParseError(offset)), + Ok(uuid) => uuid, + Err(_) => Uuid::nil(), } } #[test] fn parse_regular_and_windows_style_uuids_test() { - let _regular = as_uuid(String::from("a2a7a23e-975b-4b54-968c-150d4c32a9b6"), 0).unwrap(); - let _windows = as_uuid(String::from("{a2a7a23e-975b-4b54-968c-150d4c32a9b6}"), 0).unwrap(); + let _regular = as_uuid(String::from("a2a7a23e-975b-4b54-968c-150d4c32a9b6")); + let _windows = as_uuid(String::from("{a2a7a23e-975b-4b54-968c-150d4c32a9b6}")); +} + +#[test] +fn parse_empty_uuid_test() { + let _empty = as_uuid(String::from("")); } pub(crate) fn as_i16(b: bool) -> i16 {