Skip to content

Commit 1205963

Browse files
No_std
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent dfe2bcc commit 1205963

File tree

15 files changed

+192
-128
lines changed

15 files changed

+192
-128
lines changed

Diff for: .github/workflows/rust_tests.yml

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ jobs:
6969
toolchain: ${{ matrix.toolchain }}
7070
args: --target ${{ matrix.target }} --workspace
7171

72+
# If glibc, compile and test only the core module with no_std
73+
- uses: actions-rs/cargo@v1
74+
name: "Build"
75+
if: matrix.target == 'x86_64-unknown-linux-gnu'
76+
with:
77+
command: build
78+
toolchain: ${{ matrix.toolchain }}
79+
args: --target ${{ matrix.target }} --package cloudevents-sdk --workspace --no-default-features
80+
- uses: actions-rs/cargo@v1
81+
name: "Test"
82+
if: matrix.target == 'x86_64-unknown-linux-gnu'
83+
with:
84+
command: test
85+
toolchain: ${{ matrix.toolchain }}
86+
args: --target ${{ matrix.target }} --package cloudevents-sdk --workspace --no-default-features
87+
7288
# If musl, compile and test all
7389
- uses: actions-rs/cargo@v1
7490
name: "Build"

Diff for: Cargo.toml

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repository = "https://github.com/cloudevents/sdk-rust"
1111
exclude = [
1212
".github/*"
1313
]
14-
categories = ["web-programming", "encoding", "data-structures"]
14+
categories = ["web-programming", "encoding", "data-structures", "no_std"]
1515

1616
[lib]
1717
name = "cloudevents"
@@ -22,16 +22,22 @@ serde_json = "^1.0"
2222
chrono = { version = "^0.4", features = ["serde"] }
2323
delegate-attr = "^0.2"
2424
base64 = "^0.12"
25-
url = { version = "^2.1", features = ["serde"] }
26-
snafu = "^0.6"
25+
snafu = { version = "^0.6", default-features = false}
2726
bitflags = "^1.2"
27+
url = { version = "^2.1", features = ["serde"], optional = true }
28+
29+
[features]
30+
# Without default features, the package acts as no_std
31+
default = ["std"]
32+
33+
std = ["snafu/std", "url"]
2834

2935
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
3036
hostname = "^0.3"
3137
uuid = { version = "^0.8", features = ["v4"] }
3238

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

3743
[dev-dependencies]

Diff for: src/event/attributes.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use super::{
22
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10,
3-
ExtensionValue, SpecVersion, UriReference,
3+
ExtensionValue, SpecVersion,
4+
types::*
45
};
56
use chrono::{DateTime, Utc};
67
use serde::Serializer;
78
use std::fmt;
8-
use url::Url;
99

