Skip to content

Commit cd8cdda

Browse files
Docs in cloudevents-sdk crate
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent d1281e7 commit cd8cdda

File tree

13 files changed

+62
-74
lines changed

13 files changed

+62
-74
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository = "https://github.com/cloudevents/sdk-rust"
1111
exclude = [
1212
".github/*"
1313
]
14+
categories = ["web-programming", "encoding", "data-structures"]
1415

1516
[lib]
1617
name = "cloudevents"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ let event = EventBuilderV10::new()
4343
.id("aaa")
4444
.source(Url::parse("http://localhost").unwrap())
4545
.ty("example.demo")
46-
.build()
47-
.unwrap();
46+
.build()?;
4847
```
4948

5049
Checkout the examples using our integrations to learn how to send and receive events:

src/event/attributes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use serde::Serializer;
77
use std::fmt;
88
use url::Url;
99

10-
/// Value of a CloudEvent attribute
10+
/// Enum representing a borrowed value of a CloudEvent attribute.
11+
/// This represents the types defined in the [CloudEvent spec type system](https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system)
1112
#[derive(Debug, PartialEq, Eq)]
1213
pub enum AttributeValue<'a> {
1314
SpecVersion(SpecVersion),
@@ -230,20 +231,20 @@ impl AttributesWriter for Attributes {
230231
}
231232

232233
impl Attributes {
233-
pub fn into_v10(self) -> Self {
234+
pub(crate) fn into_v10(self) -> Self {
234235
match self {
235236
Attributes::V03(v03) => Attributes::V10(v03.into_v10()),
236237
_ => self,
237238
}
238239
}
239-
pub fn into_v03(self) -> Self {
240+
pub(crate) fn into_v03(self) -> Self {
240241
match self {
241242
Attributes::V10(v10) => Attributes::V03(v10.into_v03()),
242243
_ => self,
243244
}
244245
}
245246

246-
pub fn iter(&self) -> impl Iterator<Item = (&str, AttributeValue)> {
247+
pub(crate) fn iter(&self) -> impl Iterator<Item = (&str, AttributeValue)> {
247248
match self {
248249
Attributes::V03(a) => AttributesIter::IterV03(a.into_iter()),
249250
Attributes::V10(a) => AttributesIter::IterV10(a.into_iter()),

src/event/data.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::export::Formatter;
22
use serde_json::Value;
3-
use std::convert::{Into, TryFrom};
3+
use std::convert::TryFrom;
44
use std::fmt;
55

66
/// Event [data attribute](https://github.com/cloudevents/spec/blob/master/spec.md#event-data) representation
@@ -14,40 +14,6 @@ pub enum Data {
1414
Json(serde_json::Value),
1515
}
1616

17-
impl Data {
18-
/// Create a [`Data`] from a [`Into<Vec<u8>>`].
19-
///
20-
/// # Example
21-
///
22-
/// ```
23-
/// use cloudevents::event::Data;
24-
///
25-
/// let value = Data::from_base64(b"dmFsdWU=").unwrap();
26-
/// assert_eq!(value, Data::Binary(base64::decode("dmFsdWU=").unwrap()));
27-
/// ```
28-
///
29-
/// [`AsRef<[u8]>`]: https://doc.rust-lang.org/std/convert/trait.AsRef.html
30-
/// [`Data`]: enum.Data.html
31-
pub fn from_base64<I>(i: I) -> Result<Self, base64::DecodeError>
32-
where
33-
I: AsRef<[u8]>,
34-
{
35-
Ok(base64::decode(&i)?.into())
36-
}
37-
38-
pub fn from_binary<I>(content_type: Option<&str>, i: I) -> Result<Self, serde_json::Error>
39-
where
40-
I: AsRef<[u8]>,
41-
{
42-
let is_json = is_json_content_type(content_type.unwrap_or("application/json"));
43-
if is_json {
44-
serde_json::from_slice::<serde_json::Value>(i.as_ref()).map(Data::Json)
45-
} else {
46-
Ok(Data::Binary(i.as_ref().to_vec()))
47-
}
48-
}
49-
}
50-
5117
pub(crate) fn is_json_content_type(ct: &str) -> bool {
5218
ct.starts_with("application/json") || ct.starts_with("text/json") || ct.ends_with("+json")
5319
}

