|
| 1 | +use super::SpecVersion; |
| 2 | +use crate::event::{AttributesV10, ExtensionValue}; |
| 3 | +use chrono::{DateTime, FixedOffset}; |
| 4 | + |
| 5 | +pub trait AttributesReader { |
| 6 | + fn get_id(&self) -> &str; |
| 7 | + fn get_source(&self) -> &str; |
| 8 | + fn get_specversion(&self) -> SpecVersion; |
| 9 | + fn get_type(&self) -> &str; |
| 10 | + fn get_datacontenttype(&self) -> Option<&str>; |
| 11 | + fn get_dataschema(&self) -> Option<&str>; |
| 12 | + fn get_subject(&self) -> Option<&str>; |
| 13 | + fn get_time(&self) -> Option<&DateTime<FixedOffset>>; |
| 14 | + fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>; |
| 15 | + fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>; |
| 16 | +} |
| 17 | + |
| 18 | +pub trait AttributesWriter { |
| 19 | + fn set_id<'event>(&'event mut self, id: impl Into<&'event str>); |
| 20 | + fn set_source<'event>(&'event mut self, source: impl Into<&'event str>); |
| 21 | + fn set_type<'event>(&'event mut self, ty: impl Into<&'event str>); |
| 22 | + fn set_datacontenttype<'event>( |
| 23 | + &'event mut self, |
| 24 | + datacontenttype: Option<impl Into<&'event str>>, |
| 25 | + ); |
| 26 | + fn set_dataschema<'event>(&'event mut self, dataschema: Option<impl Into<&'event str>>); |
| 27 | + fn set_subject<'event>(&'event mut self, subject: Option<impl Into<&'event str>>); |
| 28 | + fn set_time<'event>(&'event mut self, time: Option<impl Into<DateTime<FixedOffset>>>); |
| 29 | + fn set_extension<'event>( |
| 30 | + &'event mut self, |
| 31 | + extension_name: &'event str, |
| 32 | + extension_value: impl Into<ExtensionValue>, |
| 33 | + ); |
| 34 | + fn remove_extension<'event>( |
| 35 | + &'event mut self, |
| 36 | + extension_name: &'event str, |
| 37 | + ) -> Option<ExtensionValue>; |
| 38 | +} |
| 39 | + |
| 40 | +pub enum Attributes { |
| 41 | + V10(AttributesV10), |
| 42 | +} |
| 43 | + |
| 44 | +impl AttributesReader for Attributes { |
| 45 | + fn get_id(&self) -> &str { |
| 46 | + match self { |
| 47 | + Attributes::V10(a) => a.get_id(), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn get_type(&self) -> &str { |
| 52 | + match self { |
| 53 | + Attributes::V10(a) => a.get_type(), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn get_source(&self) -> &str { |
| 58 | + match self { |
| 59 | + Attributes::V10(a) => a.get_source(), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn get_specversion(&self) -> SpecVersion { |
| 64 | + match self { |
| 65 | + Attributes::V10(a) => a.get_specversion(), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + fn get_datacontenttype(&self) -> Option<&str> { |
| 70 | + match self { |
| 71 | + Attributes::V10(a) => a.get_datacontenttype(), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fn get_dataschema(&self) -> Option<&str> { |
| 76 | + match self { |
| 77 | + Attributes::V10(a) => a.get_dataschema(), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fn get_subject(&self) -> Option<&str> { |
| 82 | + match self { |
| 83 | + Attributes::V10(a) => a.get_subject(), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fn get_time(&self) -> Option<&DateTime<FixedOffset>> { |
| 88 | + match self { |
| 89 | + Attributes::V10(a) => a.get_time(), |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue> { |
| 94 | + match self { |
| 95 | + Attributes::V10(a) => a.get_extension(extension_name), |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> { |
| 100 | + match self { |
| 101 | + Attributes::V10(a) => a.get_extensions(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl AttributesWriter for Attributes { |
| 107 | + fn set_id<'event>(&'event mut self, id: impl Into<&'event str>) { |
| 108 | + match self { |
| 109 | + Attributes::V10(a) => a.set_id(id), |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn set_type<'event>(&'event mut self, ty: impl Into<&'event str>) { |
| 114 | + match self { |
| 115 | + Attributes::V10(a) => a.set_type(ty), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn set_source<'event>(&'event mut self, source: impl Into<&'event str>) { |
| 120 | + match self { |
| 121 | + Attributes::V10(a) => a.set_source(source), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + fn set_datacontenttype<'event>( |
| 126 | + &'event mut self, |
| 127 | + datacontenttype: Option<impl Into<&'event str>>, |
| 128 | + ) { |
| 129 | + match self { |
| 130 | + Attributes::V10(a) => a.set_datacontenttype(datacontenttype), |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + fn set_extension<'event>( |
| 135 | + &'event mut self, |
| 136 | + extension_name: &'event str, |
| 137 | + extension_value: impl Into<ExtensionValue>, |
| 138 | + ) { |
| 139 | + match self { |
| 140 | + Attributes::V10(a) => a.set_extension(extension_name, extension_value), |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + fn remove_extension<'event>( |
| 145 | + &'event mut self, |
| 146 | + extension_name: &'event str, |
| 147 | + ) -> Option<ExtensionValue> { |
| 148 | + match self { |
| 149 | + Attributes::V10(a) => a.remove_extension(extension_name), |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + fn set_dataschema<'event>(&'event mut self, dataschema: Option<impl Into<&'event str>>) { |
| 154 | + match self { |
| 155 | + Attributes::V10(a) => a.set_dataschema(dataschema), |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + fn set_subject<'event>(&'event mut self, subject: Option<impl Into<&'event str>>) { |
| 160 | + match self { |
| 161 | + Attributes::V10(a) => a.set_subject(subject), |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + fn set_time<'event>(&'event mut self, time: Option<impl Into<DateTime<FixedOffset>>>) { |
| 166 | + match self { |
| 167 | + Attributes::V10(a) => a.set_time(time), |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments