Skip to content

Commit 0fd3024

Browse files
committed
fixes#2
Signed-off-by: [email protected] <[email protected]>
1 parent 0b830c9 commit 0fd3024

22 files changed

+212
-19
lines changed

Cargo.toml

+9-10
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@ categories = ["web-programming", "encoding", "data-structures"]
1717
name = "cloudevents"
1818

1919
[features]
20-
default = ["std"]
20+
default = ["no_std"]
21+
std = ["no_std_compat_bypass","snafu/std", "snafu/guide", "serde/std", "serde_json/std", "chrono/std", "base64/std", "url"]
22+
no_std = ["no_std_compat_bypass", "serde_no_std", "chrono_no_std", "base64/alloc"]
23+
24+
no_std_compat_bypass=["no-std-compat/std"]
25+
no_std_compat_layer=["no-std-compat/alloc", "no-std-compat/compat_hash", "no-std-compat/compat_sync"]
2126
chrono_no_std = ["chrono/serde", "chrono/alloc", "chrono/clock"]
22-
no_std = ["no-std-compat", "serde_no_std", "chrono_no_std", "base64/alloc"]
2327
serde_no_std = ["serde/derive", "serde/alloc", "serde_json/alloc"]
24-
std = ["snafu/std", "snafu/guide", "serde/std", "serde_json/std", "chrono/std", "base64/std", "url"]
2528

2629
[dependencies]
2730
base64 = { version = "^0.12", default-features = false }
28-
chrono = { version = "^0.4", default-features = false }
31+
chrono = { version = "^0.4", default-features = false , features = ["serde", "clock"]}
2932
delegate-attr = "^0.2"
3033
serde = { version = "^1.0", default-features=false }
3134
serde_json = { version = "^1.0", default-features = false, features = ["alloc"] }
32-
serde-value = "^0.7"
33-
snafu = { version = "^0.6",default-features = false }
35+
snafu = { version = "^0.6", default-features = false }
3436
url = { version = "^2.1", features = ["serde"], optional=true }
3537

36-
[dependencies.no-std-compat]
37-
version = "^0.4.1"
38-
features = ["alloc", "compat_hash", "compat_sync", "compat_macros"]
39-
optional=true
38+
no-std-compat = { version = "^0.4.1", features = ["alloc", "compat_hash", "compat_sync"], optional=true }
4039

4140
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
4241
hostname = "^0.3"

src/event/attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use serde::Serializer;
77
use std::fmt;
88
use std::prelude::v1::*;
99

10+
#[cfg(not(feature = "std"))]
11+
use std::string::String as Url;
1012
#[cfg(feature = "std")]
1113
use url::Url;
12-
#[cfg(not(feature = "std"))]
13-
use String as Url;
1414

1515
/// Enum representing a borrowed value of a CloudEvent attribute.
1616
/// This represents the types defined in the [CloudEvent spec type system](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system)

src/event/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{DisplayError, Event};
1+
use super::{url, DisplayError, Event};
22
use snafu::Snafu;
33

44
/// Trait to implement a builder for [`Event`]:

src/event/mod.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,39 @@ pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10;
4141
use chrono::{DateTime, Utc};
4242
use delegate_attr::delegate;
4343
use std::collections::HashMap;
44-
//use std::fmt;
4544
use std::prelude::v1::*;
45+
46+
#[cfg(feature = "std")]
4647
use url::Url;
48+
#[cfg(not(feature = "std"))]
49+
use String as Url;
50+
51+
pub trait UrlExtend {
52+
fn parse(&self) -> Result<Url, url::ParseError>;
53+
}
54+
55+
impl UrlExtend for Url {
56+
fn parse(&self) -> Result<Url, url::ParseError> {
57+
Ok(self.to_string())
58+
}
59+
}
60+
61+
pub mod url {
62+
use super::{fmt, String};
63+
64+
#[derive(Debug, Clone)]
65+
pub enum ParseError {
66+
Error(String),
67+
}
68+
69+
impl snafu::Error for ParseError {}
70+
71+
impl fmt::Display for ParseError {
72+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73+
self.fmt(f)
74+
}
75+
}
76+
}
4777

4878
use core::fmt::{self, Debug, Display};
4979
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).

src/event/spec_version.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::convert::TryFrom;
55
use std::fmt;
66
use std::fmt::Formatter;
77
use std::prelude::v1::*;
8-
use std::vec;
98

109
pub(crate) const SPEC_VERSIONS: [&str; 2] = ["0.3", "1.0"];
1110

src/event/types.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use chrono::{DateTime, Utc};
22
use std::prelude::v1::*;
3-
use url::Url;
3+
4+
use super::url;
5+
#[cfg(not(feature = "std"))]
6+
use super::{Url, UrlExtend};
7+
#[cfg(feature = "std")]
8+
use url::{self, ParseError, Url};
49

