Skip to content

Commit 2b91ee8

Browse files
Removed cloudevents::event::SPEC_VERSION_ATTRIBUTES (cloudevents#52)
* Removed cloudevents::event::SPEC_VERSION_ATTRIBUTES once and for all Signed-off-by: Francesco Guardiani <[email protected]> * cargo fmt Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 8079a14 commit 2b91ee8

File tree

7 files changed

+40
-48
lines changed

7 files changed

+40
-48
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ exclude = [
1212
".github/*"
1313
]
1414

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

1718
[dependencies]
1819
serde = { version = "^1.0", features = ["derive"] }
@@ -23,7 +24,6 @@ delegate = "^0.4"
2324
base64 = "^0.12"
2425
url = { version = "^2.1", features = ["serde"] }
2526
snafu = "^0.6"
26-
lazy_static = "1.4.0"
2727

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

40-
[lib]
41-
name = "cloudevents"
42-
4340
[workspace]
4441
members = [
4542
".",

cloudevents-sdk-actix-web/src/headers.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,21 @@ macro_rules! attribute_name_to_header {
4646
}
4747

4848
fn attributes_to_headers(
49-
map: &HashMap<SpecVersion, &'static [&'static str]>,
49+
it: impl Iterator<Item = &'static str>,
5050
) -> HashMap<&'static str, HeaderName> {
51-
map.values()
52-
.flat_map(|s| s.iter())
53-
.map(|s| {
54-
if *s == "datacontenttype" {
55-
(*s, header::CONTENT_TYPE)
56-
} else {
57-
(*s, attribute_name_to_header!(s).unwrap())
58-
}
59-
})
60-
.collect()
51+
it.map(|s| {
52+
if s == "datacontenttype" {
53+
(s, header::CONTENT_TYPE)
54+
} else {
55+
(s, attribute_name_to_header!(s).unwrap())
56+
}
57+
})
58+
.collect()
6159
}
6260

6361
lazy_static! {
6462
pub(crate) static ref ATTRIBUTES_TO_HEADERS: HashMap<&'static str, HeaderName> =
65-
attributes_to_headers(&cloudevents::event::SPEC_VERSION_ATTRIBUTES);
63+
attributes_to_headers(SpecVersion::all_attribute_names());
6664
pub(crate) static ref SPEC_VERSION_HEADER: HeaderName =
6765
HeaderName::from_static("ce-specversion");
6866
pub(crate) static ref CLOUDEVENTS_JSON_HEADER: HeaderValue =

cloudevents-sdk-actix-web/src/server_request.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
3535

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

38-
let attributes = cloudevents::event::SPEC_VERSION_ATTRIBUTES
39-
.get(&spec_version)
40-
.unwrap();
38+
let attributes = spec_version.attribute_names();
4139

4240
for (hn, hv) in
4341
self.req.headers().iter().filter(|(hn, _)| {

cloudevents-sdk-reqwest/src/client_response.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ impl BinaryDeserializer for ResponseDeserializer {
3434

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

37-
let attributes = cloudevents::event::SPEC_VERSION_ATTRIBUTES
38-
.get(&spec_version)
39-
.unwrap();
37+
let attributes = spec_version.attribute_names();
4038

4139
for (hn, hv) in self
4240
.headers

cloudevents-sdk-reqwest/src/headers.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,21 @@ macro_rules! attribute_name_to_header {
3939
}
4040

4141
fn attributes_to_headers(
42-
map: &HashMap<SpecVersion, &'static [&'static str]>,
42+
it: impl Iterator<Item = &'static str>,
4343
) -> HashMap<&'static str, HeaderName> {
44-
map.values()
45-
.flat_map(|s| s.iter())
46-
.map(|s| {
47-
if *s == "datacontenttype" {
48-
(*s, reqwest::header::CONTENT_TYPE)
49-
} else {
50-
(*s, attribute_name_to_header!(s).unwrap())
51-
}
52-
})
53-
.collect()
44+
it.map(|s| {
45+
if s == "datacontenttype" {
46+
(s, reqwest::header::CONTENT_TYPE)
47+
} else {
48+
(s, attribute_name_to_header!(s).unwrap())
49+
}
50+
})
51+
.collect()
5452
}
5553

5654
lazy_static! {
5755
pub(crate) static ref ATTRIBUTES_TO_HEADERS: HashMap<&'static str, HeaderName> =
58-
attributes_to_headers(&cloudevents::event::SPEC_VERSION_ATTRIBUTES);
56+
attributes_to_headers(SpecVersion::all_attribute_names());
5957
pub(crate) static ref SPEC_VERSION_HEADER: HeaderName =
6058
HeaderName::from_static("ce-specversion");
6159
pub(crate) static ref CLOUDEVENTS_JSON_HEADER: HeaderValue =

src/event/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub use event::Event;
1616
pub use extensions::ExtensionValue;
1717
pub use spec_version::InvalidSpecVersion;
1818
pub use spec_version::SpecVersion;
19-
pub use spec_version::ATTRIBUTE_NAMES as SPEC_VERSION_ATTRIBUTES;
2019

2120
mod v03;
2221

src/event/spec_version.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
use super::{v03, v10};
2-
use lazy_static::lazy_static;
32
use serde::export::Formatter;
4-
use std::collections::HashMap;
53
use std::convert::TryFrom;
64
use std::fmt;
75

8-
lazy_static! {
9-
/// Lazily initialized map that contains all the context attribute names per [`SpecVersion`]
10-
pub static ref ATTRIBUTE_NAMES: HashMap<SpecVersion, &'static [&'static str]> = {
11-
let mut m = HashMap::new();
12-
m.insert(SpecVersion::V03, &v03::ATTRIBUTE_NAMES[..]);
13-
m.insert(SpecVersion::V10, &v10::ATTRIBUTE_NAMES[..]);
14-
m
15-
};
16-
}
17-
186
pub(crate) const SPEC_VERSIONS: [&'static str; 2] = ["0.3", "1.0"];
197

208
/// CloudEvent specification version
@@ -31,6 +19,22 @@ impl SpecVersion {
3119
SpecVersion::V10 => "1.0",
3220
}
3321
}
22+
23+
/// Get all attribute names for this [`SpecVersion`].
24+
#[inline]
25+
pub fn attribute_names(&self) -> &'static [&'static str] {
26+
match self {
27+
SpecVersion::V03 => &v03::ATTRIBUTE_NAMES,
28+
SpecVersion::V10 => &v10::ATTRIBUTE_NAMES,
29+
}
30+
}
31+
/// Get all attribute names for all Spec versions.
32+
/// Note that the result iterator could contain duplicate entries.
33+
pub fn all_attribute_names() -> impl Iterator<Item = &'static str> {
34+
vec![SpecVersion::V03, SpecVersion::V10]
35+
.into_iter()
36+
.flat_map(|s| s.attribute_names().to_owned().into_iter())
37+
}
3438
}
3539

3640
impl fmt::Display for SpecVersion {

0 commit comments

Comments
 (0)