Skip to content

Commit 40cf311

Browse files
committed
[core] add utility functions to PrimitiveValue
1 parent cf02290 commit 40cf311

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

core/src/value.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,62 @@ pub enum PrimitiveValue {
212212
Time(C<NaiveTime>),
213213
}
214214

215+
/// A utility macro for implementing the conversion from a core type into a
216+
/// DICOM primitive value with a single element.
217+
macro_rules! impl_from_for_primitive {
218+
($typ: ty, $variant: ident) => {
219+
impl From< $typ > for PrimitiveValue {
220+
fn from(value: $typ) -> Self {
221+
PrimitiveValue:: $variant (C::from_elem(value, 1))
222+
}
223+
}
224+
};
225+
}
226+
227+
impl_from_for_primitive!(u8, U8);
228+
impl_from_for_primitive!(u16, U16);
229+
impl_from_for_primitive!(i16, I16);
230+
impl_from_for_primitive!(u32, U32);
231+
impl_from_for_primitive!(i32, I32);
232+
impl_from_for_primitive!(u64, U64);
233+
impl_from_for_primitive!(i64, I64);
234+
impl_from_for_primitive!(f32, F32);
235+
impl_from_for_primitive!(f64, F64);
236+
237+
impl_from_for_primitive!(Tag, Tags);
238+
impl_from_for_primitive!(NaiveDate, Date);
239+
impl_from_for_primitive!(NaiveTime, Time);
240+
impl_from_for_primitive!(DateTime<FixedOffset>, DateTime);
241+
242+
impl From<String> for PrimitiveValue {
243+
fn from(value: String) -> Self {
244+
PrimitiveValue::Str(value)
245+
}
246+
}
247+
248+
impl From<&str> for PrimitiveValue {
249+
fn from(value: &str) -> Self {
250+
PrimitiveValue::Str(value.to_owned())
251+
}
252+
}
253+
215254
impl PrimitiveValue {
255+
256+
/// Create a single unsigned 16-bit value.
257+
pub fn new_u16(value: u16) -> Self {
258+
PrimitiveValue::U16(C::from_elem(value, 1))
259+
}
260+
261+
/// Create a single unsinged 32 value.
262+
pub fn new_u32(value: u32) -> Self {
263+
PrimitiveValue::U32(C::from_elem(value, 1))
264+
}
265+
266+
/// Create a single I32 value.
267+
pub fn new_i32(value: u32) -> Self {
268+
PrimitiveValue::U32(C::from_elem(value, 1))
269+
}
270+
216271
/// Obtain the number of individual elements. This number may not
217272
/// match the DICOM value multiplicity in some value representations.
218273
pub fn multiplicity(&self) -> u32 {

0 commit comments

Comments
 (0)