Reworked try_get_data (#17)

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
Francesco Guardiani 2020-03-16 20:56:25 +01:00 committed by GitHub
parent 9e9122385e
commit 5766bb7d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 9 deletions

View File

@ -135,22 +135,20 @@ impl Event {
} }
} }
pub fn try_get_data<T: Sized + TryFrom<Data, Error = E>, E: std::error::Error>( pub fn try_get_data<T: Sized + TryFrom<Data>>(&self) -> Result<Option<T>, T::Error> {
&self,
) -> Option<Result<T, E>> {
match self.data.as_ref() { match self.data.as_ref() {
Some(d) => Some(T::try_from(d.clone())), Some(d) => Some(T::try_from(d.clone())),
None => None, None => None,
} }
.transpose()
} }
pub fn into_data<T: Sized + TryFrom<Data, Error = E>, E: std::error::Error>( pub fn into_data<T: Sized + TryFrom<Data>>(self) -> Result<Option<T>, T::Error> {
self,
) -> Option<Result<T, E>> {
match self.data { match self.data {
Some(d) => Some(T::try_from(d)), Some(d) => Some(T::try_from(d)),
None => None, None => None,
} }
.transpose()
} }
} }
@ -189,9 +187,7 @@ mod tests {
e.remove_data(); e.remove_data();
assert!(e assert!(e.try_get_data::<serde_json::Value>().unwrap().is_none());
.try_get_data::<serde_json::Value, serde_json::Error>()
.is_none());
assert!(e.get_dataschema().is_none()); assert!(e.get_dataschema().is_none());
assert!(e.get_datacontenttype().is_none()); assert!(e.get_datacontenttype().is_none());
} }