Clean up header

pull/200/head
Chip Senkbeil 1 year ago
parent b97f80997f
commit 5a4ca1887f
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -14,25 +14,25 @@ use std::ops::{Deref, DerefMut};
#[macro_export] #[macro_export]
macro_rules! header { macro_rules! header {
($($key:literal -> $value:expr),* $(,)?) => {{ ($($key:literal -> $value:expr),* $(,)?) => {{
let mut _header = ::std::collections::HashMap::new(); let mut _header = $crate::common::Header::default();
$( $(
_header.insert($key.to_string(), $crate::common::Value::from($value)); _header.insert($key, $value);
)* )*
$crate::common::Header::new(_header) _header
}}; }};
} }
/// Represents a packet header for a request or response /// Represents a packet header comprised of arbitrary data tied to string keys.
#[derive(Clone, Debug, Default, PartialEq, Eq, IntoIterator, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct Header(HashMap<String, Value>); pub struct Header(HashMap<String, Value>);
impl Header { impl Header {
/// Creates a new [`Header`] newtype wrapper. /// Creates an empty [`Header`] newtype wrapper.
pub fn new(map: HashMap<String, Value>) -> Self { pub fn new() -> Self {
Self(map) Self::default()
} }
/// Exists purely to support serde serialization checks. /// Exists purely to support serde serialization checks.
@ -40,6 +40,18 @@ impl Header {
pub(crate) fn is_empty(&self) -> bool { pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty() self.0.is_empty()
} }
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, [`None`] is returned.
///
/// If the map did have this key present, the value is updated, and the old value is returned.
/// The key is not updated, though; this matters for types that can be `==` without being
/// identical. See the [module-level documentation](std::collections#insert-and-complex-keys)
/// for more.
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<Value>) -> Option<Value> {
self.0.insert(key.into(), value.into())
}
} }
impl Deref for Header { impl Deref for Header {

Loading…
Cancel
Save