Skip to content

Commit

Permalink
Merge pull request #526 from Enet4/imp/core/date-time-value-ergonomics
Browse files Browse the repository at this point in the history
Improve value ergonomics and improve performance of TryFrom around date/time types
  • Loading branch information
Enet4 authored Jul 5, 2024
2 parents 8b4b676 + 0243863 commit 69dc58c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
21 changes: 21 additions & 0 deletions core/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ impl<I, P> From<String> for Value<I, P> {
}
}

impl<I, P> From<DicomDate> for Value<I, P> {
/// Converts the DICOM date into a primitive value.
fn from(value: DicomDate) -> Self {
Value::Primitive(PrimitiveValue::from(value))
}
}

impl<I, P> From<DicomTime> for Value<I, P> {
/// Converts the DICOM time into a primitive value.
fn from(value: DicomTime) -> Self {
Value::Primitive(PrimitiveValue::from(value))
}
}

impl<I, P> From<DicomDateTime> for Value<I, P> {
/// Converts the DICOM date-time into a primitive value.
fn from(value: DicomDateTime) -> Self {
Value::Primitive(PrimitiveValue::from(value))
}
}

impl<I, P> HasLength for Value<I, P> {
fn length(&self) -> Length {
match self {
Expand Down
36 changes: 18 additions & 18 deletions core/src/value/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,15 @@ impl DicomDate {
impl TryFrom<&NaiveDate> for DicomDate {
type Error = Error;
fn try_from(date: &NaiveDate) -> Result<Self> {
let year: u16 = date.year().try_into().context(ConversionSnafu {
let year: u16 = date.year().try_into().with_context(|_| ConversionSnafu {
value: date.year().to_string(),
component: DateComponent::Year,
})?;
let month: u8 = date.month().try_into().context(ConversionSnafu {
let month: u8 = date.month().try_into().with_context(|_| ConversionSnafu {
value: date.month().to_string(),
component: DateComponent::Month,
})?;
let day: u8 = date.day().try_into().context(ConversionSnafu {
let day: u8 = date.day().try_into().with_context(|_| ConversionSnafu {
value: date.day().to_string(),
component: DateComponent::Day,
})?;
Expand Down Expand Up @@ -542,15 +542,15 @@ impl DicomTime {
impl TryFrom<&NaiveTime> for DicomTime {
type Error = Error;
fn try_from(time: &NaiveTime) -> Result<Self> {
let hour: u8 = time.hour().try_into().context(ConversionSnafu {
let hour: u8 = time.hour().try_into().with_context(|_| ConversionSnafu {
value: time.hour().to_string(),
component: DateComponent::Hour,
})?;
let minute: u8 = time.minute().try_into().context(ConversionSnafu {
let minute: u8 = time.minute().try_into().with_context(|_| ConversionSnafu {
value: time.minute().to_string(),
component: DateComponent::Minute,
})?;
let second: u8 = time.second().try_into().context(ConversionSnafu {
let second: u8 = time.second().try_into().with_context(|_| ConversionSnafu {
value: time.second().to_string(),
component: DateComponent::Second,
})?;
Expand Down Expand Up @@ -700,27 +700,27 @@ impl DicomDateTime {
impl TryFrom<&DateTime<FixedOffset>> for DicomDateTime {
type Error = Error;
fn try_from(dt: &DateTime<FixedOffset>) -> Result<Self> {
let year: u16 = dt.year().try_into().context(ConversionSnafu {
let year: u16 = dt.year().try_into().with_context(|_| ConversionSnafu {
value: dt.year().to_string(),
component: DateComponent::Year,
})?;
let month: u8 = dt.month().try_into().context(ConversionSnafu {
let month: u8 = dt.month().try_into().with_context(|_| ConversionSnafu {
value: dt.month().to_string(),
component: DateComponent::Month,
})?;
let day: u8 = dt.day().try_into().context(ConversionSnafu {
let day: u8 = dt.day().try_into().with_context(|_| ConversionSnafu {
value: dt.day().to_string(),
component: DateComponent::Day,
})?;
let hour: u8 = dt.hour().try_into().context(ConversionSnafu {
let hour: u8 = dt.hour().try_into().with_context(|_| ConversionSnafu {
value: dt.hour().to_string(),
component: DateComponent::Hour,
})?;
let minute: u8 = dt.minute().try_into().context(ConversionSnafu {
let minute: u8 = dt.minute().try_into().with_context(|_| ConversionSnafu {
value: dt.minute().to_string(),
component: DateComponent::Minute,
})?;
let second: u8 = dt.second().try_into().context(ConversionSnafu {
let second: u8 = dt.second().try_into().with_context(|_| ConversionSnafu {
value: dt.second().to_string(),
component: DateComponent::Second,
})?;
Expand All @@ -743,27 +743,27 @@ impl TryFrom<&DateTime<FixedOffset>> for DicomDateTime {
impl TryFrom<&NaiveDateTime> for DicomDateTime {
type Error = Error;
fn try_from(dt: &NaiveDateTime) -> Result<Self> {
let year: u16 = dt.year().try_into().context(ConversionSnafu {
let year: u16 = dt.year().try_into().with_context(|_| ConversionSnafu {
value: dt.year().to_string(),
component: DateComponent::Year,
})?;
let month: u8 = dt.month().try_into().context(ConversionSnafu {
let month: u8 = dt.month().try_into().with_context(|_| ConversionSnafu {
value: dt.month().to_string(),
component: DateComponent::Month,
})?;
let day: u8 = dt.day().try_into().context(ConversionSnafu {
let day: u8 = dt.day().try_into().with_context(|_| ConversionSnafu {
value: dt.day().to_string(),
component: DateComponent::Day,
})?;
let hour: u8 = dt.hour().try_into().context(ConversionSnafu {
let hour: u8 = dt.hour().try_into().with_context(|_| ConversionSnafu {
value: dt.hour().to_string(),
component: DateComponent::Hour,
})?;
let minute: u8 = dt.minute().try_into().context(ConversionSnafu {
let minute: u8 = dt.minute().try_into().with_context(|_| ConversionSnafu {
value: dt.minute().to_string(),
component: DateComponent::Minute,
})?;
let second: u8 = dt.second().try_into().context(ConversionSnafu {
let second: u8 = dt.second().try_into().with_context(|_| ConversionSnafu {
value: dt.second().to_string(),
component: DateComponent::Second,
})?;
Expand Down

0 comments on commit 69dc58c

Please sign in to comment.