src/event/extensions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::fmt;
66
#[serde(untagged)]
77
/// Represents all the possible [CloudEvents extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) values
88
pub enum ExtensionValue {
9-
/// Represents a [`String`](std::string::String) value.
9+
/// Represents a [`String`] value.
1010
String(String),
11-
/// Represents a [`bool`](bool) value.
11+
/// Represents a [`bool`] value.
1212
Boolean(bool),
13-
/// Represents an integer [`i64`](i64) value.
13+
/// Represents an integer [`i64`] value.
1414
Integer(i64),
1515
}
1616

src/event/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Provides [`Event`] data structure, [`EventBuilder`] and other facilities to work with [`Event`].
2+
13
mod attributes;
24
mod builder;
35
mod data;

src/event/spec_version.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ use std::fmt;
55

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

8-
/// CloudEvent specification version
8+
/// CloudEvent specification version.
99
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
1010
pub enum SpecVersion {
11+
/// CloudEvents v0.3
1112
V03,
13+
/// CloudEvents v1.0
1214
V10,
1315
}
1416

1517
impl SpecVersion {
18+
/// Returns the string representation of [`SpecVersion`].
19+
#[inline]
1620
pub fn as_str(&self) -> &str {
1721
match self {
1822
SpecVersion::V03 => "0.3",
@@ -28,7 +32,7 @@ impl SpecVersion {
2832
SpecVersion::V10 => &v10::ATTRIBUTE_NAMES,
2933
}
3034
}
31-
/// Get all attribute names for all Spec versions.
35+
/// Get all attribute names for all specification versions.
3236
/// Note that the result iterator could contain duplicate entries.
3337
pub fn all_attribute_names() -> impl Iterator<Item = &'static str> {
3438
vec![SpecVersion::V03, SpecVersion::V10]

src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
//! This crate implements the [CloudEvents](https://cloudevents.io/) Spec for Rust.
22
//!
33
//! ```
4+
//! # use std::error::Error;
5+
//! # fn main() -> Result<(), Box<dyn Error>> {
46
//! use cloudevents::{EventBuilder, AttributesReader, EventBuilderV10};
5-
//! use chrono::Utc;
7+
//! use chrono::{Utc, DateTime};
68
//! use url::Url;
79
//!
810
//! let event = EventBuilderV10::new()
911
//! .id("my_event.my_application")
1012
//! .source("http://localhost:8080")
1113
//! .ty("example.demo")
1214
//! .time(Utc::now())
13-
//! .build()
14-
//! .unwrap();
15+
//! .build()?;
1516
//!
1617
//! println!("CloudEvent Id: {}", event.id());
17-
//! println!("CloudEvent Time: {}", event.time().unwrap());
18+
//! match event.time() {
19+
//! Some(t) => println!("CloudEvent Time: {}", t),
20+
//! None => println!("CloudEvent Time: None")
21+
//! }
22+
//! # Ok(())
23+
//! # }
1824
//! ```
1925
//!
26+
//! This crate includes:
27+
//!
28+
//! * The [`Event`] data structure, to represent CloudEvent (version 1.0 and 0.3)
29+
//! * The [`EventBuilder`] trait and implementations, to create [`Event`] instances
30+
//! * The implementation of [`serde::Serialize`] and [`serde::Deserialize`] for [`Event`] to serialize/deserialize CloudEvents to/from JSON
31+
//! * Traits and utilities in [`message`] to implement Protocol Bindings
32+
//!
2033
//! If you're looking for Protocol Binding implementations, look at crates:
2134
//!
2235
//! * [cloudevents-sdk-actix-web](https://docs.rs/cloudevents-sdk-actix-web): Integration with [Actix Web](https://github.com/actix/actix-web)
2336
//! * [cloudevents-sdk-reqwest](https://docs.rs/cloudevents-sdk-reqwest): Integration with [reqwest](https://github.com/seanmonstar/reqwest)
37+
//! * [cloudevents-sdk-rdkafka](https://docs.rs/cloudevents-sdk-rdkafka): Integration with [rdkafka](https://fede1024.github.io/rust-rdkafka)
2438
//!
2539
26-
extern crate serde;
27-
extern crate serde_json;
28-
extern crate serde_value;
29-
extern crate snafu;
40+
#![deny(broken_intra_doc_links)]
3041

31-
/// Provides [`Event`] data structure, [`EventBuilder`] and other facilities to work with [`Event`]
3242
pub mod event;
33-
/// Provides facilities to implement Protocol Bindings
3443
pub mod message;
3544

3645
pub use event::Data;

src/message/deserializer.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@ use super::{BinarySerializer, Encoding, Error, Result, StructuredSerializer};
22
use crate::event::{EventBinarySerializer, EventStructuredSerializer};
33
use crate::Event;
44

5-
/// Deserializer trait for a Message that can be encoded as structured mode
5+
/// Deserializer trait for a Message that can be encoded as structured mode.
66
pub trait StructuredDeserializer
77
where
88
Self: Sized,
99
{
10-
/// Deserialize the message to [`StructuredSerializer`]
10+
/// Deserialize the message to [`StructuredSerializer`].
1111
fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
1212
self,
1313
serializer: V,
1414
) -> Result<R>;
1515

16-
/// Convert this Message to [`Event`]
16+
/// Convert this Message to [`Event`].
1717
fn into_event(self) -> Result<Event> {
1818
self.deserialize_structured(EventStructuredSerializer {})
1919
}
2020
}
2121

22-
/// Deserializer trait for a Message that can be encoded as binary mode
22+
/// Deserializer trait for a Message that can be encoded as binary mode.
2323
pub trait BinaryDeserializer
2424
where
2525
Self: Sized,
2626
{
27-
/// Deserialize the message to [`BinarySerializer`]
27+
/// Deserialize the message to [`BinarySerializer`].
2828
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(self, serializer: V) -> Result<R>;
2929

30-
/// Convert this Message to [`Event`]
30+
/// Convert this Message to [`Event`].
3131
fn into_event(self) -> Result<Event> {
3232
self.deserialize_binary(EventBinarySerializer::new())
3333
}
3434
}
3535

36-
/// Deserializer trait for a Message that can be encoded both in structured mode or binary mode
36+
/// Deserializer trait for a Message that can be encoded both in structured mode or binary mode.
3737
pub trait MessageDeserializer
3838
where
3939
Self: StructuredDeserializer + BinaryDeserializer + Sized,
4040
{
41-
/// Get this message [`Encoding`]
41+
/// Get this message [`Encoding`].
4242
fn encoding(&self) -> Encoding;
4343

44-
/// Convert this Message to [`Event`]
44+
/// Convert this Message to [`Event`].
4545
fn into_event(self) -> Result<Event> {
4646
match self.encoding() {
4747
Encoding::BINARY => BinaryDeserializer::into_event(self),
@@ -50,7 +50,7 @@ where
5050
}
5151
}
5252

53-
/// Deserialize the message to [`BinarySerializer`]
53+
/// Deserialize the message to [`BinarySerializer`].
5454
fn deserialize_to_binary<R: Sized, T: BinarySerializer<R>>(self, serializer: T) -> Result<R> {
5555
if self.encoding() == Encoding::BINARY {
5656
return self.deserialize_binary(serializer);
@@ -59,7 +59,7 @@ where
5959
return MessageDeserializer::into_event(self)?.deserialize_binary(serializer);
6060
}
6161

62-
/// Deserialize the message to [`StructuredSerializer`]
62+
/// Deserialize the message to [`StructuredSerializer`].
6363
fn deserialize_to_structured<R: Sized, T: StructuredSerializer<R>>(
6464
self,
6565
serializer: T,
@@ -71,8 +71,8 @@ where
7171
return MessageDeserializer::into_event(self)?.deserialize_structured(serializer);
7272
}
7373

74-
/// Deserialize the message to a serializer, depending on the message encoding
75-
/// You can use this method to transcode this message directly to another serializer, without going through [`Event`]
74+
/// Deserialize the message to a serializer, depending on the message encoding.
75+
/// You can use this method to transcode this message directly to another serializer, without going through [`Event`].
7676
fn deserialize_to<R: Sized, T: BinarySerializer<R> + StructuredSerializer<R>>(
7777
self,
7878
serializer: T,

src/message/encoding.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::fmt::Debug;
22

3-
/// Represents one of the possible [message encodings/modes](https://github.com/cloudevents/spec/blob/v1.0/spec.md#message)
3+
/// Represents one of the possible [message encodings/modes](https://github.com/cloudevents/spec/blob/v1.0/spec.md#message).
44
#[derive(PartialEq, Eq, Debug)]
55
pub enum Encoding {
6+
/// Represents the _structured-mode message_.
67
STRUCTURED,
8+
/// Represents the _binary-mode message_.
79
BINARY,
10+
/// Represents a non-CloudEvent or a malformed CloudEvent that cannot be recognized by the SDK.
811
UNKNOWN,
912
}

0 commit comments

Comments
 (0)