WIP iter attributes

This commit is contained in:
slinkydeveloper 2020-03-20 13:21:16 +01:00 committed by Francesco Guardiani
parent 9e9122385e
commit 98ed5cd479
5 changed files with 44 additions and 11 deletions

View File

@ -1,6 +1,42 @@
use super::SpecVersion;
use crate::event::{AttributesV10, ExtensionValue};
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).
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`
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)
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>;
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue>;
}
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 {
Attributes::V10(a) => a.get_extensions(),
Attributes::V10(a) => a.iter_extensions(),
}
}
}

View File

@ -49,7 +49,7 @@ impl AttributesReader for Event {
fn get_subject(&self) -> Option<&str>;
fn get_time(&self) -> Option<&DateTime<Utc>>;
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>;
}
}
}

View File

@ -2,7 +2,7 @@ mod attributes;
mod builder;
mod data;
mod event;
mod extensions;
mod extension_value;
mod spec_version;
pub use attributes::Attributes;
@ -10,7 +10,7 @@ pub use attributes::{AttributesReader, AttributesWriter};
pub use builder::EventBuilder;
pub use data::Data;
pub use event::Event;
pub use extensions::ExtensionValue;
pub use extension_value::ExtensionValue;
pub use spec_version::SpecVersion;
mod v10;

View File

@ -63,11 +63,8 @@ impl AttributesReader for Attributes {
self.extensions.get(extension_name)
}
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
self.extensions
.iter()
.map(|(k, v)| (k.as_str(), v))
.collect()
fn iter_extensions(&self) -> std::collections::hash_map::Iter<String, ExtensionValue> {
self.extensions.iter()
}
}