WIP iter attributes
This commit is contained in:
parent
9e9122385e
commit
98ed5cd479
|
@ -1,6 +1,42 @@
|
||||||
use super::SpecVersion;
|
use super::SpecVersion;
|
||||||
use crate::event::{AttributesV10, ExtensionValue};
|
use crate::event::{AttributesV10, ExtensionValue};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
impl ExactSizeIterator for Iter {
|
||||||
|
type Item = (&'a str, AttributeValue<'a>);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<u32> {
|
||||||
|
let new_next = self.curr + self.next;
|
||||||
|
|
||||||
|
self.curr = self.next;
|
||||||
|
self.next = new_next;
|
||||||
|
|
||||||
|
// Since there's no endpoint to a Fibonacci sequence, the `Iterator`
|
||||||
|
// will never return `None`, and `Some` is always returned.
|
||||||
|
Some(self.curr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum AttributeValue<'a> {
|
||||||
|
SpecVersion(SpecVersion),
|
||||||
|
String(&'a str),
|
||||||
|
URI(&'a str),
|
||||||
|
URIRef(&'a str),
|
||||||
|
Time(&'a DateTime<Utc>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for AttributeValue<'_> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
AttributeValue::SpecVersion(s) => s.fmt(f),
|
||||||
|
AttributeValue::String(s) => f.write_str(s),
|
||||||
|
AttributeValue::URI(s) => f.write_str(s),
|
||||||
|
AttributeValue::URIRef(s) => f.write_str(s),
|
||||||
|
AttributeValue::Time(s) => f.write_str(&s.to_rfc2822()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
|
/// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
|
||||||
pub trait AttributesReader {
|
pub trait AttributesReader {
|
||||||
|
@ -23,7 +59,7 @@ pub trait AttributesReader {
|
||||||
/// Get the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name`
|
/// Get the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name`
|
||||||
fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>;
|
fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>;
|
||||||
/// Get all the [extensions](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes)
|
/// Get all the [extensions](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes)
|
||||||
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>;
|
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AttributesWriter {
|
pub trait AttributesWriter {
|
||||||
|
@ -108,9 +144,9 @@ impl AttributesReader for Attributes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
|
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue> {
|
||||||
match self {
|
match self {
|
||||||
Attributes::V10(a) => a.get_extensions(),
|
Attributes::V10(a) => a.iter_extensions(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl AttributesReader for Event {
|
||||||
fn get_subject(&self) -> Option<&str>;
|
fn get_subject(&self) -> Option<&str>;
|
||||||
fn get_time(&self) -> Option<&DateTime<Utc>>;
|
fn get_time(&self) -> Option<&DateTime<Utc>>;
|
||||||
fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>;
|
fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>;
|
||||||
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>;
|
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ mod attributes;
|
||||||
mod builder;
|
mod builder;
|
||||||
mod data;
|
mod data;
|
||||||
mod event;
|
mod event;
|
||||||
mod extensions;
|
mod extension_value;
|
||||||
mod spec_version;
|
mod spec_version;
|
||||||
|
|
||||||
pub use attributes::Attributes;
|
pub use attributes::Attributes;
|
||||||
|
@ -10,7 +10,7 @@ pub use attributes::{AttributesReader, AttributesWriter};
|
||||||
pub use builder::EventBuilder;
|
pub use builder::EventBuilder;
|
||||||
pub use data::Data;
|
pub use data::Data;
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use extensions::ExtensionValue;
|
pub use extension_value::ExtensionValue;
|
||||||
pub use spec_version::SpecVersion;
|
pub use spec_version::SpecVersion;
|
||||||
|
|
||||||
mod v10;
|
mod v10;
|
||||||
|
|
|
@ -63,11 +63,8 @@ impl AttributesReader for Attributes {
|
||||||
self.extensions.get(extension_name)
|
self.extensions.get(extension_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
|
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue> {
|
||||||
self.extensions
|
self.extensions.iter()
|
||||||
.iter()
|
|
||||||
.map(|(k, v)| (k.as_str(), v))
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue