Refactor header into newtype

pull/200/head
Chip Senkbeil 12 months ago
parent 4514e49122
commit b97f80997f
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -1,37 +1,16 @@
/// Represents a generic id type
pub type Id = String;
/// Represents a packet header for a request or response
pub type Header = std::collections::HashMap<String, crate::common::Value>;
/// Generates a new [`Header`] of key/value pairs based on literals.
///
/// ```
/// use distant_net::header;
///
/// let _header = header!("key" -> "value", "key2" -> 123);
/// ```
#[macro_export]
macro_rules! header {
($($key:literal -> $value:expr),* $(,)?) => {{
let mut _header = ::std::collections::HashMap::new();
$(
_header.insert($key.to_string(), $crate::common::Value::from($value));
)*
_header
}};
}
mod header;
mod request;
mod response;
pub use header::*;
pub use request::*;
pub use response::*;
use std::io::Cursor;
/// Represents a generic id type
pub type Id = String;
/// Reads the header bytes from msgpack input, including the marker and len bytes.
///
/// * If succeeds, returns (header, remaining).

@ -0,0 +1,57 @@
use crate::common::Value;
use derive_more::IntoIterator;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
/// Generates a new [`Header`] of key/value pairs based on literals.
///
/// ```
/// use distant_net::header;
///
/// let _header = header!("key" -> "value", "key2" -> 123);
/// ```
#[macro_export]
macro_rules! header {
($($key:literal -> $value:expr),* $(,)?) => {{
let mut _header = ::std::collections::HashMap::new();
$(
_header.insert($key.to_string(), $crate::common::Value::from($value));
)*
$crate::common::Header::new(_header)
}};
}
/// Represents a packet header for a request or response
#[derive(Clone, Debug, Default, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Header(HashMap<String, Value>);
impl Header {
/// Creates a new [`Header`] newtype wrapper.
pub fn new(map: HashMap<String, Value>) -> Self {
Self(map)
}
/// Exists purely to support serde serialization checks.
#[inline]
pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Deref for Header {
type Target = HashMap<String, Value>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Header {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use super::{read_header_bytes, read_key_eq, read_str_bytes, Header, Id};
use crate::common::utils;
use crate::header;
/// Represents a request to send
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use super::{read_header_bytes, read_key_eq, read_str_bytes, Header, Id};
use crate::common::utils;
use crate::header;
/// Represents a response received related to some response
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]

Loading…
Cancel
Save