use crate::event::{AttributesReader, AttributesWriter, ExtensionValue, SpecVersion};
use chrono::{DateTime, FixedOffset};
use std::collections::HashMap;
use crate::event::attributes::DataAttributesWriter;
use uuid::Uuid;
use hostname::get_hostname;
pub struct Attributes {
id: String,
ty: String,
source: String,
datacontenttype: Option<String>,
dataschema: Option<String>,
subject: Option<String>,
time: Option<DateTime<FixedOffset>>,
extensions: HashMap<String, ExtensionValue>,
}
impl AttributesReader for Attributes {
fn get_id(&self) -> &str {
&self.id
}
fn get_source(&self) -> &str {
&self.source
}
fn get_specversion(&self) -> SpecVersion {
SpecVersion::V10
}
fn get_type(&self) -> &str {
&self.ty
}
fn get_datacontenttype(&self) -> Option<&str> {
match self.datacontenttype.as_ref() {
Some(s) => Some(&s),
None => None,
}
}
fn get_dataschema(&self) -> Option<&str> {
match self.dataschema.as_ref() {
Some(s) => Some(&s),
None => None,
}
}
fn get_subject(&self) -> Option<&str> {
match self.subject.as_ref() {
Some(s) => Some(&s),
None => None,
}
}
fn get_time(&self) -> Option<&DateTime<FixedOffset>> {
self.time.as_ref()
}
fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue> {
self.extensions.get(extension_name)
}
fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
self.extensions
.iter()
.map(|(k, v)| (k.as_str(), v))
.collect()
}
}
impl AttributesWriter for Attributes {
fn set_id(&mut self, id: impl Into<String>) {
self.id = id.into()
}
fn set_source(&mut self, source: impl Into<String>) {
self.source = source.into()
}
fn set_type(&mut self, ty: impl Into<String>) {
self.ty = ty.into()
}
fn set_subject(&mut self, subject: Option<impl Into<String>>) {
self.subject = subject.map(Into::into)
}
fn set_time(&mut self, time: Option<impl Into<DateTime<FixedOffset>>>) {
self.time = time.map(Into::into)
}
fn set_extension<'name, 'event: 'name>(
&'event mut self,
extension_name: &'name str,
extension_value: impl Into<ExtensionValue>,
) {
self.extensions
.insert(extension_name.to_owned(), extension_value.into());
}
fn remove_extension<'name, 'event: 'name>(
&'event mut self,
extension_name: &'name str,
) -> Option<ExtensionValue> {
self.extensions.remove(extension_name)
}
}
impl DataAttributesWriter for Attributes {
fn set_datacontenttype(
&mut self,
datacontenttype: Option<impl Into<String>>,
) {
self.datacontenttype = datacontenttype.map(Into::into)
}
fn set_dataschema(&mut self, dataschema: Option<impl Into<String>>) {
self.dataschema = dataschema.map(Into::into)
}
}
impl Default for Attributes {
fn default() -> Self {
Attributes {
id: Uuid::new_v4().to_string(),
ty: "type".to_string(),
source: get_hostname().unwrap_or("http://localhost/".to_string()),
datacontenttype: None,
dataschema: None,
subject: None,
time: None,
extensions: HashMap::new()
}
}
}