Skip to content

Commit 0c91170

Browse files
Fixed data read/write methods
Added extensions creators Signed-off-by: Francesco Guardiani <[email protected]>
1 parent fcea76c commit 0c91170

File tree

7 files changed

+109
-77
lines changed

7 files changed

+109
-77
lines changed

src/event/attributes.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ pub trait AttributesWriter {
3131
extension_name: &'event str,
3232
extension_value: impl Into<ExtensionValue>,
3333
);
34-
fn remove_extension<'event>(&'event mut self, extension_name: &'event str) -> Option<ExtensionValue>;
34+
fn remove_extension<'event>(
35+
&'event mut self,
36+
extension_name: &'event str,
37+
) -> Option<ExtensionValue>;
3538
}
3639

3740
pub enum Attributes {
@@ -138,7 +141,10 @@ impl AttributesWriter for Attributes {
138141
}
139142
}
140143

141-
fn remove_extension<'event>(&'event mut self, extension_name: &'event str) -> Option<ExtensionValue> {
144+
fn remove_extension<'event>(
145+
&'event mut self,
146+
extension_name: &'event str,
147+
) -> Option<ExtensionValue> {
142148
match self {
143149
Attributes::V10(a) => a.remove_extension(extension_name),
144150
}

src/event/data.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use serde_json::Value;
3+
use std::convert::TryInto;
34

45
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
56
/// Possible data values
@@ -76,12 +77,21 @@ pub enum Data {
7677
// }
7778
// }
7879

79-
//TODO this should be generified for event?!
80-
81-
pub trait DataEncoder<T: Sized, E: std::error::Error> {
82-
fn encode_data(value: &T) -> Result<Data, E>;
80+
impl Into<Data> for String {
81+
fn into(self) -> Data {
82+
Data::String(self)
83+
}
8384
}
8485

85-
pub trait DataDecoder<T: Sized, E: std::error::Error> {
86-
fn decode_data(data: &Data) -> Result<&T, E>;
86+
// TODO Define an error here?
87+
impl TryInto<String> for Data {
88+
type Error = ();
89+
90+
fn try_into(self) -> Result<String, Self::Error> {
91+
match self {
92+
Data::String(s) => Ok(s),
93+
Data::Binary(v) => String::from_utf8(v).map_err(|e| ()),
94+
_ => Err(()),
95+
}
96+
}
8797
}

src/event/event.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::{Attributes, Data, DataDecoder, DataEncoder, AttributesReader, AttributesWriter, SpecVersion, ExtensionValue};
2-
use delegate::delegate;
1+
use super::{Attributes, AttributesReader, AttributesWriter, Data, ExtensionValue, SpecVersion};
32
use chrono::{DateTime, FixedOffset};
3+
use delegate::delegate;
4+
use std::convert::{TryFrom, TryInto};
45

56
pub struct Event {
67
pub attributes: Attributes,
@@ -47,27 +48,38 @@ impl AttributesWriter for Event {
4748
}
4849
}
4950

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+
}
5655

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+
}
6059

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()?);
6465
Ok(())
6566
}
66-
}
6767

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+
}
7175
}
72-
}
7376

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+
}

src/event/extensions.rs

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,45 @@ pub enum ExtensionValue {
1717
Object(Value),
1818
}
1919

20-
// impl ExtensionValue {
21-
// /// Create an [`ExtensionValue`] from a [`Into<String>`].
22-
// ///
23-
// /// # Example
24-
// ///
25-
// /// ```
26-
// /// use cloudevents::ExtensionValue;
27-
// ///
28-
// /// let value = ExtensionValue::from_string("value");
29-
// /// assert_eq!(value, ExtensionValue::String("value".to_owned()));
30-
// /// ```
31-
// ///
32-
// /// [`Into<String>`]: https://doc.rust-lang.org/std/convert/trait.Into.html
33-
// /// [`ExtensionValue`]: enum.ExtensionValue.html
34-
// pub fn from_string<S>(s: S) -> Self
35-
// where
36-
// S: Into<String>,
37-
// {
38-
// ExtensionValue::String(s.into())
39-
// }
40-
//
41-
// /// Create an [`ExtensionValue`] from a [`Serialize`] object.
42-
// ///
43-
// /// # Example
44-
// ///
45-
// /// ```
46-
// /// use cloudevents::ExtensionValue;
47-
// /// use serde_json::Value;
48-
// /// use std::error::Error;
49-
// ///
50-
// /// fn main() -> Result<(), Box<Error>> {
51-
// /// let value = ExtensionValue::from_serializable("value")?;
52-
// /// assert_eq!(value, ExtensionValue::Object(Value::String("value".to_owned())));
53-
// /// Ok(())
54-
// /// }
55-
// /// ```
56-
// ///
57-
// /// [`Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
58-
// /// [`ExtensionValue`]: enum.ExtensionValue.html
59-
// pub fn from_serializable<S>(s: S) -> Result<Self, Error>
60-
// where
61-
// S: Serialize,
62-
// {
63-
// Ok(ExtensionValue::Object(serde_json::to_value(s)?))
64-
// }
65-
// }
20+
impl ExtensionValue {
21+
/// Create an [`ExtensionValue`] from a [`Into<String>`].
22+
///
23+
/// # Example
24+
///
25+
/// ```
26+
/// use cloudevents::ExtensionValue;
27+
///
28+
/// let value = ExtensionValue::from_string("value");
29+
/// assert_eq!(value, ExtensionValue::String("value".to_owned()));
30+
/// ```
31+
///
32+
/// [`Into<String>`]: https://doc.rust-lang.org/std/convert/trait.Into.html
33+
/// [`ExtensionValue`]: enum.ExtensionValue.html
34+
pub fn from_string<S>(s: S) -> Self
35+
where
36+
S: Into<String>,
37+
{
38+
ExtensionValue::String(s.into())
39+
}
40+
41+
pub fn from_i64<S>(s: S) -> Self
42+
where
43+
S: Into<i64>,
44+
{
45+
ExtensionValue::Integer(s.into())
46+
}
47+
48+
pub fn from_bool<S>(s: S) -> Self
49+
where
50+
S: Into<bool>,
51+
{
52+
ExtensionValue::Boolean(s.into())
53+
}
54+
55+
pub fn from_json_value<S>(s: S) -> Self
56+
where
57+
S: Into<serde_json::Value>,
58+
{
59+
ExtensionValue::Object(s.into())
60+
}
61+
}

src/event/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod spec_version;
77
pub use attributes::Attributes;
88
pub(crate) use attributes::{AttributesReader, AttributesWriter};
99
pub use data::Data;
10-
pub(crate) use data::{DataDecoder, DataEncoder};
1110
pub use event::Event;
1211
pub use extensions::ExtensionValue;
1312
pub use spec_version::SpecVersion;

src/event/v10/attributes.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ impl AttributesReader for Attributes {
6060
}
6161

6262
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
63-
self.extensions.iter()
63+
self.extensions
64+
.iter()
6465
.map(|(k, v)| (k.as_str(), v))
6566
.collect()
6667
}
@@ -103,10 +104,14 @@ impl AttributesWriter for Attributes {
103104
extension_name: &'event str,
104105
extension_value: impl Into<ExtensionValue>,
105106
) {
106-
self.extensions.insert(extension_name.to_owned(), extension_value.into());
107+
self.extensions
108+
.insert(extension_name.to_owned(), extension_value.into());
107109
}
108110

109-
fn remove_extension<'event>(&'event mut self, extension_name: &'event str) -> Option<ExtensionValue> {
111+
fn remove_extension<'event>(
112+
&'event mut self,
113+
extension_name: &'event str,
114+
) -> Option<ExtensionValue> {
110115
self.extensions.remove(extension_name)
111116
}
112117
}

tests/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use cloudevents_rust_sdk::Event;
2+
3+
#[test]
4+
fn use_event() {}

0 commit comments

Comments
 (0)