reinhardt_grpc/lib.rs
1#![warn(missing_docs)]
2//! gRPC support for Reinhardt framework
3//!
4//! This crate provides gRPC infrastructure features for the Reinhardt framework.
5//!
6//! # Features
7//!
8//! - Common Protobuf types (Empty, Timestamp, Error, PageInfo, BatchResult)
9//! - GraphQL over gRPC types (GraphQLRequest, GraphQLResponse, SubscriptionEvent)
10//! - gRPC error handling with production-safe error sanitization
11//! - gRPC service adapter trait
12//! - Server configuration with message size limits, request timeouts, and connection limits
13//! - Protobuf message nesting depth limits
14//! - Protobuf field constraint validation
15//! - Dependency injection support (with `di` feature)
16//!
17//! # Security
18//!
19//! This crate includes several security features:
20//!
21//! - **Depth-limited decoding**: Prevents stack overflow from deeply nested
22//! protobuf messages via [`depth_limit::DepthLimitedDecoder`].
23//! - **Field validation**: Enforces protobuf field constraints (required
24//! fields, value ranges) via [`validation::ProtoValidator`].
25//! - **Error sanitization**: Prevents information leakage through error
26//! messages via [`error::ErrorSanitizer`].
27//! - **DI error sanitization**: Prevents type name leakage through DI
28//! error messages (with `di` feature).
29//!
30//! # Usage
31//!
32//! Users can define their own .proto files in their projects,
33//! and utilize the common types and adapter traits from this crate.
34//!
35//! ## Dependency Injection
36//!
37//! Enable the `di` feature to use dependency injection in gRPC handlers:
38//!
39//! ```toml
40//! [dependencies]
41//! reinhardt-grpc = { version = "0.1", features = ["di"] }
42//! ```
43//!
44//! Then use the `#[grpc_handler]` macro:
45//!
46//! ```rust,ignore
47//! # use reinhardt_grpc::{GrpcRequestExt, grpc_handler};
48//! # use tonic::{Request, Response, Status};
49//! # struct GetUserRequest;
50//! # struct User;
51//! # struct DatabaseConnection;
52//! # struct Handler;
53//! # impl Handler {
54//! #[grpc_handler]
55//! async fn get_user_impl(
56//! &self,
57//! request: Request<GetUserRequest>,
58//! #[inject] db: DatabaseConnection,
59//! ) -> Result<Response<User>, Status> {
60//! // db is automatically resolved
61//! # Ok(Response::new(User))
62//! }
63//! # }
64//! ```
65
66pub mod adapter;
67pub mod depth_limit;
68pub mod error;
69pub mod server;
70pub mod settings;
71pub mod validation;
72
73#[cfg(feature = "di")]
74pub mod di;
75
76/// Generated Protobuf code (common types provided by the framework).
77///
78/// These types are auto-generated by `tonic`/`prost` from `.proto` definitions
79/// and do not carry Rust doc comments.
80#[allow(missing_docs)] // Auto-generated protobuf code
81pub mod proto {
82 /// Common Protobuf types (Empty, Timestamp, Error, PageInfo, BatchResult).
83 pub mod common {
84 tonic::include_proto!("reinhardt.common");
85 }
86
87 /// GraphQL over gRPC types (GraphQLRequest, GraphQLResponse, SubscriptionEvent).
88 pub mod graphql {
89 tonic::include_proto!("reinhardt.graphql");
90 }
91}
92
93pub use adapter::{GrpcServiceAdapter, GrpcSubscriptionAdapter};
94pub use depth_limit::{DepthLimitError, DepthLimitedDecoder};
95pub use error::{ErrorSanitizer, GrpcError, GrpcResult};
96#[allow(deprecated)] // Re-export keeps the compatibility API discoverable during the 0.2 line.
97pub use server::GrpcServerConfig;
98pub use server::{GrpcServerConfigBuilder, MessageSizeLimiter};
99pub use settings::{GrpcServerSettings, create_grpc_server_config_from_settings};
100pub use validation::{FieldRule, ProtoValidator, ValidationError, ValidationRuleSet};
101
102#[cfg(feature = "di")]
103pub use di::GrpcRequestExt;
104
105#[cfg(feature = "di")]
106pub use reinhardt_grpc_macros::grpc_handler;