From 0265adbafcd9b4113f9a861eb5de89f8ad2c2982 Mon Sep 17 00:00:00 2001 From: Tpt Date: Thu, 27 Feb 2020 18:03:57 +0100 Subject: [PATCH] Avoids unwrap() in From implementations --- lib/src/lib.rs | 4 +- lib/src/model/xsd/date_time.rs | 85 ++++++++++++++++++---------------- lib/src/sparql/eval.rs | 6 +-- 3 files changed, 51 insertions(+), 44 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 32d850c5..51dee0fa 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.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, diff --git a/lib/src/model/xsd/date_time.rs b/lib/src/model/xsd/date_time.rs index 44b9fb00..fce3cea3 100644 --- a/lib/src/model/xsd/date_time.rs +++ b/lib/src/model/xsd/date_time.rs @@ -142,8 +142,10 @@ impl DateTime { } /// Conversion according to [XPath cast rules](https://www.w3.org/TR/xpath-functions/#casting-to-datetimes). -impl From for DateTime { - fn from(date: Date) -> Self { +impl TryFrom for DateTime { + type Error = DateTimeError; + + fn try_from(date: Date) -> Result { DateTime::new( date.year(), date.month(), @@ -153,7 +155,6 @@ impl From 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) -> Option { - 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) -> Option { - 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 for Time { - fn from(date_time: DateTime) -> Self { +impl TryFrom for Time { + type Error = DateTimeError; + + fn try_from(date_time: DateTime) -> Result { 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) -> Option { - 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) -> Option { - 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 for Date { - fn from(date_time: DateTime) -> Self { +impl TryFrom for Date { + type Error = DateTimeError; + + fn try_from(date_time: DateTime) -> Result { Date::new( date_time.year(), date_time.month(), date_time.day(), date_time.timezone_offset(), ) - .unwrap() } } diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index b01e0b68..79c74482 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -1357,7 +1357,7 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator { }, 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 { }, 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 { }, 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()??) }