htrpc/
lib.rs

1//! HTTP based RPC library.
2//!
3//! This crate provides a thin framework to easily implement type-safe RPC channels
4//! for client/server model communication.
5#![warn(missing_docs)]
6extern crate fibers;
7extern crate futures;
8extern crate handy_async;
9extern crate miasht;
10extern crate serde;
11#[macro_use]
12extern crate serde_derive;
13extern crate serdeconv;
14#[macro_use]
15extern crate slog;
16#[macro_use]
17extern crate trackable;
18extern crate url;
19
20pub use miasht::builtin::futures::FutureExt;
21
22#[allow(missing_docs)]
23pub type BodyReader =
24    miasht::builtin::io::BodyReader<miasht::server::Request<fibers::net::TcpStream>>;
25
26#[allow(missing_docs)]
27pub fn content_length(body: &BodyReader) -> Option<u64> {
28    if let miasht::builtin::io::BodyReader::FixedLength(ref r) = *body {
29        Some(r.limit())
30    } else {
31        None
32    }
33}
34
35#[allow(missing_docs)]
36pub type ReadBody<T> =
37    Box<dyn futures::Future<Item = (BodyReader, T), Error = Error> + Send + 'static>;
38
39pub use client::RpcClient;
40pub use error::{Error, ErrorKind};
41pub use procedure::{HandleRpc, Procedure, RpcRequest, RpcResponse};
42pub use server::{RpcServer, RpcServerBuilder};
43
44/// A helper macro to construct an `EntryPoint` instance.
45///
46/// # Examples
47///
48/// ```
49/// # #[macro_use]
50/// # extern crate htrpc;
51/// use htrpc::types::{EntryPoint, PathSegment};
52///
53/// # fn main() {
54/// static SEGMENTS: &[PathSegment] =
55///     &[PathSegment::Val("foo"), PathSegment::Var, PathSegment::Val("baz")];
56/// let p0 = EntryPoint::new(SEGMENTS);
57/// let p1 = htrpc_entry_point!["foo", _, "baz"];
58/// assert_eq!(p0, p1);
59/// # }
60/// ```
61#[macro_export]
62macro_rules! htrpc_entry_point {
63    ($($segment:tt),*) => {
64        {
65            static SEGMENTS: &[$crate::types::PathSegment] =
66                &[$(htrpc_expand_segment!($segment)),*];
67            $crate::types::EntryPoint::new(SEGMENTS)
68        }
69    }
70}
71
72#[doc(hidden)]
73#[macro_export]
74macro_rules! htrpc_expand_segment {
75    (_) => {
76        $crate::types::PathSegment::Var
77    };
78    ($s:expr) => {
79        $crate::types::PathSegment::Val($s)
80    };
81}
82
83pub mod deserializers;
84pub mod json;
85pub mod json_pretty;
86pub mod msgpack;
87pub mod pool;
88pub mod rfc7807;
89pub mod serializers;
90pub mod types;
91
92mod client;
93mod error;
94mod misc;
95mod procedure;
96mod router;
97mod server;
98
99/// This crate specific `Result` type.
100pub type Result<T> = ::std::result::Result<T, Error>;