Skip to content

Commit d1281e7

Browse files
Align with C-COMMON-TRAITS criteria (cloudevents#91)
* Fix C-COMMON-TRAITS Signed-off-by: Francesco Guardiani <[email protected]> * Fix C-COMMON-TRAITS Signed-off-by: Francesco Guardiani <[email protected]>
1 parent c4305e0 commit d1281e7

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

src/event/attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88
use url::Url;
99

1010
/// Value of a CloudEvent attribute
11-
#[derive(Debug, PartialEq)]
11+
#[derive(Debug, PartialEq, Eq)]
1212
pub enum AttributeValue<'a> {
1313
SpecVersion(SpecVersion),
1414
String(&'a str),
@@ -111,7 +111,7 @@ impl<'a> Iterator for AttributesIter<'a> {
111111
}
112112

113113
/// Union type representing one of the possible context attributes structs
114-
#[derive(PartialEq, Debug, Clone)]
114+
#[derive(PartialEq, Eq, Debug, Clone)]
115115
pub enum Attributes {
116116
V03(AttributesV03),
117117
V10(AttributesV10),

src/event/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::convert::{Into, TryFrom};
44
use std::fmt;
55

66
/// Event [data attribute](https://github.com/cloudevents/spec/blob/master/spec.md#event-data) representation
7-
#[derive(Debug, PartialEq, Clone)]
7+
#[derive(PartialEq, Eq, Debug, Clone)]
88
pub enum Data {
99
/// Event has a binary payload
1010
Binary(Vec<u8>),

src/event/event.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::{
55
use chrono::{DateTime, Utc};
66
use delegate_attr::delegate;
77
use std::collections::HashMap;
8+
use std::fmt;
89
use url::Url;
910

1011
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
@@ -38,7 +39,7 @@ use url::Url;
3839
/// # Ok(())
3940
/// # }
4041
/// ```
41-
#[derive(PartialEq, Debug, Clone)]
42+
#[derive(PartialEq, Eq, Debug, Clone)]
4243
pub struct Event {
4344
pub(crate) attributes: Attributes,
4445
pub(crate) data: Option<Data>,
@@ -79,6 +80,20 @@ impl Default for Event {
7980
}
8081
}
8182

83+
impl fmt::Display for Event {
84+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85+
write!(f, "CloudEvent:\n")?;
86+
self.iter()
87+
.map(|(name, val)| write!(f, " {}: '{}'\n", name, val))
88+
.collect::<fmt::Result>()?;
89+
match self.data() {
90+
Some(data) => write!(f, " {}", data)?,
91+
None => write!(f, " No data")?,
92+
}
93+
write!(f, "\n")
94+
}
95+
}
96+
8297
impl Event {
8398
/// Returns an [`Iterator`] for all the available [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes) and extensions.
8499
/// Same as chaining [`Event::iter_attributes()`] and [`Event::iter_extensions()`]

src/event/extensions.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use serde::{Deserialize, Serialize};
1+
use serde::{Deserialize, Serialize, Serializer};
22
use std::convert::From;
3+
use std::fmt;
34

4-
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
5+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
56
#[serde(untagged)]
67
/// Represents all the possible [CloudEvents extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) values
78
pub enum ExtensionValue {
@@ -59,3 +60,13 @@ impl ExtensionValue {
5960
ExtensionValue::from(s.into())
6061
}
6162
}
63+
64+
impl fmt::Display for ExtensionValue {
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
match self {
67+
ExtensionValue::String(s) => f.write_str(s),
68+
ExtensionValue::Boolean(b) => f.serialize_bool(*b),
69+
ExtensionValue::Integer(i) => f.serialize_i64(*i),
70+
}
71+
}
72+
}

src/event/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod spec_version;
1010
mod types;
1111

1212
pub use attributes::Attributes;
13-
pub(crate) use attributes::AttributesIter;
1413
pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
1514
pub use builder::Error as EventBuilderError;
1615
pub use builder::EventBuilder;

src/event/v03/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) const ATTRIBUTE_NAMES: [&'static str; 8] = [
1818
];
1919

2020
/// Data structure representing [CloudEvents V0.3 context attributes](https://github.com/cloudevents/spec/blob/v0.3/spec.md#context-attributes)
21-
#[derive(PartialEq, Debug, Clone)]
21+
#[derive(PartialEq, Eq, Debug, Clone)]
2222
pub struct Attributes {
2323
pub(crate) id: String,
2424
pub(crate) ty: String,

src/event/v10/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) const ATTRIBUTE_NAMES: [&'static str; 8] = [
1818
];
1919

2020
/// Data structure representing [CloudEvents V1.0 context attributes](https://github.com/cloudevents/spec/blob/v1.0/spec.md#context-attributes)
21-
#[derive(PartialEq, Debug, Clone)]
21+
#[derive(PartialEq, Eq, Debug, Clone)]
2222
pub struct Attributes {
2323
pub(crate) id: String,
2424
pub(crate) ty: String,

src/message/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22

33
/// Represents one of the possible [message encodings/modes](https://github.com/cloudevents/spec/blob/v1.0/spec.md#message)
4-
#[derive(PartialEq, Debug)]
4+
#[derive(PartialEq, Eq, Debug)]
55
pub enum Encoding {
66
STRUCTURED,
77
BINARY,

src/message/types.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::event::ExtensionValue;
22
use chrono::{DateTime, Utc};
3+
use serde::export::Formatter;
34
use std::convert::TryInto;
5+
use std::fmt;
46
use url::Url;
57

68
/// Union type representing a [CloudEvent context attribute type](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system)
9+
#[derive(PartialEq, Eq, Debug, Clone)]
710
pub enum MessageAttributeValue {
811
Boolean(bool),
912
Integer(i64),
@@ -39,16 +42,16 @@ impl TryInto<Url> for MessageAttributeValue {
3942
}
4043
}
4144

42-
impl ToString for MessageAttributeValue {
43-
fn to_string(&self) -> String {
45+
impl fmt::Display for MessageAttributeValue {
46+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4447
match self {
45-
MessageAttributeValue::Boolean(b) => b.to_string(),
46-
MessageAttributeValue::Integer(i) => i.to_string(),
47-
MessageAttributeValue::String(s) => s.clone(),
48-
MessageAttributeValue::Binary(v) => base64::encode(v),
49-
MessageAttributeValue::Uri(u) => u.to_string(),
50-
MessageAttributeValue::UriRef(u) => u.to_string(),
51-
MessageAttributeValue::DateTime(d) => d.to_rfc3339(),
48+
MessageAttributeValue::Boolean(b) => write!(f, "{}", b),
49+
MessageAttributeValue::Integer(i) => write!(f, "{}", i),
50+
MessageAttributeValue::String(s) => write!(f, "{}", s),
51+
MessageAttributeValue::Binary(v) => write!(f, "{}", base64::encode(v)),
52+
MessageAttributeValue::Uri(u) => write!(f, "{}", u.to_string()),
53+
MessageAttributeValue::UriRef(u) => write!(f, "{}", u.to_string()),
54+
MessageAttributeValue::DateTime(d) => write!(f, "{}", d.to_rfc3339()),
5255
}
5356
}
5457
}

0 commit comments

Comments
 (0)