Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions core/src/value/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ impl From<PersonName<'_>> for PrimitiveValue {
}
}

impl From<Cow<'_, str>> for PrimitiveValue {
fn from(value: Cow<'_, str>) -> Self {
PrimitiveValue::Str(value.into_owned())
}
}

impl From<()> for PrimitiveValue {
/// constructs an empty DICOM value
#[inline]
Expand Down Expand Up @@ -758,6 +764,102 @@ impl PrimitiveValue {
}
}

/// Convert this primitive value into a text-based primitive value.
///
/// This method consumes the original value and returns a `PrimitiveValue`
/// that represents the same data as text.
///
/// # Behavior
///
/// - All possible variants are converted to `PrimitiveValue::Str`
/// using their string representation (via [`to_raw_str()`]):
/// In the case of `Strs`,
/// the strings are first joined together with a backslash ('\\').
/// All other type variants are first converted to a string,
/// then joined together with a backslash.
///
/// # When to use `into_text_value()`
///
/// You can use `into_text_value` to
/// turn an existing DICOM value into a textual DICOM value
/// stored in a single string underneath,
/// regardless of the value's original format.
/// It is also an alternative to converting each number into strings
/// before they are encased in `PrimitiveValue`.
///
/// The methods [`to_str`] or [`to_multi_str`]
/// would be preferred when the intent is to
/// retrieve the underlying values as a string type.
///
/// [`to_str`]: PrimitiveValue::to_str
/// [`to_multi_str`]: PrimitiveValue::to_multi_str
/// [`to_raw_str()`]: PrimitiveValue::to_raw_str
///
/// # Examples
///
/// Converting numeric values to text:
///
/// ```
/// # use dicom_core::value::PrimitiveValue;
/// # use smallvec::smallvec;
/// let value = PrimitiveValue::U16(smallvec![100, 200, 300]);
/// let text_value = value.into_text_value();
///
/// assert_eq!(
/// text_value,
/// PrimitiveValue::Str("100\\200\\300".to_string())
/// );
/// ```
///
/// Converting dates to text:
///
/// ```
/// # use dicom_core::value::{PrimitiveValue, DicomDate};
/// # use smallvec::smallvec;
/// let value = PrimitiveValue::Date(
/// smallvec![DicomDate::from_ymd(2024, 12, 25).unwrap()]
/// );
/// let text_value = value.into_text_value();
///
/// assert_eq!(
/// text_value,
/// PrimitiveValue::Str("2024-12-25".to_string())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows... an interesting caveat. Dates are only DICOM-encoded correctly when converted via to_encoded, but to_raw_str does not use it (which would be fine because to_str and to_raw_str are ill-advised for DICOM serialization anyway, so they are not expected to be pushed back into a DICOM value).

The current behavior of this method will come across as surprising, so we will have to adjust it accordingly.

  • Date and date-time values in PrimitiveValue::Date and PrimitiveValue::DateTime are encoded to their standard DICOM textual form.
  • Other binary variants are converted to strings via to_string.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can work on this. For clarification, is it as simple as calling to_encoded() when appropriate and updating the docs? Or am I missing some nuance?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So yes, for the variants Da and Dt they should be converted to text via to_encoded(). The rule for concatenating multiple values into a single string would be the same (joined together with backslash as the separator).

/// );
/// ```
///
/// Text variants normalize to `Primitive::Str` but use `to_raw_str()`
/// and preserves extra spaces, etc. (no trimming that `to_str()` would do)
///
/// ```
/// # use dicom_core::value::PrimitiveValue;
/// let value = PrimitiveValue::Str("Hello ".to_string());
/// let text_value = value.into_text_value();
///
/// assert_eq!(text_value, PrimitiveValue::Str("Hello ".to_string()));
/// ```
///
/// ```
/// # use dicom_core::value::PrimitiveValue;
/// # use dicom_core::dicom_value;
/// let value = dicom_value!(Strs, ["Hello ", "World\0"]);
/// let text_value = value.into_text_value();
///
/// assert_eq!(text_value, PrimitiveValue::Str("Hello \\World\0".to_string()));
/// ```
///
/// Empty values also convert:
///
/// ```
/// # use dicom_core::value::PrimitiveValue;
/// let value = PrimitiveValue::Empty;
/// let text_value = value.into_text_value();
///
/// assert_eq!(text_value, PrimitiveValue::Str("".to_string()));
/// ```
pub fn into_text_value(self) -> PrimitiveValue {
PrimitiveValue::from(self.to_raw_str())
}

/// Retrieve this DICOM value as raw bytes.
///
/// Binary numeric values are returned with a reinterpretation
Expand Down Expand Up @@ -4460,6 +4562,55 @@ mod tests {
let value = dicom_value!(Strs, [" ONE", "TWO", "THREE", " SIX "]);
assert_eq!(&value.to_raw_str(), " ONE\\TWO\\THREE\\ SIX ");
}
#[test]
fn primitive_value_to_primitive_text() {
// Test Strs variant - option 1, use ::from() explicitly
let value = dicom_value!(Strs, ["DERIVED", "PRIMARY", "WHOLE BODY"]);
let cow_str = value.to_str();
assert_eq!(
PrimitiveValue::from(cow_str),
PrimitiveValue::Str("DERIVED\\PRIMARY\\WHOLE BODY".to_string())
);

// Test Date variant - option 2, use .into()
let value = PrimitiveValue::Date(smallvec![DicomDate::from_ymd(2014, 10, 12).unwrap()]);
let cow_str = value.to_str();
let primitive_text: PrimitiveValue = cow_str.into();
assert_eq!(
primitive_text,
PrimitiveValue::Str("2014-10-12".to_string())
);

// Test DateTime variant - option 3, use into_text_value()
let value = PrimitiveValue::DateTime(smallvec![DicomDateTime::from_date_and_time(
DicomDate::from_ymd(2012, 12, 21).unwrap(),
DicomTime::from_hms(9, 30, 1).unwrap()
)
.unwrap()]);
assert_eq!(
value.into_text_value(),
PrimitiveValue::Str("2012-12-21 09:30:01".to_string())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in past comments, dates and times (DA, TM, DT) should continue to comply with their value representation after being converted to text, as per PS3.5 section 6.2. This test, the implementation, and the documentation of the method need to be updated accordingly.

Suggested change
PrimitiveValue::Str("2012-12-21 09:30:01".to_string())
PrimitiveValue::Str("20121221093001".to_string())

);

// Test that Str and Strs get returned unchanged (no trimming)
let value = PrimitiveValue::Str("Hello ".to_string());
assert_eq!(
value.into_text_value(),
PrimitiveValue::Str("Hello ".to_string())
);

let value = dicom_value!(Strs, ["Hello ", "World\0"]);
let text_value = value.into_text_value();
assert_eq!(
text_value,
PrimitiveValue::Str("Hello \\World\0".to_string())
);

// Test that Empty converts to Str as well
let value = PrimitiveValue::Empty;
let text_value = value.into_text_value();
assert_eq!(text_value, PrimitiveValue::Str("".to_string()));
}

#[test]
fn primitive_value_to_bytes() {
Expand Down