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

[WIP] Support no_std #121

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions .github/workflows/rust_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ jobs:

# If glibc, compile and test all
- uses: actions-rs/cargo@v1
name: "Build"
name: "Build glibc"
if: matrix.target == 'x86_64-unknown-linux-gnu'
with:
command: build
toolchain: ${{ matrix.toolchain }}
args: --target ${{ matrix.target }} --all-features
- uses: actions-rs/cargo@v1
name: "Test"
name: "Test glibc"
if: matrix.target == 'x86_64-unknown-linux-gnu'
with:
command: test
Expand Down Expand Up @@ -129,3 +129,31 @@ jobs:
command: build
toolchain: ${{ matrix.toolchain }}
args: --target ${{ matrix.target }} --manifest-path ./example-projects/warp-example/Cargo.toml

check_no_std:
name: Check no_std
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
# Caching stuff
- uses: actions/cache@v2
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
key: ${{ runner.os }}-cargo-no-std-deps-${{ hashFiles('**/Cargo.toml') }}
- uses: actions/cache@v2
with:
path: |
target/
key: ${{ runner.os }}-cargo-no-std-build-${{ hashFiles('**/Cargo.toml') }}
- name: Download cargo-nono
run: curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git hobofan/cargo-nono
- name: Run check
run: ./cargo-nono check
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ repository = "https://github.com/cloudevents/sdk-rust"
exclude = [
".github/*"
]
categories = ["web-programming", "encoding", "data-structures"]
categories = ["web-programming", "encoding", "data-structures", "no_std"]

[lib]
name = "cloudevents"

[features]
# Without default features, the package acts as no_std
default = ["std"]

std = ["snafu/std", "url"]

actix = ["actix-web", "async-trait", "lazy_static", "bytes", "futures"]
reqwest = ["reqwest-lib", "async-trait", "lazy_static", "bytes"]
rdkafka = ["rdkafka-lib", "lazy_static", "bytes", "futures"]
Expand All @@ -28,9 +33,9 @@ serde_json = "^1.0"
chrono = { version = "^0.4", features = ["serde"] }
delegate-attr = "^0.2"
base64 = "^0.12"
url = { version = "^2.1", features = ["serde"] }
snafu = "^0.6"
snafu = { version = "^0.6", default-features = false }
bitflags = "^1.2"
url = { version = "^2.1", features = ["serde"], optional = true }

# runtime optional deps
actix-web = { version = "^3", default-features = false, optional = true }
Expand All @@ -49,7 +54,7 @@ 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
15 changes: 15 additions & 0 deletions example-projects/no-std-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "no-std-example"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cloudevents-sdk = { path = "../..", default-features = false }

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
25 changes: 25 additions & 0 deletions example-projects/no-std-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_std]
#![no_main]

use core::panic::PanicInfo;

