Avoids unwrap() in From implementations

pull/23/head
Tpt 5 years ago
parent ee39f162aa
commit 0265adbafc
  1. 4
      lib/src/lib.rs
  2. 85
      lib/src/model/xsd/date_time.rs
  3. 6
      lib/src/sparql/eval.rs

@ -59,7 +59,7 @@
clippy::expl_impl_clone_on_copy,
clippy::explicit_into_iter_loop,
clippy::explicit_iter_loop,
//TODO clippy::fallible_impl_from,
clippy::fallible_impl_from,
clippy::filter_map,
clippy::filter_map_next,
clippy::find_map,
@ -97,7 +97,7 @@
clippy::todo,
clippy::type_repetition_in_bounds,
clippy::unicode_not_nfc,
//TODO clippy::unseparated_literal_suffix,
clippy::unseparated_literal_suffix,
clippy::used_underscore_binding,
clippy::wildcard_dependencies,
clippy::wrong_pub_self_convention,

@ -142,8 +142,10 @@ impl DateTime {
}
/// Conversion according to [XPath cast rules](https://www.w3.org/TR/xpath-functions/#casting-to-datetimes).
impl From<Date> for DateTime {
fn from(date: Date) -> Self {
impl TryFrom<Date> for DateTime {
type Error = DateTimeError;
fn try_from(date: Date) -> Result<Self, DateTimeError> {
DateTime::new(
date.year(),
date.month(),
@ -153,7 +155,6 @@ impl From<Date> for DateTime {
Decimal::default(),
date.timezone_offset(),
)
.unwrap()
}
}
@ -258,53 +259,50 @@ impl Time {
/// [op:add-dayTimeDuration-to-time](https://www.w3.org/TR/xpath-functions/#func-add-dayTimeDuration-to-time)
pub fn checked_add_duration(&self, rhs: impl Into<Duration>) -> Option<Self> {
let rhs = rhs.into();
Some(
DateTime::new(
1972,
12,
31,
self.hour(),
self.minute(),
self.second(),
self.timezone_offset(),
)
.ok()?
.checked_add_duration(rhs)?
.into(),
DateTime::new(
1972,
12,
31,
self.hour(),
self.minute(),
self.second(),
self.timezone_offset(),
)
.ok()?
.checked_add_duration(rhs)?
.try_into()
.ok()
}
/// [op:sub-dayTimeDuration-from-time](https://www.w3.org/TR/xpath-functions/#func-sub-dayTimeDuration-from-time)
pub fn checked_sub_duration(&self, rhs: impl Into<Duration>) -> Option<Self> {
let rhs = rhs.into();
Some(
DateTime::new(
1972,
12,
31,
self.hour(),
self.minute(),
self.second(),
self.timezone_offset(),
)
.ok()?
.checked_sub_duration(rhs)?
.into(),
DateTime::new(
1972,
12,
31,
self.hour(),
self.minute(),
self.second(),
self.timezone_offset(),
)
.ok()?
.checked_sub_duration(rhs)?
.try_into()
.ok()
}
}
/// Conversion according to [XPath cast rules](https://www.w3.org/TR/xpath-functions/#casting-to-datetimes).
impl From<DateTime> for Time {
fn from(date_time: DateTime) -> Self {
impl TryFrom<DateTime> for Time {
type Error = DateTimeError;
fn try_from(date_time: DateTime) -> Result<Self, DateTimeError> {
Time::new(
date_time.hour(),
date_time.minute(),
date_time.second(),
date_time.timezone_offset(),
)
.unwrap()
}
}
@ -399,25 +397,34 @@ impl Date {
/// [op:add-yearMonthDuration-to-date](https://www.w3.org/TR/xpath-functions/#func-add-yearMonthDuration-to-date) and [op:add-dayTimeDuration-to-dateTime](https://www.w3.org/TR/xpath-functions/#func-add-dayTimeDuration-to-date)
pub fn checked_add_duration(&self, rhs: impl Into<Duration>) -> Option<Self> {
Some(DateTime::from(*self).checked_add_duration(rhs)?.into())
DateTime::try_from(*self)
.ok()?
.checked_add_duration(rhs)?
.try_into()
.ok()
}
/// [op:sub-yearMonthDuration-from-date](https://www.w3.org/TR/xpath-functions/#func-sub-yearMonthDuration-from-date) and [op:sub-dayTimeDuration-from-date](https://www.w3.org/TR/xpath-functions/#func-sub-dayTimeDuration-from-date)
pub fn checked_sub_duration(&self, rhs: impl Into<Duration>) -> Option<Self> {
Some(DateTime::from(*self).checked_sub_duration(rhs)?.into())
DateTime::try_from(*self)
.ok()?
.checked_sub_duration(rhs)?
.try_into()
.ok()
}
}
/// Conversion according to [XPath cast rules](https://www.w3.org/TR/xpath-functions/#casting-to-datetimes).
impl From<DateTime> for Date {
fn from(date_time: DateTime) -> Self {
impl TryFrom<DateTime> for Date {
type Error = DateTimeError;
fn try_from(date_time: DateTime) -> Result<Self, DateTimeError> {
Date::new(
date_time.year(),
date_time.month(),
date_time.day(),
date_time.timezone_offset(),
)
.unwrap()
}
}

@ -1357,7 +1357,7 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
},
PlanExpression::DateCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::DateLiteral(value) => Some(value.into()),
EncodedTerm::DateTimeLiteral(value) => Some(Date::from(value).into()),
EncodedTerm::DateTimeLiteral(value) => Some(Date::try_from(value).ok()?.into()),
EncodedTerm::StringLiteral { value_id } => {
parse_date_str(&*self.dataset.get_str(value_id).ok()??)
}
@ -1365,7 +1365,7 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
},
PlanExpression::TimeCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::TimeLiteral(value) => Some(value.into()),
EncodedTerm::DateTimeLiteral(value) => Some(Time::from(value).into()),
EncodedTerm::DateTimeLiteral(value) => Some(Time::try_from(value).ok()?.into()),
EncodedTerm::StringLiteral { value_id } => {
parse_time_str(&*self.dataset.get_str(value_id).ok()??)
}
@ -1373,7 +1373,7 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
},
PlanExpression::DateTimeCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::DateTimeLiteral(value) => Some(value.into()),
EncodedTerm::DateLiteral(value) => Some(DateTime::from(value).into()),
EncodedTerm::DateLiteral(value) => Some(DateTime::try_from(value).ok()?.into()),
EncodedTerm::StringLiteral { value_id } => {
parse_date_time_str(&*self.dataset.get_str(value_id).ok()??)
}

Loading…
Cancel
Save