-
Notifications
You must be signed in to change notification settings - Fork 114
Add into_text_value() to PrimitiveValues #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
techtenk
wants to merge
4
commits into
Enet4:master
Choose a base branch
from
techtenk:techtenk/into_text_value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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] | ||||||
|
|
@@ -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()) | ||||||
| /// ); | ||||||
| /// ``` | ||||||
| /// | ||||||
| /// 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 | ||||||
|
|
@@ -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()) | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in past comments, dates and times (
Suggested change
|
||||||
| ); | ||||||
|
|
||||||
| // 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() { | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, butto_raw_strdoes not use it (which would be fine becauseto_strandto_raw_strare 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.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
DaandDtthey should be converted to text viato_encoded(). The rule for concatenating multiple values into a single string would be the same (joined together with backslash as the separator).