use cloudevents;
use cloudevents::EventBuilder;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {
#[allow(dead_code)]
let event = cloudevents::EventBuilderV10::new()
.id("my_id")
.source("my_source")
.subject("some_subject")
.build()
.unwrap();
}
}
51 changes: 23 additions & 28 deletions src/event/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use super::{
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10,
ExtensionValue, SpecVersion, UriReference,
types::*, AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10,
ExtensionValue, SpecVersion,
};
use chrono::{DateTime, Utc};
use serde::Serializer;
use std::fmt;
use url::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)
#[derive(Debug, PartialEq, Eq)]
pub enum AttributeValue<'a> {
SpecVersion(SpecVersion),
String(&'a str),
URI(&'a Url),
URI(&'a Uri),
URIRef(&'a UriReference),
Boolean(&'a bool),
Integer(&'a i64),
Expand Down Expand Up @@ -57,7 +56,7 @@ pub trait AttributesReader {
/// Get the [datacontenttype](https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype).
fn datacontenttype(&self) -> Option<&str>;
/// Get the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
fn dataschema(&self) -> Option<&Url>;
fn dataschema(&self) -> Option<&Uri>;
/// Get the [subject](https://github.com/cloudevents/spec/blob/master/spec.md#subject).
fn subject(&self) -> Option<&str>;
/// Get the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
Expand Down Expand Up @@ -87,7 +86,7 @@ pub trait AttributesWriter {
-> Option<String>;
/// Set the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
/// Returns the previous value.
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri>;
}

pub(crate) trait AttributesConverter {
Expand Down Expand Up @@ -154,7 +153,7 @@ impl AttributesReader for Attributes {
}
}

fn dataschema(&self) -> Option<&Url> {
fn dataschema(&self) -> Option<&Uri> {
match self {
Attributes::V03(a) => a.dataschema(),
Attributes::V10(a) => a.dataschema(),
Expand Down Expand Up @@ -222,7 +221,7 @@ impl AttributesWriter for Attributes {
}
}

fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url> {
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri> {
match self {
Attributes::V03(a) => a.set_dataschema(dataschema),
Attributes::V10(a) => a.set_dataschema(dataschema),
Expand Down Expand Up @@ -253,31 +252,27 @@ impl Attributes {
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn default_hostname() -> Url {
Url::parse(
format!(
"http://{}",
hostname::get()
.ok()
.map(|s| s.into_string().ok())
.flatten()
.unwrap_or_else(|| "localhost".to_string())
)
.as_ref(),
pub(crate) fn default_hostname() -> Uri {
format!(
"http://{}",
hostname::get()
.ok()
.map(|s| s.into_string().ok())
.flatten()
.unwrap_or_else(|| "localhost".to_string())
)
.into_uri()
.unwrap()
}

#[cfg(target_arch = "wasm32")]
pub(crate) fn default_hostname() -> Url {
pub(crate) fn default_hostname() -> Uri {
use std::str::FromStr;

Url::from_str(
web_sys::window()
.map(|w| w.location().host().ok())
.flatten()
.unwrap_or(String::from("http://localhost"))
.as_str(),
)
.unwrap()
web_sys::window()
.map(|w| w.location().host().ok())
.flatten()
.unwrap_or(String::from("http://localhost"))
.into_uri()
.unwrap()
}
6 changes: 3 additions & 3 deletions src/event/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::types;
use super::Event;
use snafu::Snafu;

/// Trait to implement a builder for [`Event`]:
/// ```
/// use cloudevents::event::{EventBuilderV10, EventBuilder};
/// use chrono::Utc;
/// use url::Url;
///
/// let event = EventBuilderV10::new()
/// .id("my_event.my_application")
Expand Down Expand Up @@ -48,9 +48,9 @@ pub enum Error {
attribute_name,
source
))]
ParseUrlError {
ParseUriError {
attribute_name: &'static str,
source: url::ParseError,
source: types::ParseUriError,
},
#[snafu(display(
"Invalid value setting attribute '{}' with uriref type",
Expand Down
14 changes: 6 additions & 8 deletions src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod extensions;
mod format;
mod message;
mod spec_version;
mod types;
pub mod types;

pub use attributes::Attributes;
pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
Expand All @@ -20,7 +20,6 @@ pub(crate) use message::EventBinarySerializer;
pub(crate) use message::EventStructuredSerializer;
pub use spec_version::SpecVersion;
pub use spec_version::UnknownSpecVersion;
pub use types::{TryIntoTime, TryIntoUrl, UriReference};

mod v03;

Expand All @@ -38,11 +37,10 @@ pub use v10::EventBuilder as EventBuilderV10;
pub(crate) use v10::EventFormatDeserializer as EventFormatDeserializerV10;
pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10;

use chrono::{DateTime, Utc};
use delegate_attr::delegate;
use std::collections::HashMap;
use std::fmt;
use url::Url;
use types::*;

/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
/// It provides methods to get the attributes through [`AttributesReader`]
Expand Down Expand Up @@ -89,7 +87,7 @@ impl AttributesReader for Event {
fn specversion(&self) -> SpecVersion;
fn ty(&self) -> &str;
fn datacontenttype(&self) -> Option<&str>;
fn dataschema(&self) -> Option<&Url>;
fn dataschema(&self) -> Option<&Uri>;
fn subject(&self) -> Option<&str>;
fn time(&self) -> Option<&DateTime<Utc>>;
}
Expand All @@ -103,7 +101,7 @@ impl AttributesWriter for Event {
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>)
-> Option<String>;
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri>;
}

impl Default for Event {
Expand Down Expand Up @@ -165,10 +163,10 @@ impl Event {
///
/// let (datacontenttype, dataschema, data) = e.take_data();
/// ```
pub fn take_data(&mut self) -> (Option<String>, Option<Url>, Option<Data>) {
pub fn take_data(&mut self) -> (Option<String>, Option<Uri>, Option<Data>) {
(
self.attributes.set_datacontenttype(None as Option<String>),
self.attributes.set_dataschema(None as Option<Url>),
self.attributes.set_dataschema(None as Option<Uri>),
self.data.take(),
)
}
Expand Down
Loading