Skip to content

Commit fdc5935

Browse files
committed
[core] add dicom_value! macro
- utility for creating primitive DICOM values of variable cardinality
1 parent cafa19c commit fdc5935

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

core/src/value.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ pub enum PrimitiveValue {
216216
/// DICOM primitive value with a single element.
217217
macro_rules! impl_from_for_primitive {
218218
($typ: ty, $variant: ident) => {
219-
impl From< $typ > for PrimitiveValue {
219+
impl From<$typ> for PrimitiveValue {
220220
fn from(value: $typ) -> Self {
221-
PrimitiveValue:: $variant (C::from_elem(value, 1))
221+
PrimitiveValue::$variant(C::from_elem(value, 1))
222222
}
223223
}
224224
};
@@ -239,20 +239,33 @@ impl_from_for_primitive!(NaiveDate, Date);
239239
impl_from_for_primitive!(NaiveTime, Time);
240240
impl_from_for_primitive!(DateTime<FixedOffset>, DateTime);
241241

242+
/// Construct a DICOM value.
243+
#[macro_export]
244+
macro_rules! dicom_value {
245+
($typ: ident, [ $($elem: expr),* ]) => {
246+
{
247+
use smallvec::smallvec; // import smallvec macro
248+
dicom_core::value::PrimitiveValue :: $typ (smallvec![$($elem,)*])
249+
}
250+
};
251+
($typ: ident, $elem: expr) => {
252+
dicom_core::value::PrimitiveValue :: $typ (dicom_core::value::C::from_elem($elem, 1))
253+
};
254+
}
255+
242256
impl From<String> for PrimitiveValue {
243257
fn from(value: String) -> Self {
244258
PrimitiveValue::Str(value)
245259
}
246-
}
260+
}
247261

248262
impl From<&str> for PrimitiveValue {
249263
fn from(value: &str) -> Self {
250264
PrimitiveValue::Str(value.to_owned())
251265
}
252-
}
266+
}
253267

254268
impl PrimitiveValue {
255-
256269
/// Create a single unsigned 16-bit value.
257270
pub fn new_u16(value: u16) -> Self {
258271
PrimitiveValue::U16(C::from_elem(value, 1))

0 commit comments

Comments
 (0)