Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed cloudevents::event::SPEC_VERSION_ATTRIBUTES #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ exclude = [
".github/*"
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "cloudevents"

[dependencies]
serde = { version = "^1.0", features = ["derive"] }
Expand All @@ -23,7 +24,6 @@ delegate = "^0.4"
base64 = "^0.12"
url = { version = "^2.1", features = ["serde"] }
snafu = "^0.6"
lazy_static = "1.4.0"

[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
hostname = "^0.3"
Expand All @@ -37,9 +37,6 @@ uuid = { version = "^0.8", features = ["v4", "wasm-bindgen"] }
rstest = "0.6"
claim = "0.3.1"

[lib]
name = "cloudevents"

[workspace]
members = [
".",
Expand Down
22 changes: 10 additions & 12 deletions cloudevents-sdk-actix-web/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,21 @@ macro_rules! attribute_name_to_header {
}

fn attributes_to_headers(
map: &HashMap<SpecVersion, &'static [&'static str]>,
it: impl Iterator<Item = &'static str>,
) -> HashMap<&'static str, HeaderName> {
map.values()
.flat_map(|s| s.iter())
.map(|s| {
if *s == "datacontenttype" {
(*s, header::CONTENT_TYPE)
} else {
(*s, attribute_name_to_header!(s).unwrap())
}
})
.collect()
it.map(|s| {
if s == "datacontenttype" {
(s, header::CONTENT_TYPE)
} else {
(s, attribute_name_to_header!(s).unwrap())
}
})
.collect()
}

lazy_static! {
pub(crate) static ref ATTRIBUTES_TO_HEADERS: HashMap<&'static str, HeaderName> =
attributes_to_headers(&cloudevents::event::SPEC_VERSION_ATTRIBUTES);
attributes_to_headers(SpecVersion::all_attribute_names());
pub(crate) static ref SPEC_VERSION_HEADER: HeaderName =
HeaderName::from_static("ce-specversion");
pub(crate) static ref CLOUDEVENTS_JSON_HEADER: HeaderValue =
Expand Down
4 changes: 1 addition & 3 deletions cloudevents-sdk-actix-web/src/server_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {

visitor = visitor.set_spec_version(spec_version.clone())?;

let attributes = cloudevents::event::SPEC_VERSION_ATTRIBUTES
.get(&spec_version)
.unwrap();
let attributes = spec_version.attribute_names();

for (hn, hv) in
self.req.headers().iter().filter(|(hn, _)| {
Expand Down
4 changes: 1 addition & 3 deletions cloudevents-sdk-reqwest/src/client_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ impl BinaryDeserializer for ResponseDeserializer {

visitor = visitor.set_spec_version(spec_version.clone())?;

let attributes = cloudevents::event::SPEC_VERSION_ATTRIBUTES
.get(&spec_version)
.unwrap();
let attributes = spec_version.attribute_names();

for (hn, hv) in self
.headers
Expand Down
22 changes: 10 additions & 12 deletions cloudevents-sdk-reqwest/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,21 @@ macro_rules! attribute_name_to_header {
}

fn attributes_to_headers(
map: &HashMap<SpecVersion, &'static [&'static str]>,
it: impl Iterator<Item = &'static str>,
) -> HashMap<&'static str, HeaderName> {
map.values()
.flat_map(|s| s.iter())
.map(|s| {
if *s == "datacontenttype" {
(*s, reqwest::header::CONTENT_TYPE)
} else {
(*s, attribute_name_to_header!(s).unwrap())
}
})
.collect()
it.map(|s| {
if s == "datacontenttype" {
(s, reqwest::header::CONTENT_TYPE)
} else {
(s, attribute_name_to_header!(s).unwrap())
}
})
.collect()
}

lazy_static! {
pub(crate) static ref ATTRIBUTES_TO_HEADERS: HashMap<&'static str, HeaderName> =
attributes_to_headers(&cloudevents::event::SPEC_VERSION_ATTRIBUTES);
attributes_to_headers(SpecVersion::all_attribute_names());
pub(crate) static ref SPEC_VERSION_HEADER: HeaderName =
HeaderName::from_static("ce-specversion");
pub(crate) static ref CLOUDEVENTS_JSON_HEADER: HeaderValue =
Expand Down
1 change: 0 additions & 1 deletion src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub use event::Event;
pub use extensions::ExtensionValue;
pub use spec_version::InvalidSpecVersion;
pub use spec_version::SpecVersion;
pub use spec_version::ATTRIBUTE_NAMES as SPEC_VERSION_ATTRIBUTES;

mod v03;

Expand Down
28 changes: 16 additions & 12 deletions src/event/spec_version.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
use super::{v03, v10};
use lazy_static::lazy_static;
use serde::export::Formatter;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt;

lazy_static! {
/// Lazily initialized map that contains all the context attribute names per [`SpecVersion`]
pub static ref ATTRIBUTE_NAMES: HashMap<SpecVersion, &'static [&'static str]> = {
let mut m = HashMap::new();
m.insert(SpecVersion::V03, &v03::ATTRIBUTE_NAMES[..]);
m.insert(SpecVersion::V10, &v10::ATTRIBUTE_NAMES[..]);
m
};
}

pub(crate) const SPEC_VERSIONS: [&'static str; 2] = ["0.3", "1.0"];

/// CloudEvent specification version
Expand All @@ -31,6 +19,22 @@ impl SpecVersion {
SpecVersion::V10 => "1.0",
}
}

/// Get all attribute names for this [`SpecVersion`].
#[inline]
pub fn attribute_names(&self) -> &'static [&'static str] {
match self {
SpecVersion::V03 => &v03::ATTRIBUTE_NAMES,
SpecVersion::V10 => &v10::ATTRIBUTE_NAMES,
}
}
/// Get all attribute names for all Spec versions.
/// Note that the result iterator could contain duplicate entries.
pub fn all_attribute_names() -> impl Iterator<Item = &'static str> {
vec![SpecVersion::V03, SpecVersion::V10]
.into_iter()
.flat_map(|s| s.attribute_names().to_owned().into_iter())
}
}

impl fmt::Display for SpecVersion {
Expand Down