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
//! # RPC-IT
//!
//! Low-level RPC abstraction
//!
//! # Concepts
//!
//! There are three concepts for RPC handling:
//!
//! - Send Notify
//! - Send Request => Recv Response
//! - Recv Request
//!
//! This library is modeled after the `msgpack-rpc`, but in general, most RPC protocols follow
//! similar patterns, we may adopt this to other protocols in the future. (JSON-RPC, etc...)
//!
//!
//!
//!
//! # Usage
//!
//!
//!

// Re-export crates
pub extern crate bytes;
pub extern crate erased_serde;
pub extern crate futures_util;
pub extern crate serde;

pub mod codec;
pub mod codecs;
pub mod rpc;
pub mod transport;
pub mod transports;

#[cfg(feature = "service-macros")]
pub use rpc_it_macros::service;

#[cfg(feature = "service")]
pub mod service;

#[cfg(feature = "macros")]
pub mod __util {
    use serde::ser::SerializeMap;

    pub fn iter_as_map<'a, I>(iter: I) -> impl serde::Serialize + 'a
    where
        I: IntoIterator<Item = (&'a dyn erased_serde::Serialize, &'a dyn erased_serde::Serialize)>
            + 'a,
        I::IntoIter: ExactSizeIterator + Clone,
    {
        struct IterAsMap<'a, I>(I)
        where
            I: Iterator<Item = (&'a dyn erased_serde::Serialize, &'a dyn erased_serde::Serialize)>
                + ExactSizeIterator
                + Clone;

        impl<'a, I> serde::Serialize for IterAsMap<'a, I>
        where
            I: Iterator<Item = (&'a dyn erased_serde::Serialize, &'a dyn erased_serde::Serialize)>
                + ExactSizeIterator
                + Clone,
        {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                let mut map = serializer.serialize_map(Some(self.0.len()))?;
                for (k, v) in self.0.clone() {
                    map.serialize_entry(k, v)?;
                }
                map.end()
            }
        }

        IterAsMap(iter.into_iter())
    }
}

pub use rpc::{msg::*, *};

pub use serde::de::IgnoredAny;

#[cfg(feature = "service")]
pub use service::{
    ExactMatchRouter, RegisterError, RouteMessageError, Router, Service, ServiceBuilder,
    TypedRequest, TypedResponse,
};

/// Create a map from a list of key-value pairs.
///
/// This is useful to quickly create a fixed-sized map for serialization.
#[cfg(feature = "macros")]
#[macro_export]
macro_rules! kv_pairs {
    ($($key:tt = $value:expr),*) => {
        $crate::__util::iter_as_map([$(
            (&($key) as &dyn $crate::erased_serde::Serialize,
             &($value) as &dyn $crate::erased_serde::Serialize)
        ),*])
    };
}