Skip to content

Commit

Permalink
Cleaned up cargo.toml+fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-bhatt committed Jan 3, 2021
1 parent e652e12 commit d296862
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 28 deletions.
31 changes: 16 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ categories = ["web-programming", "encoding", "data-structures"]
[lib]
name = "cloudevents"

[features]
default = ["std"]
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]
serde = { version = "^1.0", default-features=false,features = ["derive","alloc"] }
base64 = { version = "^0.12", default-features = false }
chrono = { version = "^0.4", default-features = false }
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"
chrono = { version = "^0.4", default-features=false ,features = ["serde","alloc","clock"] }
delegate-attr = "^0.2"
base64 = { version = "^0.12", default-features = false, features = ["alloc"] }
url = { version = "^2.1", features = ["serde"] }
core-error = "0.0.1-rc4"

[dependencies.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]
git = "https://gitlab.com/jD91mZM2/no-std-compat.git"
features = ["alloc","compat_hash"]


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

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

#[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 Expand Up @@ -253,6 +257,7 @@ impl Attributes {
}
}

#[cfg(feature = "std")]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn default_hostname() -> Url {
Url::parse(
Expand All @@ -269,6 +274,7 @@ pub(crate) fn default_hostname() -> Url {
.unwrap()
}

#[cfg(feature = "std")]
#[cfg(target_arch = "wasm32")]
pub(crate) fn default_hostname() -> Url {
use std::str::FromStr;
Expand All @@ -282,3 +288,8 @@ pub(crate) fn default_hostname() -> Url {
)
.unwrap()
}

#[cfg(not(feature = "std"))]
pub(crate) fn default_hostname() -> Url {
String::from("http://localhost")
}
5 changes: 2 additions & 3 deletions src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::collections::HashMap;
use std::prelude::v1::*;
use url::Url;

use core::fmt::{self,Debug, Display};
use core::fmt::{self, Debug, Display};
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
/// It provides methods to get the attributes through [`AttributesReader`]
/// and write them through [`AttributesWriter`].
Expand Down Expand Up @@ -237,7 +237,7 @@ impl Event {
}
}

// Facilitates compatibility with snafu::Error for external objects
// Facilitates compatibility with snafu::Error for external objects

#[derive(PartialEq, Eq, Clone)]
pub struct DisplayError<T>(pub T);
Expand All @@ -262,7 +262,6 @@ where

impl<T> snafu::Error for DisplayError<T> where T: Display + Debug {}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions src/event/spec_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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
20 changes: 19 additions & 1 deletion src/event/v10/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use crate::message::{BinarySerializer, MessageAttributeValue};
use chrono::{DateTime, Utc};
use core::fmt::Debug;
use std::prelude::v1::*;
use url::Url;
use uuid::Uuid;

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

pub(crate) const ATTRIBUTE_NAMES: [&str; 8] = [
"specversion",
"id",
Expand Down Expand Up @@ -206,6 +210,20 @@ impl AttributesConverter for Attributes {
self
}

#[cfg(feature = "std")]
fn into_v03(self) -> AttributesV03 {
AttributesV03 {
id: self.id,
ty: self.ty,
source: self.source,
datacontenttype: self.datacontenttype,
schemaurl: self.dataschema,
subject: self.subject,
time: self.time,
}
}

#[cfg(not(feature = "std"))]
fn into_v03(self) -> AttributesV03 {
AttributesV03 {
id: self.id,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
#![deny(broken_intra_doc_links)]
#![no_std]

extern crate core_error;
#[cfg(not(feature = "std"))]
extern crate no_std_compat as std;
#[cfg(feature = "std")]
extern crate std;

extern crate serde;
extern crate serde_json;
Expand Down
13 changes: 7 additions & 6 deletions src/message/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ pub enum Error {
#[snafu(source(from(serde_json::Error, DisplayError)))]
source: DisplayError<serde_json::Error>,
},

#[cfg(feature = "std")]
#[snafu(display("IO Error: {}", source))]
#[snafu(context(false))]
IOError {
#[snafu(source(from(super::no_std_io::IoError, DisplayError)))]
source: DisplayError<super::no_std_io::IoError>,
},
IOError { source: std::io::Error },

#[cfg(feature = "std")]
#[snafu(display("Other error: {}", source))]
Other {
#[snafu(source(from(Box<dyn core_error::Error>, DisplayError)))]
source: DisplayError<Box<dyn core_error::Error>>,
//#[snafu(source(from(Box<dyn core_error::Error>, DisplayError)))]
source: Box<dyn std::error::Error + Send + Sync>,
},
}

Expand Down
2 changes: 0 additions & 2 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
mod deserializer;
mod encoding;
mod error;
mod no_std_io;
mod serializer;
mod types;

pub use deserializer::*;
pub use encoding::*;
pub use error::*;
pub use no_std_io::{IoError, Read, Write};
pub use serializer::*;
pub use types::MessageAttributeValue;
13 changes: 13 additions & 0 deletions src/message/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use chrono::{DateTime, Utc};
use std::convert::TryInto;
use std::fmt;
use std::prelude::v1::*;

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

/// Union type representing a [CloudEvent context attribute type](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system).
#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down Expand Up @@ -33,13 +37,22 @@ impl TryInto<DateTime<Utc>> for MessageAttributeValue {
impl TryInto<Url> for MessageAttributeValue {
type Error = super::Error;

#[cfg(feature = "std")]
fn try_into(self) -> Result<Url, Self::Error> {
match self {
MessageAttributeValue::Uri(u) => Ok(u),
MessageAttributeValue::UriRef(u) => Ok(u),
v => Ok(Url::parse(v.to_string().as_ref())?),
}
}
#[cfg(not(feature = "std"))]
fn try_into(self) -> Result<Url, Self::Error> {
match self {
MessageAttributeValue::Uri(u) => Ok(u),
MessageAttributeValue::UriRef(u) => Ok(u),
v => Ok(v.to_string()),
}
}
}

impl fmt::Display for MessageAttributeValue {
Expand Down

0 comments on commit d296862

Please sign in to comment.