1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
//! Encoding / Decoding utilities.
/// Encode a serde::Serialize item as message-pack data to given writer.
/// You may wish to first wrap your writer in a BufWriter.
pub fn rmp_encode<W, S>(write: &mut W, item: S) -> Result<(), std::io::Error>
where
W: std::io::Write,
S: serde::Serialize,
{
let mut se = rmp_serde::encode::Serializer::new(write)
.with_struct_map()
.with_string_variants();
item.serialize(&mut se)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
/// Decode message-pack data from given reader into an owned item.
/// You may wish to first wrap your reader in a BufReader.
pub fn rmp_decode<R, D>(r: &mut R) -> Result<D, std::io::Error>
where
R: std::io::Read,
for<'de> D: Sized + serde::Deserialize<'de>,
{
let mut de = rmp_serde::decode::Deserializer::new(r);
D::deserialize(&mut de).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}
/// Apply to a data item to indicate it can be encoded / decoded.
pub trait Codec: Clone + Sized {
/// Variant identifier (for debugging or as a cheap discriminant).
fn variant_type(&self) -> &'static str;
/// Encode this item to given writer.
/// You may wish to first wrap your writer in a BufWriter.
fn encode<W>(&self, w: &mut W) -> Result<(), std::io::Error>
where
W: std::io::Write;
/// Encode this item to an owned vector of bytes.
/// Uses `encode()` internally.
fn encode_vec(&self) -> Result<Vec<u8>, std::io::Error> {
let mut data = Vec::new();
self.encode(&mut data)?;
Ok(data)
}
/// Decode a reader into this item.
/// You may wish to first wrap your reader in a BufReader.
fn decode<R>(r: &mut R) -> Result<Self, std::io::Error>
where
R: std::io::Read;
/// Decode a range of bytes into this item.
/// Will also return the byte count read.
/// Uses `decode()` internally.
fn decode_ref(r: &[u8]) -> Result<(u64, Self), std::io::Error> {
let mut r = std::io::Cursor::new(r);
let item = Self::decode(&mut r)?;
Ok((r.position(), item))
}
}
/// DSL-style macro for generating a serialization protocol message enum.
///
/// DSL:
///
/// ```ignore
/// /// [codec doc here]
/// codec $codec_name {
/// /// [var doc here]
/// $var_name($var_id) {
/// /// [type doc here]
/// $type_name.$type_idx: $type_ty,
/// },
/// }
/// ```
///
/// - $codec_name - camel-case codec enum name
/// - $var_name - camel-case variant/struct name
/// - $var_id - protocol variant identifier byte (u8) literal
/// - $type_name - snake-case type name
/// - $type_idx - zero-index type index in message array (usize)
/// - $type_ty - type rust type
///
/// E.G.:
///
/// ```ignore
/// /// My codec is awesome.
/// codec MyCodec {
/// /// My codec has only one variant.
/// MyVariant(0x00) {
/// /// My variant has only one type
/// my_type.0: String,
/// },
/// }
/// ```
#[macro_export]
macro_rules! write_codec_enum {
($(#[doc = $codec_doc:expr])* codec $codec_name:ident {$(
$(#[doc = $var_doc:expr])* $var_name:ident($var_id:literal) {$(
$(#[doc = $type_doc:expr])* $type_name:ident.$type_idx:literal: $type_ty:ty,
)*},
)*}) => {
$crate::dependencies::paste::item! {
$(
$(#[doc = $var_doc])*
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
// NOTE: calling crate must depend on kitsune_p2p_types with feature fuzzing
#[cfg_attr(feature = "fuzzing", derive($crate::dependencies::proptest_derive::Arbitrary))]
pub struct [< $var_name:camel >] {
$(
$(#[doc = $type_doc])* pub [< $type_name:snake >]: $type_ty,
)*
}
impl $crate::codec::Codec for [< $var_name:camel >] {
fn variant_type(&self) -> &'static str {
concat!(
stringify!([< $codec_name:camel >]),
"::",
stringify!([< $var_name:camel >]),
)
}
fn encode<W>(&self, w: &mut W) -> ::std::io::Result<()>
where
W: ::std::io::Write
{
#[cfg(debug_assertions)]
#[allow(dead_code)]
{
const MSG: &str = "type index must begin at 0 and increment by exactly 1 per type - switching type order will break parsing compatibility";
let mut _idx = -1;
$(
_idx += 1;
assert_eq!(_idx, $type_idx, "{}", MSG);
)*
}
let t: (
$(&$type_ty,)*
) = (
$(&self.[< $type_name:snake >],)*
);
$crate::codec::rmp_encode(w, &t)
}
fn decode<R>(r: &mut R) -> ::std::io::Result<Self>
where
R: ::std::io::Read
{
let (
$([< $type_name:snake >],)*
): (
$($type_ty,)*
) = $crate::codec::rmp_decode(r)?;
Ok([< $var_name:camel >] {
$(
[< $type_name:snake >],
)*
})
}
}
)*
$(#[doc = $codec_doc])*
#[derive(Clone, Debug, PartialEq, Eq)]
// NOTE: calling crate must depend on kitsune_p2p_types with feature fuzzing
#[cfg_attr(feature = "fuzzing", derive($crate::dependencies::proptest_derive::Arbitrary))]
pub enum [< $codec_name:camel >] {
$(
$(#[doc = $var_doc])*
[< $var_name:camel >]([< $var_name:camel >]),
)*
}
impl [< $codec_name:camel >] {
$(
/// Variant constructor helper function.
pub fn [< $var_name:snake >]($(
[< $type_name:snake >]: $type_ty,
)*) -> Self {
Self::[< $var_name:camel >]([< $var_name:camel >] {
$(
[< $type_name:snake >],
)*
})
}
)*
}
impl $crate::codec::Codec for [< $codec_name:camel >] {
fn variant_type(&self) -> &'static str {
match self {
$(
Self::[< $var_name:camel >](data) =>
$crate::codec::Codec::variant_type(data),
)*
}
}
fn encode<W>(&self, w: &mut W) -> ::std::io::Result<()>
where
W: ::std::io::Write
{
match self {
$(
Self::[< $var_name:camel >](data) => {
::std::io::Write::write_all(w, &[$var_id])?;
$crate::codec::Codec::encode(data, w)
}
)*
}
}
fn decode<R>(r: &mut R) -> Result<Self, ::std::io::Error>
where
R: ::std::io::Read
{
let mut c = [0_u8; 1];
::std::io::Read::read_exact(r, &mut c)?;
match c[0] {
$(
$var_id => {
Ok(Self::[< $var_name:camel >]($crate::codec::Codec::decode(r)?))
},
)*
_ => Err(::std::io::Error::new(::std::io::ErrorKind::Other, "invalid protocol byte")),
}
}
}
}
};
}
#[cfg(test)]
mod tests {
#![allow(dead_code)]
use super::*;
use std::sync::Arc;
#[derive(
Clone,
Debug,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
proptest_derive::Arbitrary,
)]
pub struct Sub(pub Vec<u8>);
write_codec_enum! {
/// Codec
codec Bob {
/// variant
BobOne(0x42) {
/// type 1
yay.0: bool,
/// type 2
age.1: u32,
/// type 3
sub.2: Arc<Sub>,
},
/// nother variant
BobTwo(0x43) {
},
}
}
#[test]
fn test_encode_decode() {
let bob = Bob::bob_one(true, 42, Arc::new(Sub(b"test".to_vec())));
let data = bob.encode_vec().unwrap();
let res = Bob::decode_ref(&data).unwrap().1;
assert_eq!(bob, res);
}
}