Skip to content

Commit

Permalink
fixes#2
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-bhatt committed Jan 3, 2021
1 parent 5308b13 commit f7e776d
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 19 deletions.
19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,25 @@ categories = ["web-programming", "encoding", "data-structures"]
name = "cloudevents"

[features]
default = ["std"]
default = ["no_std"]
std = ["no_std_compat_bypass","snafu/std", "snafu/guide", "serde/std", "serde_json/std", "chrono/std", "base64/std", "url"]
no_std = ["no_std_compat_bypass", "serde_no_std", "chrono_no_std", "base64/alloc"]

no_std_compat_bypass=["no-std-compat/std"]
no_std_compat_layer=["no-std-compat/alloc", "no-std-compat/compat_hash", "no-std-compat/compat_sync"]
chrono_no_std = ["chrono/serde", "chrono/alloc", "chrono/clock"]
no_std = ["no-std-compat", "serde_no_std", "chrono_no_std", "base64/alloc"]
serde_no_std = ["serde/derive", "serde/alloc", "serde_json/alloc"]
std = ["snafu/std", "snafu/guide", "serde/std", "serde_json/std", "chrono/std", "base64/std", "url"]

[dependencies]
base64 = { version = "^0.12", default-features = false }
chrono = { version = "^0.4", default-features = false }
chrono = { version = "^0.4", default-features = false , features = ["serde", "clock"]}
delegate-attr = "^0.2"
serde = { version = "^1.0", default-features=false }
serde_json = { version = "^1.0", default-features = false, features = ["alloc"] }
serde-value = "^0.7"
snafu = { version = "^0.6",default-features = false }
snafu = { version = "^0.6", default-features = false }
url = { version = "^2.1", features = ["serde"], optional=true }

[dependencies.no-std-compat]
version = "^0.4.1"
features = ["alloc", "compat_hash", "compat_sync", "compat_macros"]
optional=true
no-std-compat = { version = "^0.4.1", features = ["alloc", "compat_hash", "compat_sync"], optional=true }

[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
hostname = "^0.3"
Expand Down
4 changes: 2 additions & 2 deletions src/event/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use serde::Serializer;
use std::fmt;
use std::prelude::v1::*;

#[cfg(not(feature = "std"))]
use std::string::String as Url;
#[cfg(feature = "std")]
use url::Url;
#[cfg(not(feature = "std"))]
use String as Url;

/// Enum representing a borrowed value of a CloudEvent attribute.
/// This represents the types defined in the [CloudEvent spec type system](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system)
Expand Down
2 changes: 1 addition & 1 deletion src/event/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{DisplayError, Event};
use super::{url, DisplayError, Event};
use snafu::Snafu;

/// Trait to implement a builder for [`Event`]:
Expand Down
32 changes: 31 additions & 1 deletion src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,39 @@ pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10;
use chrono::{DateTime, Utc};
use delegate_attr::delegate;
use std::collections::HashMap;
//use std::fmt;
use std::prelude::v1::*;

#[cfg(feature = "std")]
use url::Url;
#[cfg(not(feature = "std"))]
use String as Url;

pub trait UrlExtend {
fn parse(&self) -> Result<Url, url::ParseError>;
}

impl UrlExtend for Url {
fn parse(&self) -> Result<Url, url::ParseError> {
Ok(self.to_string())
}
}

pub mod url {
use super::{fmt, String};

#[derive(Debug, Clone)]
pub enum ParseError {
Error(String),
}

impl snafu::Error for ParseError {}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt(f)
}
}
}

use core::fmt::{self, Debug, Display};
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
Expand Down
1 change: 0 additions & 1 deletion src/event/spec_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::export::Formatter;
use std::convert::TryFrom;
use std::fmt;
use std::prelude::v1::*;
use std::vec;

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

Expand Down
16 changes: 15 additions & 1 deletion src/event/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use chrono::{DateTime, Utc};
use std::prelude::v1::*;
use url::Url;

use super::url;
#[cfg(not(feature = "std"))]
use super::{Url, UrlExtend};
#[cfg(feature = "std")]
use url::{self, ParseError, Url};

/// Trait to define conversion to [`Url`]
pub trait TryIntoUrl {
Expand All @@ -13,12 +18,21 @@ impl TryIntoUrl for Url {
}
}

#[cfg(not(feature = "std"))]
impl TryIntoUrl for &str {
fn into_url(self) -> Result<Url, url::ParseError> {
Url::parse(&self.to_string())
}
}

#[cfg(feature = "std")]
impl TryIntoUrl for &str {
fn into_url(self) -> Result<Url, url::ParseError> {
Url::parse(self)
}
}

