Macro dicom_core::dicom_value

source ·
macro_rules! dicom_value {
    () => { ... };
    (Strs, [ $($elem: expr),+ , ]) => { ... };
    (Strs, [ $($elem: expr),+ ]) => { ... };
    ($typ: ident, [ $($elem: expr),+ , ]) => { ... };
    ($typ: ident, [ $($elem: expr),+ ]) => { ... };
    (Str, $elem: expr) => { ... };
    ($typ: ident, $elem: expr) => { ... };
    ($elem: expr) => { ... };
}
Expand description

Helper macro for constructing a DICOM primitive value, of an arbitrary variant and multiplicity.

The base syntax is a value type identifier, which is one of the variants of PrimitiveValue, followed by either an expression resolving to one standard Rust value, or an explicitly laid out array of Rust values. The type variant may be omitted in some cases.

Passing a single expression for multiple values is not supported. Please use standard From conversions instead.

dicom_value!() // empty value
dicom_value!(«Type», «expression») // one value
dicom_value!(«Type», [«expression1», «expression2», ...]) // multiple values
dicom_value!(«expression») // a single value, inferred variant

§Examples:

Strings are automatically converted to retain ownership.

use dicom_core::value::PrimitiveValue;
use dicom_core::{DicomValue, dicom_value};

let value = dicom_value!(Str, "Smith^John");
assert_eq!(
    value,
    PrimitiveValue::Str("Smith^John".to_owned()),
);

A DICOM value may also have multiple elements:

let value = dicom_value!(Strs, [
    "Smith^John",
    "Simões^João",
]);
assert_eq!(
    value,
    PrimitiveValue::Strs([
        "Smith^John".to_string(),
        "Simões^João".to_string(),
    ][..].into()),
);
let value = dicom_value!(U16, [5, 6, 7]);
assert_eq!(
    value,
    PrimitiveValue::U16([5, 6, 7][..].into()),
);

The output is a PrimitiveValue, which can be converted to a DicomValue as long as its type parameters are specified or inferable.

// conversion to a DicomValue only requires its type parameters
// to be specified or inferable.
assert_eq!(
    DicomValue::from(value),
    DicomValue::<EmptyObject, ()>::Primitive(
        PrimitiveValue::U16([5, 6, 7][..].into())),
);