Skip to content

Commit

Permalink
Merge pull request #4 from Blubbz0r/ProvideFullAndNormalizedUidName
Browse files Browse the repository at this point in the history
Added normalized version of uid name
  • Loading branch information
RobertHabrich authored Oct 28, 2018
2 parents 85c2f0a + be569a3 commit 8d68fdf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
49 changes: 48 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ impl Parser {
// we'll trim them out
uid.value = uid.value.replace("\u{200b}", "");
}
1 => uid.name = text.unwrap(),
1 => {
uid.full_name = text.unwrap();
uid.normalized_name = Self::normalize_uid_name(&uid.full_name);
}
2 => match text.unwrap().as_ref() {
"Application Context Name" => uid.kind = Kind::ApplicationContextName,
"Application Hosting Model" => uid.kind = Kind::ApplicationHostingModel,
Expand Down Expand Up @@ -297,4 +300,48 @@ impl Parser {
reader.read_to_string(&mut content)?;
Ok(content)
}

fn normalize_uid_name(full_uid_name: &str) -> String {
let mut normalized_uid_name = full_uid_name.to_owned();
if normalized_uid_name.contains(":") {
let colon_index = normalized_uid_name.find(":").unwrap();
normalized_uid_name.split_off(colon_index);
normalized_uid_name.shrink_to_fit();
}

if normalized_uid_name.contains(" (Retired)") {
normalized_uid_name = normalized_uid_name.replace(" (Retired)", "");
}

normalized_uid_name
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn normalize_uid_name_doesnt_change_input_without_colon_or_retired() {
assert_eq!(
Parser::normalize_uid_name("Test String"),
"Test String".to_owned()
);
}

#[test]
fn normalize_uid_name_strips_everything_starting_at_colon() {
assert_eq!(
Parser::normalize_uid_name("Test String: With a colon"),
"Test String".to_owned()
);
}

#[test]
fn normalize_uid_name_removes_retired() {
assert_eq!(
Parser::normalize_uid_name("Test String (Retired)"),
"Test String".to_owned()
);
}
}
25 changes: 23 additions & 2 deletions src/uid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,37 @@ pub enum Kind {

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct UID {
/// The value of the UID (e.g. "1.2.840.10008.1.1" for "Verification SOP
/// Class")
pub value: String,
pub name: String,

/// The full name of the UID as given in the DICOM Standard (e.g. "Implicit
/// VR Little Endian: Default Transfer Syntax for DICOM")
pub full_name: String,

/// A normalized form of the full name. The following content is trimmed
/// from the full name:
/// * everything behind a colon (e.g. full name "Implicit VR Little Endian:
/// Default Transfer Syntax for DICOM" is trimmed down to "Implicit VR
/// Little Endian")
/// * the string " (Retired)" (e.g. "Explicit VR Big Endian (Retired)" is
/// trimmed down to "Explicit VR Big Endian")
///
/// Note that there can still be some "noise" in this due to the format of
/// the original names. Examples: "JPEG Lossless, Non-Hierarchical (Process
/// 14)" or "MPEG-4 AVC/H.264 High Profile / Level 4.2 For 2D Video".
pub normalized_name: String,

/// The type of this UID
pub kind: Kind,
}

impl UID {
pub fn new() -> Self {
UID {
value: String::new(),
name: String::new(),
normalized_name: String::new(),
full_name: String::new(),
kind: Kind::TransferSyntax,
}
}
Expand Down
21 changes: 16 additions & 5 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,18 @@ fn parse_unique_identifier_registry_from_file() {
Ok(uids) => {
assert_eq!(uids.len(), 400);

let explicit_vr_little_endian = &uids[2];
assert_eq!(explicit_vr_little_endian.value, "1.2.840.10008.1.2.1");
assert_eq!(explicit_vr_little_endian.name, "Explicit VR Little Endian");
let implicit_little_endian = &uids[1];
assert_eq!(implicit_little_endian.value, "1.2.840.10008.1.2");
assert_eq!(
explicit_vr_little_endian.kind,
implicit_little_endian.full_name,
"Implicit VR Little Endian: Default Transfer Syntax for DICOM"
);
assert_eq!(
implicit_little_endian.normalized_name,
"Implicit VR Little Endian"
);
assert_eq!(
implicit_little_endian.kind,
dict_parser::Kind::TransferSyntax
);
}
Expand All @@ -200,7 +207,11 @@ fn parse_unique_identifier_registry_from_downloaded_dict() {
// checking some random element
let verification_sop_class = &uids[0];
assert_eq!(verification_sop_class.value, "1.2.840.10008.1.1");
assert_eq!(verification_sop_class.name, "Verification SOP Class");
assert_eq!(verification_sop_class.full_name, "Verification SOP Class");
assert_eq!(
verification_sop_class.normalized_name,
"Verification SOP Class"
);
assert_eq!(verification_sop_class.kind, dict_parser::Kind::SopClass);
}
Err(e) => assert!(false, e.to_string()),
Expand Down

0 comments on commit 8d68fdf

Please sign in to comment.