hrpc/
lib.rs

1//! Generic client and server implementations and transport implementations for hRPC.
2//!
3//! - For generic client and client implementations, see the [`client`] module.
4//! - For generic server and server implementations, see the [`server`] module.
5//! - For common code shared by client and servers, see the [`common`] module.
6//! - Modules named `transport` contain transport specific code.
7//! - Modules named `layer` contain layers for use. These can be generic, or
8//! transport specific.
9#![deny(missing_docs)]
10#![allow(clippy::blocks_in_if_conditions)]
11
12/// Some re-exported crates that might be useful while writing software with `hrpc`.
13pub mod exports {
14    pub use bytes;
15    pub use futures_util;
16    pub use prost;
17    pub use tracing;
18
19    #[cfg(feature = "_common_http")]
20    pub use http;
21    #[cfg(feature = "_common")]
22    pub use tower;
23}
24
25/// Common client types and functions.
26#[cfg(feature = "client")]
27pub mod client;
28/// Common server types and functions.
29#[cfg(feature = "server")]
30pub mod server;
31
32/// Body utitilies and types.
33pub mod body;
34/// Common utilities.
35pub mod common;
36/// Decoding utilities.
37pub mod decode;
38/// Encoding utilities.
39pub mod encode;
40/// The hRPC generated protocol.
41pub mod proto;
42/// The `Request` type used by hRPC.
43pub mod request;
44/// The `Response` type used by hRPC.
45pub mod response;
46
47use std::error::Error;
48
49#[doc(inline)]
50pub use request::Request;
51#[doc(inline)]
52pub use response::Response;
53
54/// Alias for a type-erased error type.
55pub type BoxError = Box<dyn Error + Send + Sync>;
56
57/// Convenience function for converting some error to a boxed error.
58pub fn box_error<Err>(err: Err) -> BoxError
59where
60    Err: Error + Send + Sync + 'static,
61{
62    Box::new(err)
63}
64
65/// The hRPC protobuf mimetype.
66pub const HRPC_CONTENT_MIMETYPE: &str = "application/hrpc";
67/// The hRPC spec version this version of `hrpc-rs` implements.
68pub const HRPC_SPEC_VERSION: &str = "1";
69
70/// Include generated proto server and client items.
71///
72/// You must specify the hRPC package name.
73///
74/// ```rust,ignore
75/// mod pb {
76///     hrpc::include_proto!("helloworld");
77/// }
78/// ```
79///
80/// # Note:
81/// **This only works if the hrpc-build output directory has been unmodified**.
82/// The default output directory is set to the [`OUT_DIR`] environment variable.
83/// If the output directory has been modified, the following pattern may be used
84/// instead of this macro.
85///
86/// ```rust,ignore
87/// mod pb {
88///     include!("/relative/protobuf/directory/helloworld.rs");
89/// }
90/// ```
91/// You can also use a custom environment variable using the following pattern.
92/// ```rust,ignore
93/// mod pb {
94///     include!(concat!(env!("PROTOBUFS"), "/helloworld.rs"));
95/// }
96/// ```
97///
98/// [`OUT_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
99#[macro_export]
100macro_rules! include_proto {
101    ($package: tt) => {
102        include!(concat!(env!("OUT_DIR"), concat!("/", $package, ".rs")));
103    };
104}