dicom_object/
ops.rs

1//! Baseline attribute operation implementations.
2//!
3//! See the [`dicom_core::ops`] module
4//! for more information.
5
6use dicom_core::ops::{ApplyOp, AttributeOp, AttributeSelector, AttributeSelectorStep};
7use dicom_core::value::{ModifyValueError, ValueType};
8use dicom_core::Tag;
9use snafu::Snafu;
10
11use crate::FileDicomObject;
12
13/// An error which may occur when applying an attribute operation to an object.
14#[derive(Debug, Snafu)]
15#[non_exhaustive]
16#[snafu(visibility(pub(crate)))]
17pub enum ApplyError {
18    /// Missing intermediate sequence for {selector} at step {step_index}
19    MissingSequence {
20        selector: AttributeSelector,
21        step_index: u32,
22    },
23    /// Step {step_index} for {selector} is not a data set sequence
24    NotASequence {
25        selector: AttributeSelector,
26        step_index: u32,
27    },
28    /// Incompatible source element type {kind:?} for extension
29    IncompatibleTypes {
30        /// the source element value type
31        kind: ValueType,
32    },
33    /// Illegal removal of mandatory attribute
34    Mandatory,
35    /// Could not modify source element type through extension
36    Modify { source: ModifyValueError },
37    /// Illegal extension of fixed cardinality attribute
38    IllegalExtend,
39    /// Unsupported action
40    UnsupportedAction,
41    /// Unsupported attribute insertion
42    UnsupportedAttribute,
43}
44
45/// Result type for when applying attribute operations to an object.
46pub type ApplyResult<T = (), E = ApplyError> = std::result::Result<T, E>;
47
48impl<T> ApplyOp for FileDicomObject<T>
49where
50    T: ApplyOp<Err = ApplyError>,
51{
52    type Err = ApplyError;
53
54    /// Apply the given attribute operation on this object.
55    ///
56    /// The operation is delegated to the file meta table
57    /// if the selector root tag is in group `0002`,
58    /// and to the underlying object otherwise.
59    ///
60    /// See the [`dicom_core::ops`] module
61    /// for more information.
62    fn apply(&mut self, op: AttributeOp) -> ApplyResult {
63        if let AttributeSelectorStep::Tag(Tag(0x0002, _)) = op.selector.first_step() {
64            self.meta.apply(op)
65        } else {
66            self.obj.apply(op)
67        }
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use dicom_core::ops::{ApplyOp, AttributeAction, AttributeOp};
74    use dicom_core::{DataElement, PrimitiveValue, VR};
75
76    use crate::{FileMetaTableBuilder, InMemDicomObject};
77
78    /// Attribute operations can be applied on a `FileDicomObject<InMemDicomObject>`
79    #[test]
80    fn file_dicom_object_can_apply_op() {
81        let mut obj = InMemDicomObject::new_empty();
82
83        obj.put(DataElement::new(
84            dicom_dictionary_std::tags::PATIENT_NAME,
85            VR::PN,
86            PrimitiveValue::from("John Doe"),
87        ));
88
89        let mut obj = obj
90            .with_meta(
91                FileMetaTableBuilder::new()
92                    .media_storage_sop_class_uid("1.2.840.10008.5.1.4.1.1.7")
93                    .media_storage_sop_instance_uid("1.2.23456789")
94                    .transfer_syntax("1.2.840.10008.1.2.1"),
95            )
96            .unwrap();
97
98        // apply operation on main data set
99        obj.apply(AttributeOp {
100            selector: dicom_dictionary_std::tags::PATIENT_NAME.into(),
101            action: AttributeAction::SetStr("Patient^Anonymous".into()),
102        })
103        .unwrap();
104
105        // contains new patient name
106        assert_eq!(
107            obj.element(dicom_dictionary_std::tags::PATIENT_NAME)
108                .unwrap()
109                .value()
110                .to_str()
111                .unwrap(),
112            "Patient^Anonymous",
113        );
114
115        // apply operation on file meta information
116        obj.apply(AttributeOp {
117            selector: dicom_dictionary_std::tags::MEDIA_STORAGE_SOP_INSTANCE_UID.into(),
118            action: AttributeAction::SetStr("2.25.153241429675951194530939969687300037165".into()),
119        })
120        .unwrap();
121
122        // file meta table contains new SOP instance UID
123        assert_eq!(
124            obj.meta().media_storage_sop_instance_uid(),
125            "2.25.153241429675951194530939969687300037165",
126        );
127    }
128}