Skip to content

Commit e21bcd1

Browse files
committed
[cloudevents#104] Allowing handling URI-ref types
1 parent da40d3d commit e21bcd1

File tree

10 files changed

+47
-51
lines changed

10 files changed

+47
-51
lines changed

src/event/attributes.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10,
3-
ExtensionValue, SpecVersion,
3+
ExtensionValue, SpecVersion, UriReference,
44
};
55
use chrono::{DateTime, Utc};
66
use serde::Serializer;
@@ -14,7 +14,7 @@ pub enum AttributeValue<'a> {
1414
SpecVersion(SpecVersion),
1515
String(&'a str),
1616
URI(&'a Url),
17-
URIRef(&'a Url),
17+
URIRef(&'a UriReference),
1818
Boolean(&'a bool),
1919
Integer(&'a i64),
2020
Time(&'a DateTime<Utc>),
@@ -49,7 +49,7 @@ pub trait AttributesReader {
4949
/// Get the [id](https://github.com/cloudevents/spec/blob/master/spec.md#id).
5050
fn id(&self) -> &str;
5151
/// Get the [source](https://github.com/cloudevents/spec/blob/master/spec.md#source-1).
52-
fn source(&self) -> &Url;
52+
fn source(&self) -> &UriReference;
5353
/// Get the [specversion](https://github.com/cloudevents/spec/blob/master/spec.md#specversion).
5454
fn specversion(&self) -> SpecVersion;
5555
/// Get the [type](https://github.com/cloudevents/spec/blob/master/spec.md#type).
@@ -71,7 +71,7 @@ pub trait AttributesWriter {
7171
fn set_id(&mut self, id: impl Into<String>) -> String;
7272
/// Set the [source](https://github.com/cloudevents/spec/blob/master/spec.md#source-1).
7373
/// Returns the previous value.
74-
fn set_source(&mut self, source: impl Into<Url>) -> Url;
74+
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference;
7575
/// Set the [type](https://github.com/cloudevents/spec/blob/master/spec.md#type).
7676
/// Returns the previous value.
7777
fn set_type(&mut self, ty: impl Into<String>) -> String;
@@ -126,7 +126,7 @@ impl AttributesReader for Attributes {
126126
}
127127
}
128128

129-
fn source(&self) -> &Url {
129+
fn source(&self) -> &UriReference {
130130
match self {
131131
Attributes::V03(a) => a.source(),
132132
Attributes::V10(a) => a.source(),
@@ -184,7 +184,7 @@ impl AttributesWriter for Attributes {
184184
}
185185
}
186186

187-
fn set_source(&mut self, source: impl Into<Url>) -> Url {
187+
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference {
188188
match self {
189189
Attributes::V03(a) => a.set_source(source),
190190
Attributes::V10(a) => a.set_source(source),

src/event/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) use message::EventBinarySerializer;
2020
pub(crate) use message::EventStructuredSerializer;
2121
pub use spec_version::SpecVersion;
2222
pub use spec_version::UnknownSpecVersion;
23-
pub use types::{TryIntoTime, TryIntoUrl};
23+
pub use types::{TryIntoTime, TryIntoUrl, UriReference};
2424

2525
mod v03;
2626

@@ -85,7 +85,7 @@ pub struct Event {
8585
#[delegate(self.attributes)]
8686
impl AttributesReader for Event {
8787
fn id(&self) -> &str;
88-
fn source(&self) -> &Url;
88+
fn source(&self) -> &UriReference;
8989
fn specversion(&self) -> SpecVersion;
9090
fn ty(&self) -> &str;
9191
fn datacontenttype(&self) -> Option<&str>;
@@ -97,7 +97,7 @@ impl AttributesReader for Event {
9797
#[delegate(self.attributes)]
9898
impl AttributesWriter for Event {
9999
fn set_id(&mut self, id: impl Into<String>) -> String;
100-
fn set_source(&mut self, source: impl Into<Url>) -> Url;
100+
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference;
101101
fn set_type(&mut self, ty: impl Into<String>) -> String;
102102
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>;
103103
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;

src/event/types.rs

+12
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ impl TryIntoTime for String {
4646
self.as_str().into_time()
4747
}
4848
}
49+
50+
/// The URI-reference type.
51+
///
52+
/// The URI reference can be a URI, or just a relative path.
53+
///
54+
/// As the [`url::Url`] type can only represent an absolute URL, we are falling back to a string
55+
/// here.
56+
///
57+
/// Also see:
58+
/// * https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#type-system
59+
/// * https://tools.ietf.org/html/rfc3986#section-4.1
60+
pub type UriReference = String;

src/event/v03/attributes.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::event::attributes::{default_hostname, AttributeValue, AttributesConverter};
2-
use crate::event::AttributesV10;
3-
use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
2+
use crate::event::{AttributesReader, AttributesV10, AttributesWriter, SpecVersion, UriReference};
43
use crate::message::{BinarySerializer, MessageAttributeValue};
54
use chrono::{DateTime, Utc};
65
use url::Url;
@@ -22,7 +21,7 @@ pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
2221
pub struct Attributes {
2322
pub(crate) id: String,
2423
pub(crate) ty: String,
25-
pub(crate) source: Url,
24+
pub(crate) source: UriReference,
2625
pub(crate) datacontenttype: Option<String>,
2726
pub(crate) schemaurl: Option<Url>,
2827
pub(crate) subject: Option<String>,
@@ -64,7 +63,7 @@ impl<'a> Iterator for AttributesIntoIterator<'a> {
6463
.attributes
6564
.schemaurl
6665
.as_ref()
67-
.map(|v| ("schemaurl", AttributeValue::URIRef(v))),
66+
.map(|v| ("schemaurl", AttributeValue::URI(v))),
6867
6 => self
6968
.attributes
7069
.subject
@@ -90,7 +89,7 @@ impl AttributesReader for Attributes {
9089
&self.id
9190
}
9291

93-
fn source(&self) -> &Url {
92+
fn source(&self) -> &UriReference {
9493
&self.source
9594
}
9695

@@ -124,7 +123,7 @@ impl AttributesWriter for Attributes {
124123
std::mem::replace(&mut self.id, id.into())
125124
}
126125

127-
fn set_source(&mut self, source: impl Into<Url>) -> Url {
126+
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference {
128127
std::mem::replace(&mut self.source, source.into())
129128
}
130129

@@ -157,7 +156,7 @@ impl Default for Attributes {
157156
Attributes {
158157
id: Uuid::new_v4().to_string(),
159158
ty: "type".to_string(),
160-
source: default_hostname(),
159+
source: default_hostname().to_string(),
161160
datacontenttype: None,
162161
schemaurl: None,
163162
subject: None,
@@ -228,7 +227,7 @@ mod tests {
228227
let a = Attributes {
229228
id: String::from("1"),
230229
ty: String::from("someType"),
231-
source: Url::parse("https://example.net").unwrap(),
230+
source: "https://example.net".into(),
232231
datacontenttype: None,
233232
schemaurl: None,
234233
subject: None,

src/event/v03/builder.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Attributes as AttributesV03;
22
use crate::event::{
33
Attributes, Data, Event, EventBuilderError, ExtensionValue, TryIntoTime, TryIntoUrl,
4+
UriReference,
45
};
56
use crate::message::MessageAttributeValue;
67
use chrono::{DateTime, Utc};
@@ -13,7 +14,7 @@ use url::Url;
1314
pub struct EventBuilder {
1415
id: Option<String>,
1516
ty: Option<String>,
16-
source: Option<Url>,
17+
source: Option<UriReference>,
1718
datacontenttype: Option<String>,
1819
schemaurl: Option<Url>,
1920
subject: Option<String>,
@@ -29,16 +30,8 @@ impl EventBuilder {
2930
self
3031
}
3132

32-
pub fn source(mut self, source: impl TryIntoUrl) -> Self {
33-
match source.into_url() {
34-
Ok(u) => self.source = Some(u),
35-
Err(e) => {
36-
self.error = Some(EventBuilderError::ParseUrlError {
37-
attribute_name: "source",
38-
source: e,
39-
})
40-
}
41-
};
33+
pub fn source(mut self, source: impl Into<String>) -> Self {
34+
self.source = Some(source.into());
4235
self
4336
}
4437

@@ -190,7 +183,7 @@ impl crate::event::message::AttributesSerializer for EventBuilder {
190183
match name {
191184
"id" => self.id = Some(value.to_string()),
192185
"type" => self.ty = Some(value.to_string()),
193-
"source" => self.source = Some(value.try_into()?),
186+
"source" => self.source = Some(value.to_string()),
194187
"datacontenttype" => self.datacontenttype = Some(value.to_string()),
195188
"schemaurl" => self.schemaurl = Some(value.try_into()?),
196189
"subject" => self.subject = Some(value.to_string()),

src/event/v03/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl crate::event::format::EventFormatDeserializer for EventFormatDeserializer {
1818
Ok(crate::event::Attributes::V03(Attributes {
1919
id: extract_field!(map, "id", String, E)?,
2020
ty: extract_field!(map, "type", String, E)?,
21-
source: extract_field!(map, "source", String, E, |s: String| Url::parse(&s))?,
21+
source: extract_field!(map, "source", String, E)?,
2222
datacontenttype: extract_optional_field!(map, "datacontenttype", String, E)?,
2323
schemaurl: extract_optional_field!(map, "schemaurl", String, E, |s: String| {
2424
Url::parse(&s)

src/event/v10/attributes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::event::attributes::{default_hostname, AttributeValue, AttributesConverter};
2-
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
2+
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion, UriReference};
33
use crate::message::{BinarySerializer, MessageAttributeValue};
44
use chrono::{DateTime, Utc};
55
use core::fmt::Debug;
@@ -22,7 +22,7 @@ pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
2222
pub struct Attributes {
2323
pub(crate) id: String,
2424
pub(crate) ty: String,
25-
pub(crate) source: Url,
25+
pub(crate) source: UriReference,
2626
pub(crate) datacontenttype: Option<String>,
2727
pub(crate) dataschema: Option<Url>,
2828
pub(crate) subject: Option<String>,
@@ -90,7 +90,7 @@ impl AttributesReader for Attributes {
9090
&self.id
9191
}
9292

93-
fn source(&self) -> &Url {
93+
fn source(&self) -> &UriReference {
9494
&self.source
9595
}
9696

@@ -124,7 +124,7 @@ impl AttributesWriter for Attributes {
124124
std::mem::replace(&mut self.id, id.into())
125125
}
126126

127-
fn set_source(&mut self, source: impl Into<Url>) -> Url {
127+
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference {
128128
std::mem::replace(&mut self.source, source.into())
129129
}
130130

@@ -157,7 +157,7 @@ impl Default for Attributes {
157157
Attributes {
158158
id: Uuid::new_v4().to_string(),
159159
ty: "type".to_string(),
160-
source: default_hostname(),
160+
source: default_hostname().to_string(),
161161
datacontenttype: None,
162162
dataschema: None,
163163
subject: None,

src/event/v10/builder.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Attributes as AttributesV10;
22
use crate::event::{
33
Attributes, Data, Event, EventBuilderError, ExtensionValue, TryIntoTime, TryIntoUrl,
4+
UriReference,
45
};
56
use crate::message::MessageAttributeValue;
67
use chrono::{DateTime, Utc};
@@ -13,7 +14,7 @@ use url::Url;
1314
pub struct EventBuilder {
1415
id: Option<String>,
1516
ty: Option<String>,
16-
source: Option<Url>,
17+
source: Option<UriReference>,
1718
datacontenttype: Option<String>,
1819
dataschema: Option<Url>,
1920
subject: Option<String>,
@@ -29,16 +30,8 @@ impl EventBuilder {
2930
self
3031
}
3132

32-
pub fn source(mut self, source: impl TryIntoUrl) -> Self {
33-
match source.into_url() {
34-
Ok(u) => self.source = Some(u),
35-
Err(e) => {
36-
self.error = Some(EventBuilderError::ParseUrlError {
37-
attribute_name: "source",
38-
source: e,
39-
})
40-
}
41-
};
33+
pub fn source(mut self, source: impl Into<String>) -> Self {
34+
self.source = Some(source.into());
4235
self
4336
}
4437

@@ -190,7 +183,7 @@ impl crate::event::message::AttributesSerializer for EventBuilder {
190183
match name {
191184
"id" => self.id = Some(value.to_string()),
192185
"type" => self.ty = Some(value.to_string()),
193-
"source" => self.source = Some(value.try_into()?),
186+
"source" => self.source = Some(value.to_string()),
194187
"datacontenttype" => self.datacontenttype = Some(value.to_string()),
195188
"dataschema" => self.dataschema = Some(value.try_into()?),
196189
"subject" => self.subject = Some(value.to_string()),

src/event/v10/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl crate::event::format::EventFormatDeserializer for EventFormatDeserializer {
1818
Ok(crate::event::Attributes::V10(Attributes {
1919
id: extract_field!(map, "id", String, E)?,
2020
ty: extract_field!(map, "type", String, E)?,
21-
source: extract_field!(map, "source", String, E, |s: String| Url::parse(&s))?,
21+
source: extract_field!(map, "source", String, E)?,
2222
datacontenttype: extract_optional_field!(map, "datacontenttype", String, E)?,
2323
dataschema: extract_optional_field!(map, "dataschema", String, E, |s: String| {
2424
Url::parse(&s)

src/message/types.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::event::ExtensionValue;
1+
use crate::event::{ExtensionValue, UriReference};
22
use chrono::{DateTime, Utc};
33
use std::convert::TryInto;
44
use std::fmt;
@@ -12,7 +12,7 @@ pub enum MessageAttributeValue {
1212
String(String),
1313
Binary(Vec<u8>),
1414
Uri(Url),
15-
UriRef(Url),
15+
UriRef(UriReference),
1616
DateTime(DateTime<Utc>),
1717
}
1818

@@ -35,7 +35,6 @@ impl TryInto<Url> for MessageAttributeValue {
3535
fn try_into(self) -> Result<Url, Self::Error> {
3636
match self {
3737
MessageAttributeValue::Uri(u) => Ok(u),
38-
MessageAttributeValue::UriRef(u) => Ok(u),
3938
v => Ok(Url::parse(v.to_string().as_ref())?),
4039
}
4140
}

0 commit comments

Comments
 (0)