510
/// Trait to define conversion to [`Url`]
611
pub trait TryIntoUrl {
@@ -13,12 +18,21 @@ impl TryIntoUrl for Url {
1318
}
1419
}
1520

21+
#[cfg(not(feature = "std"))]
22+
impl TryIntoUrl for &str {
23+
fn into_url(self) -> Result<Url, url::ParseError> {
24+
Url::parse(&self.to_string())
25+
}
26+
}
27+
28+
#[cfg(feature = "std")]
1629
impl TryIntoUrl for &str {
1730
fn into_url(self) -> Result<Url, url::ParseError> {
1831
Url::parse(self)
1932
}
2033
}
2134

35+
#[cfg(feature = "std")]
2236
impl TryIntoUrl for String {
2337
fn into_url(self) -> Result<Url, url::ParseError> {
2438
self.as_str().into_url()

src/event/v03/attributes.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
44
use crate::message::{BinarySerializer, MessageAttributeValue};
55
use chrono::{DateTime, Utc};
66
use std::prelude::v1::*;
7-
use url::Url;
87
use uuid::Uuid;
98

9+
#[cfg(not(feature = "std"))]
10+
use super::super::Url;
11+
#[cfg(feature = "std")]
12+
use url::Url;
13+
1014
pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
1115
"specversion",
1216
"id",
@@ -222,8 +226,10 @@ impl crate::event::message::AttributesDeserializer for super::Attributes {
222226
#[cfg(test)]
223227
mod tests {
224228
use super::*;
229+
use crate::event::UrlExtend;
225230
use chrono::NaiveDateTime;
226231

232+
#[cfg(feature = "std")]
227233
#[test]
228234
fn iterator_test_v03() {
229235
let a = Attributes {
@@ -259,4 +265,41 @@ mod tests {
259265
);
260266
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
261267
}
268+
269+
#[cfg(not(feature = "std"))]
270+
#[test]
271+
fn iterator_test_v03() {
272+
let a = Attributes {
273+
id: String::from("1"),
274+
ty: String::from("someType"),
275+
source: Url::parse(&"https://example.net".to_string()).unwrap(),
276+
datacontenttype: None,
277+
schemaurl: None,
278+
subject: None,
279+
time: Some(DateTime::<Utc>::from_utc(
280+
NaiveDateTime::from_timestamp(61, 0),
281+
Utc,
282+
)),
283+
};
284+
let b = &mut a.into_iter();
285+
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
286+
287+
assert_eq!(
288+
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
289+
b.next().unwrap()
290+
);
291+
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
292+
assert_eq!(
293+
("type", AttributeValue::String("someType")),
294+
b.next().unwrap()
295+
);
296+
assert_eq!(
297+
(
298+
"source",
299+
AttributeValue::URIRef(&Url::parse(&"https://example.net".to_string()).unwrap())
300+
),
301+
b.next().unwrap()
302+
);
303+
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
304+
}
262305
}

src/event/v03/builder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use chrono::{DateTime, Utc};
88
use std::collections::HashMap;
99
use std::convert::TryInto;
1010
use std::prelude::v1::*;
11+
12+
#[cfg(not(feature = "std"))]
13+
use super::super::Url;
14+
#[cfg(feature = "std")]
1115
use url::Url;
1216

1317
/// Builder to create a CloudEvent V0.3

src/event/v03/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use serde::{Deserialize, Serializer};
88
use serde_json::{Map, Value};
99
use std::collections::HashMap;
1010
use std::prelude::v1::*;
11+
12+
#[cfg(not(feature = "std"))]
13+
use super::super::{Url, UrlExtend};
14+
#[cfg(feature = "std")]
1115
use url::Url;
1216

1317
pub(crate) struct EventFormatDeserializer {}

src/event/v10/attributes.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use core::fmt::Debug;
66
use std::prelude::v1::*;
77
use uuid::Uuid;
88

9+
#[cfg(not(feature = "std"))]
10+
use crate::event::Url;
911
#[cfg(feature = "std")]
1012
use url::Url;
11-
#[cfg(not(feature = "std"))]
12-
use String as Url;
1313

1414
pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
1515
"specversion",
@@ -240,8 +240,10 @@ impl AttributesConverter for Attributes {
240240
#[cfg(test)]
241241
mod tests {
242242
use super::*;
243+
use crate::event::UrlExtend;
243244
use chrono::NaiveDateTime;
244245

246+
#[cfg(feature = "std")]
245247
#[test]
246248
fn iterator_test_v10() {
247249
let a = Attributes {
@@ -277,4 +279,41 @@ mod tests {
277279
);
278280
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
279281
}
282+
283+
#[cfg(not(feature = "std"))]
284+
#[test]
285+
fn iterator_test_v10() {
286+
let a = Attributes {
287+
id: String::from("1"),
288+
ty: String::from("someType"),
289+
source: Url::parse(&"https://example.net".to_string()).unwrap(),
290+
datacontenttype: None,
291+
dataschema: None,
292+
subject: None,
293+
time: Some(DateTime::<Utc>::from_utc(
294+
NaiveDateTime::from_timestamp(61, 0),
295+
Utc,
296+
)),
297+
};
298+
let b = &mut a.into_iter();
299+
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
300+
301+
assert_eq!(
302+
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
303+
b.next().unwrap()
304+
);
305+
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
306+
assert_eq!(
307+
("type", AttributeValue::String("someType")),
308+
b.next().unwrap()
309+
);
310+
assert_eq!(
311+
(
312+
"source",
313+
AttributeValue::URIRef(&Url::parse(&"https://example.net".to_string()).unwrap())
314+
),
315+
b.next().unwrap()
316+
);
317+
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
318+
}
280319
}

src/event/v10/builder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use chrono::{DateTime, Utc};
88
use std::collections::HashMap;
99
use std::convert::TryInto;
1010
use std::prelude::v1::*;
11+
12+
#[cfg(not(feature = "std"))]
13+
use super::super::Url;
14+
#[cfg(feature = "std")]
1115
use url::Url;
1216

1317
/// Builder to create a CloudEvent V1.0

src/event/v10/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use serde::{Deserialize, Serializer};
88
use serde_json::{Map, Value};
99
use std::collections::HashMap;
1010
use std::prelude::v1::*;
11+
12+
#[cfg(not(feature = "std"))]
13+
use super::super::{Url, UrlExtend};
14+
#[cfg(feature = "std")]
1115
use url::Url;
1216

1317
pub(crate) struct EventFormatDeserializer {}

src/message/error.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use core::fmt::{self, Debug, Display};
22
use snafu::Snafu;
33
use std::prelude::v1::*;
44

5+
#[cfg(feature = "std")]
6+
use url;
7+
#[cfg(not(feature = "std"))]
8+
use String as url;
9+
510
pub struct DisplayError<T>(pub T);
611

712
impl<T> Debug for DisplayError<T>
@@ -47,12 +52,19 @@ pub enum Error {
4752
#[snafu(source(from(chrono::ParseError, DisplayError)))]
4853
source: DisplayError<chrono::ParseError>,
4954
},
55+
5056
#[snafu(display("Error while parsing a url: {}", source))]
5157
#[snafu(context(false))]
5258
ParseUrlError {
59+
#[cfg(not(feature = "std"))]
60+
#[snafu(source(from(String, DisplayError)))]
61+
source: DisplayError<String>,
62+
63+
#[cfg(feature = "std")]
5364
#[snafu(source(from(url::ParseError, DisplayError)))]
5465
source: DisplayError<url::ParseError>,
5566
},
67+
5668
#[snafu(display("Error while decoding base64: {}", source))]
5769
#[snafu(context(false))]
5870
Base64DecodingError {
File renamed without changes.

tests/attributes_iter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use cloudevents::event::AttributeValue;
33
use cloudevents::event::SpecVersion;
44
use test_data::*;
55

6+
#[cfg(feature="std")]
67
#[test]
78
fn iter_v10_test() {
89
let in_event = v10::full_no_data();
@@ -14,6 +15,7 @@ fn iter_v10_test() {
1415
);
1516
}
1617

18+
#[cfg(feature="std")]
1719
#[test]
1820
fn iter_v03_test() {
1921
let in_event = v03::full_json_data();

tests/builder_v03.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ use cloudevents::event::{
77
};
88
use cloudevents::EventBuilderV03;
99
use std::convert::TryInto;
10+
11+
use cloudevents::event::UrlExtend;
12+
#[cfg(feature = "std")]
1013
use url::Url;
14+
#[cfg(not(feature = "std"))]
15+
use String as Url;
1116

17+
#[cfg(feature = "std")]
1218
#[test]
1319
fn build_event() {
1420
let id = "aaa";

tests/builder_v10.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ use cloudevents::event::{
77
};
88
use cloudevents::EventBuilderV10;
99
use std::convert::TryInto;
10+
11+
use cloudevents::event::UrlExtend;
12+
#[cfg(feature = "std")]
1013
use url::Url;
14+
#[cfg(not(feature = "std"))]
15+
use String as Url;
1116

17+
#[cfg(feature = "std")]
1218
#[test]
1319
fn build_event() {
1420
let id = "aaa";

0 commit comments

Comments
 (0)