Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making core no_std compatible #94

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .history/src/lib_20201107132414.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! This crate implements the [CloudEvents](https://cloudevents.io/) Spec for Rust.
//!
//! ```
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! use cloudevents::{EventBuilder, AttributesReader, EventBuilderV10};
//! use chrono::{Utc, DateTime};
//! use url::Url;
//!
//! let event = EventBuilderV10::new()
//! .id("my_event.my_application")
//! .source("http://localhost:8080")
//! .ty("example.demo")
//! .time(Utc::now())
//! .build()?;
//!
//! println!("CloudEvent Id: {}", event.id());
//! match event.time() {
//! Some(t) => println!("CloudEvent Time: {}", t),
//! None => println!("CloudEvent Time: None")
//! }
//! # Ok(())
//! # }
//! ```
//!
//! This crate includes:
//!
//! * The [`Event`] data structure, to represent CloudEvent (version 1.0 and 0.3)
//! * The [`EventBuilder`] trait and implementations, to create [`Event`] instances
//! * The implementation of [`serde::Serialize`] and [`serde::Deserialize`] for [`Event`] to serialize/deserialize CloudEvents to/from JSON
//! * Traits and utilities in [`message`] to implement Protocol Bindings
//!
//! If you're looking for Protocol Binding implementations, look at crates:
//!
//! * [cloudevents-sdk-actix-web](https://docs.rs/cloudevents-sdk-actix-web): Integration with [Actix Web](https://github.com/actix/actix-web)
//! * [cloudevents-sdk-reqwest](https://docs.rs/cloudevents-sdk-reqwest): Integration with [reqwest](https://github.com/seanmonstar/reqwest)
//! * [cloudevents-sdk-rdkafka](https://docs.rs/cloudevents-sdk-rdkafka): Integration with [rdkafka](https://fede1024.github.io/rust-rdkafka)
//!

<<<<<<< master
#![doc(html_root_url = "https://docs.rs/cloudevents-sdk/0.3.0")]
#![deny(broken_intra_doc_links)]
=======
#![no_std]

extern crate no_std_compat as std;

extern crate serde;
extern crate serde_json;
extern crate serde_value;
extern crate snafu;
>>>>>>> no_std #1

pub mod event;
pub mod message;

pub use event::Data;
pub use event::Event;
pub use event::{AttributesReader, AttributesWriter};
pub use event::{EventBuilder, EventBuilderV03, EventBuilderV10};
57 changes: 57 additions & 0 deletions .history/src/lib_20201107132439.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! This crate implements the [CloudEvents](https://cloudevents.io/) Spec for Rust.
//!
//! ```
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! use cloudevents::{EventBuilder, AttributesReader, EventBuilderV10};
//! use chrono::{Utc, DateTime};
//! use url::Url;
//!
//! let event = EventBuilderV10::new()
//! .id("my_event.my_application")
//! .source("http://localhost:8080")
//! .ty("example.demo")
//! .time(Utc::now())
//! .build()?;
//!
//! println!("CloudEvent Id: {}", event.id());
//! match event.time() {
//! Some(t) => println!("CloudEvent Time: {}", t),
//! None => println!("CloudEvent Time: None")
//! }
//! # Ok(())
//! # }
//! ```
//!
//! This crate includes:
//!
//! * The [`Event`] data structure, to represent CloudEvent (version 1.0 and 0.3)
//! * The [`EventBuilder`] trait and implementations, to create [`Event`] instances
//! * The implementation of [`serde::Serialize`] and [`serde::Deserialize`] for [`Event`] to serialize/deserialize CloudEvents to/from JSON
//! * Traits and utilities in [`message`] to implement Protocol Bindings
//!
//! If you're looking for Protocol Binding implementations, look at crates:
//!
//! * [cloudevents-sdk-actix-web](https://docs.rs/cloudevents-sdk-actix-web): Integration with [Actix Web](https://github.com/actix/actix-web)
//! * [cloudevents-sdk-reqwest](https://docs.rs/cloudevents-sdk-reqwest): Integration with [reqwest](https://github.com/seanmonstar/reqwest)
//! * [cloudevents-sdk-rdkafka](https://docs.rs/cloudevents-sdk-rdkafka): Integration with [rdkafka](https://fede1024.github.io/rust-rdkafka)
//!

#![doc(html_root_url = "https://docs.rs/cloudevents-sdk/0.3.0")]
#![deny(broken_intra_doc_links)]
#![no_std]

extern crate no_std_compat as std;

extern crate serde;
extern crate serde_json;
extern crate serde_value;
extern crate snafu;

pub mod event;
pub mod message;

pub use event::Data;
pub use event::Event;
pub use event::{AttributesReader, AttributesWriter};
pub use event::{EventBuilder, EventBuilderV03, EventBuilderV10};
27 changes: 19 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@ categories = ["web-programming", "encoding", "data-structures"]
[lib]
name = "cloudevents"

[features]
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"]
serde_no_std = ["serde/derive", "serde/alloc", "serde_json/alloc"]

[dependencies]
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
chrono = { version = "^0.4", features = ["serde"] }
base64 = { version = "^0.12", default-features = false }
chrono = { version = "^0.4", default-features = false , features = ["serde", "clock"]}
delegate-attr = "^0.2"
base64 = "^0.12"
url = { version = "^2.1", features = ["serde"] }
snafu = "^0.6"
bitflags = "^1.2"
serde = { version = "^1.0", default-features=false }
serde_json = { version = "^1.0", default-features = false, features = ["alloc"] }
snafu = { version = "^0.6", default-features = false }
url = { version = "^2.1", features = ["serde"], 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"
uuid = { version = "^0.8", features = ["v4"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "^0.3", features = ["Window", "Location"] }
web-sys = { version = "^0.3", default-features = false,features = ["Window", "Location"] }
uuid = { version = "^0.8", features = ["v4", "wasm-bindgen"] }

[dev-dependencies]
Expand Down
12 changes: 12 additions & 0 deletions src/event/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use super::{
use chrono::{DateTime, Utc};
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;

/// Enum representing a borrowed value of a CloudEvent attribute.
Expand Down Expand Up @@ -252,6 +257,7 @@ impl Attributes {
}
}

#[cfg(feature = "std")]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn default_hostname() -> Url {
Url::parse(
Expand All @@ -268,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 @@ -281,3 +288,8 @@ pub(crate) fn default_hostname() -> Url {
)
.unwrap()
}

#[cfg(not(feature = "std"))]
pub(crate) fn default_hostname() -> Url {
String::from("http://localhost")
}
8 changes: 5 additions & 3 deletions src/event/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Event;
use super::{url, DisplayError, Event};
use snafu::Snafu;

/// Trait to implement a builder for [`Event`]:
Expand Down Expand Up @@ -41,7 +41,8 @@ pub enum Error {
))]
ParseTimeError {
attribute_name: &'static str,
source: chrono::ParseError,
#[snafu(source(from(chrono::ParseError, DisplayError)))]
source: DisplayError<chrono::ParseError>,
},
#[snafu(display(
"Error while setting attribute '{}' with uri/uriref type: {}",
Expand All @@ -50,6 +51,7 @@ pub enum Error {
))]
ParseUrlError {
attribute_name: &'static str,
source: url::ParseError,
#[snafu(source(from(url::ParseError, DisplayError)))]
source: DisplayError<url::ParseError>,
},
}
1 change: 1 addition & 0 deletions src/event/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde_json::Value;
use std::convert::TryFrom;
use std::fmt;
use std::fmt::Formatter;
use std::prelude::v1::*;

/// Event [data attribute](https://github.com/cloudevents/spec/blob/master/spec.md#event-data) representation
#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down
Loading