pub struct Event { /* private fields */ }
Expand description
Data structure that represents a CloudEvent.
It provides methods to get the attributes through AttributesReader
and write them through AttributesWriter
.
It also provides methods to read and write the event data.
You can build events using super::EventBuilder
use cloudevents::*;
use std::convert::TryInto;
// Create an event using the Default trait
let mut e = Event::default();
e.set_data(
"application/json",
serde_json::json!({"hello": "world"})
);
// Print the event id
println!("Event id: {}", e.id());
// Get the event data
let data: Option<Data> = e.data().cloned();
match data {
Some(d) => println!("{}", d),
None => println!("No event data")
}
Implementations§
Source§impl Event
impl Event
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, AttributeValue<'_>)>
pub fn iter(&self) -> impl Iterator<Item = (&str, AttributeValue<'_>)>
Returns an Iterator
for all the available CloudEvents Context attributes and extensions.
Same as chaining Event::iter_attributes()
and Event::iter_extensions()
Sourcepub fn iter_attributes(
&self,
) -> impl Iterator<Item = (&str, AttributeValue<'_>)>
pub fn iter_attributes( &self, ) -> impl Iterator<Item = (&str, AttributeValue<'_>)>
Returns an Iterator
for all the available CloudEvents Context attributes, excluding extensions.
This iterator does not contain the data
field.
Sourcepub fn iter_extensions(&self) -> impl Iterator<Item = (&str, &ExtensionValue)>
pub fn iter_extensions(&self) -> impl Iterator<Item = (&str, &ExtensionValue)>
Get all the extensions
Sourcepub fn take_data(&mut self) -> (Option<String>, Option<Url>, Option<Data>)
pub fn take_data(&mut self) -> (Option<String>, Option<Url>, Option<Data>)
Take (datacontenttype
, dataschema
, data
) from this event, leaving these fields empty
use cloudevents::Event;
use serde_json::json;
use std::convert::Into;
let mut e = Event::default();
e.set_data("application/json", json!({}));
let (datacontenttype, dataschema, data) = e.take_data();
Sourcepub fn set_data(
&mut self,
datacontenttype: impl Into<String>,
data: impl Into<Data>,
) -> (Option<String>, Option<Data>)
pub fn set_data( &mut self, datacontenttype: impl Into<String>, data: impl Into<Data>, ) -> (Option<String>, Option<Data>)
Set data
into this Event
with the specified datacontenttype
.
Returns the previous value of datacontenttype
and data
.
use cloudevents::Event;
use serde_json::json;
use std::convert::Into;
let mut e = Event::default();
let (old_datacontenttype, old_data) = e.set_data("application/json", json!({}));
Sourcepub fn set_data_unchecked(&mut self, data: impl Into<Data>) -> Option<Data>
pub fn set_data_unchecked(&mut self, data: impl Into<Data>) -> Option<Data>
Set data
into this Event
, without checking if there is a datacontenttype
.
Returns the previous value of data
.
use cloudevents::Event;
use serde_json::json;
use std::convert::Into;
let mut e = Event::default();
let old_data = e.set_data_unchecked(json!({}));
Sourcepub fn extension(&self, extension_name: &str) -> Option<&ExtensionValue>
pub fn extension(&self, extension_name: &str) -> Option<&ExtensionValue>
Get the extension named extension_name
Sourcepub fn set_extension<'name, 'event: 'name>(
&'event mut self,
extension_name: &'name str,
extension_value: impl Into<ExtensionValue>,
)
pub fn set_extension<'name, 'event: 'name>( &'event mut self, extension_name: &'name str, extension_value: impl Into<ExtensionValue>, )
Set the extension named extension_name
with extension_value
Sourcepub fn remove_extension<'name, 'event: 'name>(
&'event mut self,
extension_name: &'name str,
) -> Option<ExtensionValue>
pub fn remove_extension<'name, 'event: 'name>( &'event mut self, extension_name: &'name str, ) -> Option<ExtensionValue>
Remove the extension named extension_name
Trait Implementations§
Source§impl AttributesReader for Event
impl AttributesReader for Event
Source§fn source(&self) -> &UriReference
fn source(&self) -> &UriReference
Source§fn specversion(&self) -> SpecVersion
fn specversion(&self) -> SpecVersion
Source§fn datacontenttype(&self) -> Option<&str>
fn datacontenttype(&self) -> Option<&str>
Source§fn dataschema(&self) -> Option<&Url>
fn dataschema(&self) -> Option<&Url>
Source§impl AttributesWriter for Event
impl AttributesWriter for Event
Source§fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference
fn set_source(&mut self, source: impl Into<UriReference>) -> UriReference
Source§fn set_type(&mut self, ty: impl Into<String>) -> String
fn set_type(&mut self, ty: impl Into<String>) -> String
Source§fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>
Source§fn set_time(
&mut self,
time: Option<impl Into<DateTime<Utc>>>,
) -> Option<DateTime<Utc>>
fn set_time( &mut self, time: Option<impl Into<DateTime<Utc>>>, ) -> Option<DateTime<Utc>>
Source§fn set_datacontenttype(
&mut self,
datacontenttype: Option<impl Into<String>>,
) -> Option<String>
fn set_datacontenttype( &mut self, datacontenttype: Option<impl Into<String>>, ) -> Option<String>
Source§fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>
Source§impl BinaryDeserializer for Event
impl BinaryDeserializer for Event
Source§fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(
self,
visitor: V,
) -> Result<R>
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>( self, visitor: V, ) -> Result<R>
BinarySerializer
.Source§impl<'de> Deserialize<'de> for Event
impl<'de> Deserialize<'de> for Event
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Self, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Self, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl From<Event> for EventBuilder
impl From<Event> for EventBuilder
Source§impl From<Event> for EventBuilder
impl From<Event> for EventBuilder
Source§impl Serialize for Event
impl Serialize for Event
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl StructuredDeserializer for Event
impl StructuredDeserializer for Event
Source§fn deserialize_structured<R, V: StructuredSerializer<R>>(
self,
visitor: V,
) -> Result<R>
fn deserialize_structured<R, V: StructuredSerializer<R>>( self, visitor: V, ) -> Result<R>
StructuredSerializer
.