#[cfg(feature = "std")]
impl TryIntoUrl for String {
fn into_url(self) -> Result<Url, url::ParseError> {
self.as_str().into_url()
Expand Down
45 changes: 44 additions & 1 deletion src/event/v03/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
use crate::message::{BinarySerializer, MessageAttributeValue};
use chrono::{DateTime, Utc};
use std::prelude::v1::*;
use url::Url;
use uuid::Uuid;

#[cfg(not(feature = "std"))]
use super::super::Url;
#[cfg(feature = "std")]
use url::Url;

pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
"specversion",
"id",
Expand Down Expand Up @@ -222,8 +226,10 @@ impl crate::event::message::AttributesDeserializer for super::Attributes {
#[cfg(test)]
mod tests {
use super::*;
use crate::event::UrlExtend;
use chrono::NaiveDateTime;

#[cfg(feature = "std")]
#[test]
fn iterator_test_v03() {
let a = Attributes {
Expand Down Expand Up @@ -259,4 +265,41 @@ mod tests {
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}

#[cfg(not(feature = "std"))]
#[test]
fn iterator_test_v03() {
let a = Attributes {
id: String::from("1"),
ty: String::from("someType"),
source: Url::parse(&"https://example.net".to_string()).unwrap(),
datacontenttype: None,
schemaurl: None,
subject: None,
time: Some(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(61, 0),
Utc,
)),
};
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);

assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
b.next().unwrap()
);
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),
b.next().unwrap()
);
assert_eq!(
(
"source",
AttributeValue::URIRef(&Url::parse(&"https://example.net".to_string()).unwrap())
),
b.next().unwrap()
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}
}
4 changes: 4 additions & 0 deletions src/event/v03/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use chrono::{DateTime, Utc};
use std::collections::HashMap;
use std::convert::TryInto;
use std::prelude::v1::*;

#[cfg(not(feature = "std"))]
use super::super::Url;
#[cfg(feature = "std")]
use url::Url;

/// Builder to create a CloudEvent V0.3
Expand Down
4 changes: 4 additions & 0 deletions src/event/v03/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use serde::{Deserialize, Serializer};
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::prelude::v1::*;

#[cfg(not(feature = "std"))]
use super::super::{Url, UrlExtend};
#[cfg(feature = "std")]
use url::Url;

pub(crate) struct EventFormatDeserializer {}
Expand Down
43 changes: 41 additions & 2 deletions src/event/v10/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use core::fmt::Debug;
use std::prelude::v1::*;
use uuid::Uuid;

#[cfg(not(feature = "std"))]
use crate::event::Url;
#[cfg(feature = "std")]
use url::Url;
#[cfg(not(feature = "std"))]
use String as Url;

pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
"specversion",
Expand Down Expand Up @@ -240,8 +240,10 @@ impl AttributesConverter for Attributes {
#[cfg(test)]
mod tests {
use super::*;
use crate::event::UrlExtend;
use chrono::NaiveDateTime;

#[cfg(feature = "std")]
#[test]
fn iterator_test_v10() {
let a = Attributes {
Expand Down Expand Up @@ -277,4 +279,41 @@ mod tests {
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}

#[cfg(not(feature = "std"))]
#[test]
fn iterator_test_v10() {
let a = Attributes {
id: String::from("1"),
ty: String::from("someType"),
source: Url::parse(&"https://example.net".to_string()).unwrap(),
datacontenttype: None,
dataschema: None,
subject: None,
time: Some(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(61, 0),
Utc,
)),
};
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);

assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
b.next().unwrap()
);
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),
b.next().unwrap()
);
assert_eq!(
(
"source",
AttributeValue::URIRef(&Url::parse(&"https://example.net".to_string()).unwrap())
),
b.next().unwrap()
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}
}
4 changes: 4 additions & 0 deletions src/event/v10/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use chrono::{DateTime, Utc};
use std::collections::HashMap;
use std::convert::TryInto;
use std::prelude::v1::*;

#[cfg(not(feature = "std"))]
use super::super::Url;
#[cfg(feature = "std")]
use url::Url;

/// Builder to create a CloudEvent V1.0
Expand Down
4 changes: 4 additions & 0 deletions src/event/v10/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use serde::{Deserialize, Serializer};
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::prelude::v1::*;

#[cfg(not(feature = "std"))]
use super::super::{Url, UrlExtend};
#[cfg(feature = "std")]
use url::Url;

pub(crate) struct EventFormatDeserializer {}
Expand Down
12 changes: 12 additions & 0 deletions src/message/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ use core::fmt::{self, Debug, Display};
use snafu::Snafu;
use std::prelude::v1::*;

#[cfg(feature = "std")]
use url;
#[cfg(not(feature = "std"))]
use String as url;

pub struct DisplayError<T>(pub T);

impl<T> Debug for DisplayError<T>
Expand Down Expand Up @@ -47,12 +52,19 @@ pub enum Error {
#[snafu(source(from(chrono::ParseError, DisplayError)))]
source: DisplayError<chrono::ParseError>,
},

#[snafu(display("Error while parsing a url: {}", source))]
#[snafu(context(false))]
ParseUrlError {
#[cfg(not(feature = "std"))]
#[snafu(source(from(String, DisplayError)))]
source: DisplayError<String>,

#[cfg(feature = "std")]
#[snafu(source(from(url::ParseError, DisplayError)))]
source: DisplayError<url::ParseError>,
},

#[snafu(display("Error while decoding base64: {}", source))]
#[snafu(context(false))]
Base64DecodingError {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/attributes_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cloudevents::event::AttributeValue;
use cloudevents::event::SpecVersion;
use test_data::*;

#[cfg(feature="std")]
#[test]
fn iter_v10_test() {
let in_event = v10::full_no_data();
Expand All @@ -14,6 +15,7 @@ fn iter_v10_test() {
);
}

#[cfg(feature="std")]
#[test]
fn iter_v03_test() {
let in_event = v03::full_json_data();
Expand Down
6 changes: 6 additions & 0 deletions tests/builder_v03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ use cloudevents::event::{
};
use cloudevents::EventBuilderV03;
use std::convert::TryInto;

use cloudevents::event::UrlExtend;
#[cfg(feature = "std")]
use url::Url;
#[cfg(not(feature = "std"))]
use String as Url;

#[cfg(feature = "std")]
#[test]
fn build_event() {
let id = "aaa";
Expand Down
6 changes: 6 additions & 0 deletions tests/builder_v10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ use cloudevents::event::{
};
use cloudevents::EventBuilderV10;
use std::convert::TryInto;

use cloudevents::event::UrlExtend;
#[cfg(feature = "std")]
use url::Url;
#[cfg(not(feature = "std"))]
use String as Url;

#[cfg(feature = "std")]
#[test]
fn build_event() {
let id = "aaa";
Expand Down
Loading

0 comments on commit f7e776d

Please sign in to comment.