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
//! `common` contains types useful across multiple protocol versions.
//! You shouldn't need to use it unless you're making your own.

use serde::{
    de::{Deserializer, Visitor},
    ser::Serializer,
};
use std::borrow::Cow;
use std::convert::AsRef;
use std::error::Error;
use std::fmt::{self, Formatter};
use std::marker::PhantomData;

/// A CowStr allows hard-coded strings to be used in places.
pub type CowStr = Cow<'static, str>;

/// A visitor that converts byte slices / vecs into the desired type
struct GimmeBytesVisitor<T>(PhantomData<T>);

impl<'de, T> Visitor<'de> for GimmeBytesVisitor<T>
where
    for<'a> T: From<Vec<u8>> + From<&'a [u8]>,
{
    type Value = T;
    fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "some sorta bytes")
    }

    fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
        Ok(v.into())
    }

    fn visit_borrowed_bytes<E: Error>(self, v: &'de [u8]) -> Result<Self::Value, E> {
        Ok(v.into())
    }

    fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
        Ok(v.into())
    }
}

// TODO: support cow and ref
/// A deserialize function that produces a Vec<u8> using the correct deserializer method.
pub fn deserialize_owned_bytes<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
    D: Deserializer<'de>,
{
    deserializer.deserialize_byte_buf(GimmeBytesVisitor(PhantomData))
}

/// A serializer function that serializes any byte slice like object.
pub fn serialize_bytes<T, S>(t: &T, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
    T: AsRef<[u8]>,
{
    serializer.serialize_bytes(t.as_ref())
}

/// An implementer of MessageTypeId has a u8 ID.
pub trait MessageTypeId {
    const MSG_TYPE_ID: u8;

    /// Get self's ID.
    /// Does not need to be implemented.
    fn msg_type_id(&self) -> u8 {
        Self::MSG_TYPE_ID
    }
}

/// Helper for message_type_ids.
/// Implements the MessageTypeId trait for the given type.
macro_rules! message_type_id {
    ($mtype:ty, $id:expr) => {
        impl $crate::common::MessageTypeId for $mtype {
            const MSG_TYPE_ID: u8 = $id;
        }
    };
}

/// Allows you to write message type IDs all at once, similar
/// to how they'd be written in in Fcall.h.
macro_rules! message_type_ids {
    {$($mtype:ty = $id:expr),*} => {
        $(
            message_type_id!($mtype, $id);
        )*
    }
}