|
1 | | -use super::{Attributes, Data, DataDecoder, DataEncoder, AttributesReader, AttributesWriter, SpecVersion, ExtensionValue}; |
2 | | -use delegate::delegate; |
| 1 | +use super::{Attributes, AttributesReader, AttributesWriter, Data, ExtensionValue, SpecVersion}; |
3 | 2 | use chrono::{DateTime, FixedOffset}; |
| 3 | +use delegate::delegate; |
| 4 | +use std::convert::{TryFrom, TryInto}; |
4 | 5 |
|
5 | 6 | pub struct Event { |
6 | 7 | pub attributes: Attributes, |
@@ -47,27 +48,38 @@ impl AttributesWriter for Event { |
47 | 48 | } |
48 | 49 | } |
49 | 50 |
|
50 | | -pub trait DataWriter<T: Sized, E: std::error::Error, D: DataEncoder<T, E>> |
51 | | - where |
52 | | - Self: Sized, |
53 | | -{ |
54 | | - fn write_payload(&mut self, value: &T) -> Result<(), E>; |
55 | | -} |
| 51 | +impl Event { |
| 52 | + fn remove_data(&mut self) { |
| 53 | + self.data = None; |
| 54 | + } |
56 | 55 |
|
57 | | -pub trait DataReader<T: Sized, E: std::error::Error, D: DataDecoder<T, E>> { |
58 | | - fn read_data(&self) -> Option<Result<&T, E>>; |
59 | | -} |
| 56 | + fn write_data(&mut self, v: impl Into<Data>) { |
| 57 | + self.data = Some(v.into()); |
| 58 | + } |
60 | 59 |
|
61 | | -impl <T: Sized, E: std::error::Error, D: DataEncoder<T, E>> DataWriter<T, E, D> for Event { |
62 | | - fn write_payload(&mut self, value: &T) -> Result<(), E> { |
63 | | - self.data = Some(D::encode_data(value)?); |
| 60 | + fn try_write_data<E: std::error::Error>( |
| 61 | + &mut self, |
| 62 | + v: impl TryInto<Data, Error = E>, |
| 63 | + ) -> Result<(), E> { |
| 64 | + self.data = Some(v.try_into()?); |
64 | 65 | Ok(()) |
65 | 66 | } |
66 | | -} |
67 | 67 |
|
68 | | -impl <T: Sized, E: std::error::Error, D: DataDecoder<T, E>> DataReader<T, E, D> for Event { |
69 | | - fn read_data(&self) -> Option<Result<&T, E>> { |
70 | | - self.data.as_ref().map(D::decode_data) |
| 68 | + fn get_data<T: Sized + TryFrom<Data, Error = E>, E: std::error::Error>( |
| 69 | + &self, |
| 70 | + ) -> Option<Result<T, E>> { |
| 71 | + match self.data.as_ref() { |
| 72 | + Some(d) => Some(T::try_from(d.clone())), |
| 73 | + None => None, |
| 74 | + } |
71 | 75 | } |
72 | | -} |
73 | 76 |
|
| 77 | + fn into_data<T: Sized + TryFrom<Data, Error = E>, E: std::error::Error>( |
| 78 | + self, |
| 79 | + ) -> Option<Result<T, E>> { |
| 80 | + match self.data { |
| 81 | + Some(d) => Some(T::try_from(d)), |
| 82 | + None => None, |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments