Skip to content

Commit c926188

Browse files
Cleanup to follow C-GETTER (cloudevents#88)
* Cleanup to follow C-GETTER Signed-off-by: Francesco Guardiani <[email protected]> * Cleanup to follow C-GETTER Implemented Debug in event::Data Exposing event::Data in main cloudevents export Fixed rebase errors with previous pr Signed-off-by: Francesco Guardiani <[email protected]> * cargo fmt Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 1858a1c commit c926188

File tree

11 files changed

+134
-148
lines changed

11 files changed

+134
-148
lines changed

src/event/attributes.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ impl fmt::Display for AttributeValue<'_> {
4646
/// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
4747
pub trait AttributesReader {
4848
/// Get the [id](https://github.com/cloudevents/spec/blob/master/spec.md#id).
49-
fn get_id(&self) -> &str;
49+
fn id(&self) -> &str;
5050
/// Get the [source](https://github.com/cloudevents/spec/blob/master/spec.md#source-1).
51-
fn get_source(&self) -> &Url;
51+
fn source(&self) -> &Url;
5252
/// Get the [specversion](https://github.com/cloudevents/spec/blob/master/spec.md#specversion).
53-
fn get_specversion(&self) -> SpecVersion;
53+
fn specversion(&self) -> SpecVersion;
5454
/// Get the [type](https://github.com/cloudevents/spec/blob/master/spec.md#type).
55-
fn get_type(&self) -> &str;
55+
fn ty(&self) -> &str;
5656
/// Get the [datacontenttype](https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype).
57-
fn get_datacontenttype(&self) -> Option<&str>;
57+
fn datacontenttype(&self) -> Option<&str>;
5858
/// Get the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
59-
fn get_dataschema(&self) -> Option<&Url>;
59+
fn dataschema(&self) -> Option<&Url>;
6060
/// Get the [subject](https://github.com/cloudevents/spec/blob/master/spec.md#subject).
61-
fn get_subject(&self) -> Option<&str>;
61+
fn subject(&self) -> Option<&str>;
6262
/// Get the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
63-
fn get_time(&self) -> Option<&DateTime<Utc>>;
63+
fn time(&self) -> Option<&DateTime<Utc>>;
6464
}
6565

6666
/// Trait to set [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
@@ -117,59 +117,59 @@ pub enum Attributes {
117117
}
118118

119119
impl AttributesReader for Attributes {
120-
fn get_id(&self) -> &str {
120+
fn id(&self) -> &str {
121121
match self {
122-
Attributes::V03(a) => a.get_id(),
123-
Attributes::V10(a) => a.get_id(),
122+
Attributes::V03(a) => a.id(),
123+
Attributes::V10(a) => a.id(),
124124
}
125125
}
126126

127-
fn get_source(&self) -> &Url {
127+
fn source(&self) -> &Url {
128128
match self {
129-
Attributes::V03(a) => a.get_source(),
130-
Attributes::V10(a) => a.get_source(),
129+
Attributes::V03(a) => a.source(),
130+
Attributes::V10(a) => a.source(),
131131
}
132132
}
133133

134-
fn get_specversion(&self) -> SpecVersion {
134+
fn specversion(&self) -> SpecVersion {
135135
match self {
136-
Attributes::V03(a) => a.get_specversion(),
137-
Attributes::V10(a) => a.get_specversion(),
136+
Attributes::V03(a) => a.specversion(),
137+
Attributes::V10(a) => a.specversion(),
138138
}
139139
}
140140

141-
fn get_type(&self) -> &str {
141+
fn ty(&self) -> &str {
142142
match self {
143-
Attributes::V03(a) => a.get_type(),
144-
Attributes::V10(a) => a.get_type(),
143+
Attributes::V03(a) => a.ty(),
144+
Attributes::V10(a) => a.ty(),
145145
}
146146
}
147147

148-
fn get_datacontenttype(&self) -> Option<&str> {
148+
fn datacontenttype(&self) -> Option<&str> {
149149
match self {
150-
Attributes::V03(a) => a.get_datacontenttype(),
151-
Attributes::V10(a) => a.get_datacontenttype(),
150+
Attributes::V03(a) => a.datacontenttype(),
151+
Attributes::V10(a) => a.datacontenttype(),
152152
}
153153
}
154154

155-
fn get_dataschema(&self) -> Option<&Url> {
155+
fn dataschema(&self) -> Option<&Url> {
156156
match self {
157-
Attributes::V03(a) => a.get_dataschema(),
158-
Attributes::V10(a) => a.get_dataschema(),
157+
Attributes::V03(a) => a.dataschema(),
158+
Attributes::V10(a) => a.dataschema(),
159159
}
160160
}
161161

162-
fn get_subject(&self) -> Option<&str> {
162+
fn subject(&self) -> Option<&str> {
163163
match self {
164-
Attributes::V03(a) => a.get_subject(),
165-
Attributes::V10(a) => a.get_subject(),
164+
Attributes::V03(a) => a.subject(),
165+
Attributes::V10(a) => a.subject(),
166166
}
167167
}
168168

169-
fn get_time(&self) -> Option<&DateTime<Utc>> {
169+
fn time(&self) -> Option<&DateTime<Utc>> {
170170
match self {
171-
Attributes::V03(a) => a.get_time(),
172-
Attributes::V10(a) => a.get_time(),
171+
Attributes::V03(a) => a.time(),
172+
Attributes::V10(a) => a.time(),
173173
}
174174
}
175175
}

src/event/data.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use serde::export::Formatter;
12
use std::convert::{Into, TryFrom};
3+
use std::fmt;
24

35
/// Event [data attribute](https://github.com/cloudevents/spec/blob/master/spec.md#event-data) representation
46
#[derive(Debug, PartialEq, Clone)]
@@ -102,3 +104,13 @@ impl TryFrom<Data> for String {
102104
}
103105
}
104106
}
107+
108+
impl fmt::Display for Data {
109+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
110+
match self {
111+
Data::Binary(vec) => write!(f, "Binary data: {:?}", vec),
112+
Data::String(s) => write!(f, "String data: {}", s),
113+
Data::Json(j) => write!(f, "Json data: {}", j),
114+
}
115+
}
116+
}

src/event/event.rs

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::event::attributes::DataAttributesWriter;
66
use chrono::{DateTime, Utc};
77
use delegate_attr::delegate;
88
use std::collections::HashMap;
9-
use std::convert::TryFrom;
109
use url::Url;
1110

1211
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
@@ -16,9 +15,11 @@ use url::Url;
1615
///
1716
/// You can build events using [`super::EventBuilder`]
1817
/// ```
19-
/// use cloudevents::Event;
20-
/// use cloudevents::event::AttributesReader;
18+
/// use cloudevents::*;
19+
/// use std::convert::TryInto;
2120
///
21+
/// # use std::error::Error;
22+
/// # fn main() -> Result<(), Box<dyn Error>> {
2223
/// // Create an event using the Default trait
2324
/// let mut e = Event::default();
2425
/// e.write_data(
@@ -27,11 +28,16 @@ use url::Url;
2728
/// );
2829
///
2930
/// // Print the event id
30-
/// println!("Event id: {}", e.get_id());
31+
/// println!("Event id: {}", e.id());
3132
///
3233
/// // Get the event data
33-
/// let data: serde_json::Value = e.try_get_data().unwrap().unwrap();
34-
/// println!("Event data: {}", data)
34+
/// let data: Option<Data> = e.data().cloned();
35+
/// match data {
36+
/// Some(d) => println!("{}", d),
37+
/// None => println!("No event data")
38+
/// }
39+
/// # Ok(())
40+
/// # }
3541
/// ```
3642
#[derive(PartialEq, Debug, Clone)]
3743
pub struct Event {
@@ -42,14 +48,14 @@ pub struct Event {
4248

4349
#[delegate(self.attributes)]
4450
impl AttributesReader for Event {
45-
fn get_id(&self) -> &str;
46-
fn get_source(&self) -> &Url;
47-
fn get_specversion(&self) -> SpecVersion;
48-
fn get_type(&self) -> &str;
49-
fn get_datacontenttype(&self) -> Option<&str>;
50-
fn get_dataschema(&self) -> Option<&Url>;
51-
fn get_subject(&self) -> Option<&str>;
52-
fn get_time(&self) -> Option<&DateTime<Utc>>;
51+
fn id(&self) -> &str;
52+
fn source(&self) -> &Url;
53+
fn specversion(&self) -> SpecVersion;
54+
fn ty(&self) -> &str;
55+
fn datacontenttype(&self) -> Option<&str>;
56+
fn dataschema(&self) -> Option<&Url>;
57+
fn subject(&self) -> Option<&str>;
58+
fn time(&self) -> Option<&DateTime<Utc>>;
5359
}
5460

5561
#[delegate(self.attributes)]
@@ -126,6 +132,11 @@ impl Event {
126132
self.data = Some(data.into());
127133
}
128134

135+
/// Get `data` from this `Event`
136+
pub fn data(&self) -> Option<&Data> {
137+
self.data.as_ref()
138+
}
139+
129140
/// Write `data` into this `Event` with the specified `datacontenttype` and `dataschema`.
130141
///
131142
/// ```
@@ -152,34 +163,8 @@ impl Event {
152163
self.data = Some(data.into());
153164
}
154165

155-
/// Get `data` from this `Event`
156-
pub fn get_data<T: Sized + From<Data>>(&self) -> Option<T> {
157-
match self.data.as_ref() {
158-
Some(d) => Some(T::from(d.clone())),
159-
None => None,
160-
}
161-
}
162-
163-
/// Try to get `data` from this `Event`
164-
pub fn try_get_data<T: Sized + TryFrom<Data>>(&self) -> Result<Option<T>, T::Error> {
165-
match self.data.as_ref() {
166-
Some(d) => Some(T::try_from(d.clone())),
167-
None => None,
168-
}
169-
.transpose()
170-
}
171-
172-
/// Transform this `Event` into the content of `data`
173-
pub fn into_data<T: Sized + TryFrom<Data>>(self) -> Result<Option<T>, T::Error> {
174-
match self.data {
175-
Some(d) => Some(T::try_from(d)),
176-
None => None,
177-
}
178-
.transpose()
179-
}
180-
181166
/// Get the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name`
182-
pub fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue> {
167+
pub fn extension(&self, extension_name: &str) -> Option<&ExtensionValue> {
183168
self.extensions.get(extension_name)
184169
}
185170

@@ -206,28 +191,6 @@ impl Event {
206191
mod tests {
207192
use super::*;
208193

209-
#[test]
210-
fn try_get_data_json() {
211-
let expected_data = serde_json::json!({
212-
"hello": "world"
213-
});
214-
215-
let mut e = Event::default();
216-
e.write_data_with_schema(
217-
"application/json",
218-
Url::parse("http://localhost:8080/schema").unwrap(),
219-
expected_data.clone(),
220-
);
221-
222-
let data: serde_json::Value = e.try_get_data().unwrap().unwrap();
223-
assert_eq!(expected_data, data);
224-
assert_eq!("application/json", e.get_datacontenttype().unwrap());
225-
assert_eq!(
226-
&Url::parse("http://localhost:8080/schema").unwrap(),
227-
e.get_dataschema().unwrap()
228-
)
229-
}
230-
231194
#[test]
232195
fn take_data() {
233196
let mut e = Event::default();
@@ -238,11 +201,15 @@ mod tests {
238201
}),
239202
);
240203

241-
let _d = e.take_data();
204+
let (datacontenttype, dataschema, data) = e.take_data();
205+
206+
assert!(datacontenttype.is_some());
207+
assert!(dataschema.is_none());
208+
assert!(data.is_some());
242209

243-
assert!(e.try_get_data::<serde_json::Value>().unwrap().is_none());
244-
assert!(e.get_dataschema().is_none());
245-
assert!(e.get_datacontenttype().is_none());
210+
assert!(e.data().is_none());
211+
assert!(e.dataschema().is_none());
212+
assert!(e.datacontenttype().is_none());
246213
}
247214

248215
#[test]
@@ -251,7 +218,7 @@ mod tests {
251218
e.set_id("001");
252219

253220
assert_eq!(e.set_id("002"), String::from("001"));
254-
assert_eq!(e.get_id(), "002")
221+
assert_eq!(e.id(), "002")
255222
}
256223

257224
#[test]

src/event/format.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ pub(crate) trait EventFormatDeserializer {
106106
) -> Result<Event, E> {
107107
let attributes = Self::deserialize_attributes(&mut map)?;
108108
let data = Self::deserialize_data(
109-
attributes
110-
.get_datacontenttype()
111-
.unwrap_or("application/json"),
109+
attributes.datacontenttype().unwrap_or("application/json"),
112110
&mut map,
113111
)?;
114112
let extensions = map

src/event/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl StructuredDeserializer for Event {
1717

1818
impl BinaryDeserializer for Event {
1919
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(self, mut visitor: V) -> Result<R> {
20-
visitor = visitor.set_spec_version(self.get_specversion())?;
20+
visitor = visitor.set_spec_version(self.specversion())?;
2121
visitor = self.attributes.deserialize_attributes(visitor)?;
2222
for (k, v) in self.extensions.into_iter() {
2323
visitor = visitor.set_extension(&k, v.into())?;

src/event/v03/attributes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,35 @@ impl<'a> Iterator for AttributesIntoIterator<'a> {
8888
}
8989

9090
impl AttributesReader for Attributes {
91-
fn get_id(&self) -> &str {
91+
fn id(&self) -> &str {
9292
&self.id
9393
}
9494

95-
fn get_source(&self) -> &Url {
95+
fn source(&self) -> &Url {
9696
&self.source
9797
}
9898

99-
fn get_specversion(&self) -> SpecVersion {
99+
fn specversion(&self) -> SpecVersion {
100100
SpecVersion::V03
101101
}
102102

103-
fn get_type(&self) -> &str {
103+
fn ty(&self) -> &str {
104104
&self.ty
105105
}
106106

107-
fn get_datacontenttype(&self) -> Option<&str> {
107+
fn datacontenttype(&self) -> Option<&str> {
108108
self.datacontenttype.as_deref()
109109
}
110110

111-
fn get_dataschema(&self) -> Option<&Url> {
111+
fn dataschema(&self) -> Option<&Url> {
112112
self.schemaurl.as_ref()
113113
}
114114

115-
fn get_subject(&self) -> Option<&str> {
115+
fn subject(&self) -> Option<&str> {
116116
self.subject.as_deref()
117117
}
118118

119-
fn get_time(&self) -> Option<&DateTime<Utc>> {
119+
fn time(&self) -> Option<&DateTime<Utc>> {
120120
self.time.as_ref()
121121
}
122122
}

0 commit comments

Comments
 (0)