1010
/// Enum representing a borrowed value of a CloudEvent attribute.
1111
/// This represents the types defined in the [CloudEvent spec type system](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system)
1212
#[derive(Debug, PartialEq, Eq)]
1313
pub enum AttributeValue<'a> {
1414
SpecVersion(SpecVersion),
1515
String(&'a str),
16-
URI(&'a Url),
16+
URI(&'a Uri),
1717
URIRef(&'a UriReference),
1818
Boolean(&'a bool),
1919
Integer(&'a i64),
@@ -57,7 +57,7 @@ pub trait AttributesReader {
5757
/// Get the [datacontenttype](https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype).
5858
fn datacontenttype(&self) -> Option<&str>;
5959
/// Get the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
60-
fn dataschema(&self) -> Option<&Url>;
60+
fn dataschema(&self) -> Option<&Uri>;
6161
/// Get the [subject](https://github.com/cloudevents/spec/blob/master/spec.md#subject).
6262
fn subject(&self) -> Option<&str>;
6363
/// Get the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
@@ -87,7 +87,7 @@ pub trait AttributesWriter {
8787
-> Option<String>;
8888
/// Set the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
8989
/// Returns the previous value.
90-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
90+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri>;
9191
}
9292

9393
pub(crate) trait AttributesConverter {
@@ -154,7 +154,7 @@ impl AttributesReader for Attributes {
154154
}
155155
}
156156

157-
fn dataschema(&self) -> Option<&Url> {
157+
fn dataschema(&self) -> Option<&Uri> {
158158
match self {
159159
Attributes::V03(a) => a.dataschema(),
160160
Attributes::V10(a) => a.dataschema(),
@@ -222,7 +222,7 @@ impl AttributesWriter for Attributes {
222222
}
223223
}
224224

225-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url> {
225+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri> {
226226
match self {
227227
Attributes::V03(a) => a.set_dataschema(dataschema),
228228
Attributes::V10(a) => a.set_dataschema(dataschema),
@@ -253,31 +253,27 @@ impl Attributes {
253253
}
254254

255255
#[cfg(not(target_arch = "wasm32"))]
256-
pub(crate) fn default_hostname() -> Url {
257-
Url::parse(
258-
format!(
259-
"http://{}",
260-
hostname::get()
261-
.ok()
262-
.map(|s| s.into_string().ok())
263-
.flatten()
264-
.unwrap_or_else(|| "localhost".to_string())
265-
)
266-
.as_ref(),
256+
pub(crate) fn default_hostname() -> Uri {
257+
format!(
258+
"http://{}",
259+
hostname::get()
260+
.ok()
261+
.map(|s| s.into_string().ok())
262+
.flatten()
263+
.unwrap_or_else(|| "localhost".to_string())
267264
)
265+
.into_uri()
268266
.unwrap()
269267
}
270268

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

275-
Url::from_str(
276-
web_sys::window()
277-
.map(|w| w.location().host().ok())
278-
.flatten()
279-
.unwrap_or(String::from("http://localhost"))
280-
.as_str(),
281-
)
282-
.unwrap()
273+
web_sys::window()
274+
.map(|w| w.location().host().ok())
275+
.flatten()
276+
.unwrap_or(String::from("http://localhost"))
277+
.into_uri()
278+
.unwrap()
283279
}

Diff for: src/event/builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use super::Event;
21
use snafu::Snafu;
2+
use super::Event;
3+
use super::types;
34

45
/// Trait to implement a builder for [`Event`]:
56
/// ```
67
/// use cloudevents::event::{EventBuilderV10, EventBuilder};
78
/// use chrono::Utc;
8-
/// use url::Url;
99
///
1010
/// let event = EventBuilderV10::new()
1111
/// .id("my_event.my_application")
@@ -48,9 +48,9 @@ pub enum Error {
4848
attribute_name,
4949
source
5050
))]
51-
ParseUrlError {
51+
ParseUriError {
5252
attribute_name: &'static str,
53-
source: url::ParseError,
53+
source: types::ParseUriError,
5454
},
5555
#[snafu(display(
5656
"Invalid value setting attribute '{}' with uriref type",

Diff for: src/event/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod extensions;
88
mod format;
99
mod message;
1010
mod spec_version;
11-
mod types;
11+
pub mod types;
1212

1313
pub use attributes::Attributes;
1414
pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
@@ -20,7 +20,6 @@ pub(crate) use message::EventBinarySerializer;
2020
pub(crate) use message::EventStructuredSerializer;
2121
pub use spec_version::SpecVersion;
2222
pub use spec_version::UnknownSpecVersion;
23-
pub use types::{TryIntoTime, TryIntoUrl, UriReference};
2423

2524
mod v03;
2625

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

41-
use chrono::{DateTime, Utc};
4240
use delegate_attr::delegate;
4341
use std::collections::HashMap;
4442
use std::fmt;
45-
use url::Url;
43+
use types::*;
4644

4745
/// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md).
4846
/// It provides methods to get the attributes through [`AttributesReader`]
@@ -89,7 +87,7 @@ impl AttributesReader for Event {
8987
fn specversion(&self) -> SpecVersion;
9088
fn ty(&self) -> &str;
9189
fn datacontenttype(&self) -> Option<&str>;
92-
fn dataschema(&self) -> Option<&Url>;
90+
fn dataschema(&self) -> Option<&Uri>;
9391
fn subject(&self) -> Option<&str>;
9492
fn time(&self) -> Option<&DateTime<Utc>>;
9593
}
@@ -103,7 +101,7 @@ impl AttributesWriter for Event {
103101
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
104102
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>)
105103
-> Option<String>;
106-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
104+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Uri>>) -> Option<Uri>;
107105
}
108106

109107
impl Default for Event {
@@ -166,10 +164,10 @@ impl Event {
166164
///
167165
/// let (datacontenttype, dataschema, data) = e.take_data();
168166
/// ```
169-
pub fn take_data(&mut self) -> (Option<String>, Option<Url>, Option<Data>) {
167+
pub fn take_data(&mut self) -> (Option<String>, Option<Uri>, Option<Data>) {
170168
(
171169
self.attributes.set_datacontenttype(None as Option<String>),
172-
self.attributes.set_dataschema(None as Option<Url>),
170+
self.attributes.set_dataschema(None as Option<Uri>),
173171
self.data.take(),
174172
)
175173
}

0 commit comments

Comments
 (0)