Skip to content

Commit 9b2f262

Browse files
Introduced URL support (cloudevents#27)
* Introduced url library Signed-off-by: Francesco Guardiani <[email protected]> * Fixed lock Signed-off-by: Francesco Guardiani <[email protected]> * Integrated changes with url library Signed-off-by: Francesco Guardiani <[email protected]> * Cargo fmt Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 4c25539 commit 9b2f262

File tree

19 files changed

+231
-118
lines changed

19 files changed

+231
-118
lines changed

Cargo.lock

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ delegate = "^0.4"
1919
uuid = { version = "^0.8", features = ["serde", "v4"] }
2020
hostname = "^0.1"
2121
base64 = "^0.12"
22+
url = { version = "^2.1", features = ["serde"] }
2223
snafu = "^0.6"
2324

2425
[dev-dependencies]

src/event/attributes.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use super::{AttributesV03, AttributesV10, SpecVersion};
22
use chrono::{DateTime, Utc};
3+
use url::Url;
34

45
/// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
56
pub trait AttributesReader {
67
/// Get the [id](https://github.com/cloudevents/spec/blob/master/spec.md#id).
78
fn get_id(&self) -> &str;
89
/// Get the [source](https://github.com/cloudevents/spec/blob/master/spec.md#source-1).
9-
fn get_source(&self) -> &str;
10+
fn get_source(&self) -> &Url;
1011
/// Get the [specversion](https://github.com/cloudevents/spec/blob/master/spec.md#specversion).
1112
fn get_specversion(&self) -> SpecVersion;
1213
/// Get the [type](https://github.com/cloudevents/spec/blob/master/spec.md#type).
1314
fn get_type(&self) -> &str;
1415
/// Get the [datacontenttype](https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype).
1516
fn get_datacontenttype(&self) -> Option<&str>;
1617
/// Get the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
17-
fn get_dataschema(&self) -> Option<&str>;
18+
fn get_dataschema(&self) -> Option<&Url>;
1819
/// Get the [subject](https://github.com/cloudevents/spec/blob/master/spec.md#subject).
1920
fn get_subject(&self) -> Option<&str>;
2021
/// Get the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
@@ -23,7 +24,7 @@ pub trait AttributesReader {
2324

2425
pub trait AttributesWriter {
2526
fn set_id(&mut self, id: impl Into<String>);
26-
fn set_source(&mut self, source: impl Into<String>);
27+
fn set_source(&mut self, source: impl Into<Url>);
2728
fn set_type(&mut self, ty: impl Into<String>);
2829
fn set_subject(&mut self, subject: Option<impl Into<String>>);
2930
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>);
@@ -36,7 +37,7 @@ pub(crate) trait AttributesConverter {
3637

3738
pub(crate) trait DataAttributesWriter {
3839
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>);
39-
fn set_dataschema(&mut self, dataschema: Option<impl Into<String>>);
40+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>);
4041
}
4142

4243
#[derive(PartialEq, Debug, Clone)]
@@ -53,7 +54,7 @@ impl AttributesReader for Attributes {
5354
}
5455
}
5556

56-
fn get_source(&self) -> &str {
57+
fn get_source(&self) -> &Url {
5758
match self {
5859
Attributes::V03(a) => a.get_source(),
5960
Attributes::V10(a) => a.get_source(),
@@ -81,7 +82,7 @@ impl AttributesReader for Attributes {
8182
}
8283
}
8384

84-
fn get_dataschema(&self) -> Option<&str> {
85+
fn get_dataschema(&self) -> Option<&Url> {
8586
match self {
8687
Attributes::V03(a) => a.get_dataschema(),
8788
Attributes::V10(a) => a.get_dataschema(),
@@ -111,7 +112,7 @@ impl AttributesWriter for Attributes {
111112
}
112113
}
113114

114-
fn set_source(&mut self, source: impl Into<String>) {
115+
fn set_source(&mut self, source: impl Into<Url>) {
115116
match self {
116117
Attributes::V03(a) => a.set_source(source),
117118
Attributes::V10(a) => a.set_source(source),
@@ -148,7 +149,7 @@ impl DataAttributesWriter for Attributes {
148149
}
149150
}
150151

151-
fn set_dataschema(&mut self, dataschema: Option<impl Into<String>>) {
152+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) {
152153
match self {
153154
Attributes::V03(a) => a.set_dataschema(dataschema),
154155
Attributes::V10(a) => a.set_dataschema(dataschema),

src/event/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use super::{EventBuilderV03, EventBuilderV10};
44
/// ```
55
/// use cloudevents::EventBuilder;
66
/// use chrono::Utc;
7+
/// use url::Url;
78
///
89
/// let event = EventBuilder::v10()
910
/// .id("my_event.my_application")
10-
/// .source("http://localhost:8080")
11+
/// .source(Url::parse("http://localhost:8080").unwrap())
1112
/// .time(Utc::now())
1213
/// .build();
1314
/// ```

src/event/event.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use chrono::{DateTime, Utc};
77
use delegate::delegate;
88
use std::collections::HashMap;
99
use std::convert::TryFrom;
10+
use url::Url;
1011

1112
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
1213
/// It provides methods to get the attributes through [`AttributesReader`]
@@ -43,11 +44,11 @@ impl AttributesReader for Event {
4344
delegate! {
4445
to self.attributes {
4546
fn get_id(&self) -> &str;
46-
fn get_source(&self) -> &str;
47+
fn get_source(&self) -> &Url;
4748
fn get_specversion(&self) -> SpecVersion;
4849
fn get_type(&self) -> &str;
4950
fn get_datacontenttype(&self) -> Option<&str>;
50-
fn get_dataschema(&self) -> Option<&str>;
51+
fn get_dataschema(&self) -> Option<&Url>;
5152
fn get_subject(&self) -> Option<&str>;
5253
fn get_time(&self) -> Option<&DateTime<Utc>>;
5354
}
@@ -58,7 +59,7 @@ impl AttributesWriter for Event {
5859
delegate! {
5960
to self.attributes {
6061
fn set_id(&mut self, id: impl Into<String>);
61-
fn set_source(&mut self, source: impl Into<String>);
62+
fn set_source(&mut self, source: impl Into<Url>);
6263
fn set_type(&mut self, ty: impl Into<String>);
6364
fn set_subject(&mut self, subject: Option<impl Into<String>>);
6465
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>);
@@ -79,7 +80,7 @@ impl Default for Event {
7980
impl Event {
8081
pub fn remove_data(&mut self) {
8182
self.data = None;
82-
self.attributes.set_dataschema(None as Option<String>);
83+
self.attributes.set_dataschema(None as Option<Url>);
8384
self.attributes.set_datacontenttype(None as Option<String>);
8485
}
8586

@@ -95,7 +96,7 @@ impl Event {
9596
/// ```
9697
pub fn write_data(&mut self, datacontenttype: impl Into<String>, data: impl Into<Data>) {
9798
self.attributes.set_datacontenttype(Some(datacontenttype));
98-
self.attributes.set_dataschema(None as Option<&str>);
99+
self.attributes.set_dataschema(None as Option<Url>);
99100
self.data = Some(data.into());
100101
}
101102

@@ -105,14 +106,19 @@ impl Event {
105106
/// use cloudevents::Event;
106107
/// use serde_json::json;
107108
/// use std::convert::Into;
109+
/// use url::Url;
108110
///
109111
/// let mut e = Event::default();
110-
/// e.write_data_with_schema("application/json", "http://myapplication.com/schema", json!({}))
112+
/// e.write_data_with_schema(
113+
/// "application/json",
114+
/// Url::parse("http://myapplication.com/schema").unwrap(),
115+
/// json!({})
116+
/// )
111117
/// ```
112118
pub fn write_data_with_schema(
113119
&mut self,
114120
datacontenttype: impl Into<String>,
115-
dataschema: impl Into<String>,
121+
dataschema: impl Into<Url>,
116122
data: impl Into<Data>,
117123
) {
118124
self.attributes.set_datacontenttype(Some(datacontenttype));
@@ -186,14 +192,17 @@ mod tests {
186192
let mut e = Event::default();
187193
e.write_data_with_schema(
188194
"application/json",
189-
"http://localhost:8080/schema",
195+
Url::parse("http://localhost:8080/schema").unwrap(),
190196
expected_data.clone(),
191197
);
192198

193199
let data: serde_json::Value = e.try_get_data().unwrap().unwrap();
194200
assert_eq!(expected_data, data);
195201
assert_eq!("application/json", e.get_datacontenttype().unwrap());
196-
assert_eq!("http://localhost:8080/schema", e.get_dataschema().unwrap())
202+
assert_eq!(
203+
&Url::parse("http://localhost:8080/schema").unwrap(),
204+
e.get_dataschema().unwrap()
205+
)
197206
}
198207

199208
#[test]

src/event/serde.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@ macro_rules! parse_optional_field {
2020
})
2121
.transpose()
2222
};
23+
24+
($map:ident, $name:literal, $value_variant:ident, $error:ty, $mapper:expr) => {
25+
$map.remove($name)
26+
.map(|val| match val {
27+
Value::$value_variant(v) => $mapper(&v).map_err(|e| {
28+
<$error>::invalid_value(
29+
crate::event::serde::value_to_unexpected(&Value::$value_variant(v)),
30+
&e.to_string().as_str(),
31+
)
32+
}),
33+
other => Err(<$error>::invalid_type(
34+
crate::event::serde::value_to_unexpected(&other),
35+
&stringify!($value_variant),
36+
)),
37+
})
38+
.transpose()
39+
};
2340
}
2441

2542
macro_rules! parse_field {
2643
($map:ident, $name:literal, $value_variant:ident, $error:ty) => {
2744
parse_optional_field!($map, $name, $value_variant, $error)?
2845
.ok_or_else(|| <$error>::missing_field($name))
2946
};
47+
48+
($map:ident, $name:literal, $value_variant:ident, $error:ty, $mapper:expr) => {
49+
parse_optional_field!($map, $name, $value_variant, $error, $mapper)?
50+
.ok_or_else(|| <$error>::missing_field($name))
51+
};
3052
}
3153

3254
macro_rules! parse_data_json {

0 commit comments

Comments
 (0)