Skip to content

Commit

Permalink
default to a nil UUID if the supplied value can't be parsed
Browse files Browse the repository at this point in the history
Fixes ixmilia#21.
  • Loading branch information
brettfo committed Jul 12, 2021
1 parent 93e3edf commit 76c334d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions spec/HeaderVariablesSpec.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@
<Variable Name="CEPSNID" Code="390" Type="Handle" Field="new_object_plot_style_handle" DefaultValue="Handle::empty()" ReaderOverride="pair.as_handle()?" WriteConverter="&amp;{}.as_string()" MinVersion="R2000" DontWriteDefault="true" Comment="PlotStyle handle of new objects." />
<Variable Name="CEPSNTYPE" Code="380" Type="PlotStyle" Field="new_object_plot_style" DefaultValue="PlotStyle::ByLayer" ReadConverter="enum_from_number!(PlotStyle, ByLayer, from_i16, {})" WriteConverter="{} as i16" MinVersion="R2000" Comment="Plot style of new objects." />
<Variable Name="PSTYLEMODE" Code="290" Type="bool" Field="uses_color_dependent_plot_style_tables" DefaultValue="true" MinVersion="R2000" Comment="Indicates whether the current drawing is in a Color-Dependent or Named Plot Style mode." />
<Variable Name="FINGERPRINTGUID" Code="2" Type="Uuid" Field="fingerprint_guid" DefaultValue="Uuid::new_v4()" ReadConverter="as_uuid({}, pair.offset)?" WriteConverter="&amp;uuid_string(&amp;{})" MinVersion="R2000" Comment="Set at creation time, uniquely identifies a particular drawing." />
<Variable Name="VERSIONGUID" Code="2" Type="Uuid" Field="version_guid" DefaultValue="Uuid::new_v4()" ReadConverter="as_uuid({}, pair.offset)?" WriteConverter="&amp;uuid_string(&amp;{})" MinVersion="R2000" Comment="Uniquely identifies a particular version of a drawing. Updated when the drawing is modified." />
<Variable Name="FINGERPRINTGUID" Code="2" Type="Uuid" Field="fingerprint_guid" DefaultValue="Uuid::new_v4()" ReadConverter="as_uuid({})" WriteConverter="&amp;uuid_string(&amp;{})" MinVersion="R2000" Comment="Set at creation time, uniquely identifies a particular drawing." />
<Variable Name="VERSIONGUID" Code="2" Type="Uuid" Field="version_guid" DefaultValue="Uuid::new_v4()" ReadConverter="as_uuid({})" WriteConverter="&amp;uuid_string(&amp;{})" MinVersion="R2000" Comment="Uniquely identifies a particular version of a drawing. Updated when the drawing is modified." />
<Variable Name="EXTNAMES" Code="290" Type="bool" Field="use_acad2000_symbol_table_naming" DefaultValue="true" MinVersion="R2000" Comment="Controls symbol table naming." />
<Variable Name="PSVPSCALE" Code="40" Type="f64" Field="viewport_view_scale_factor" DefaultValue="0.0" MinVersion="R2000" Comment="View scale factor for new viewports." />
<Variable Name="OLESTARTUP" Code="290" Type="bool" Field="ole_startup" DefaultValue="false" MinVersion="R2000" Comment="Controls whether the source application of an embedded OLE object loads when plotting." />
Expand Down
15 changes: 10 additions & 5 deletions src/helper_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uuid> {
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
Expand All @@ -128,15 +128,20 @@ pub(crate) fn as_uuid(s: String, offset: usize) -> DxfResult<Uuid> {
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 {
Expand Down

0 comments on commit 76c334d

Please sign in to comment.