hyprstream_rpc/service/traits.rs
1//! Service traits for RPC handlers.
2
3use anyhow::Result;
4
5/// Trait for RPC service implementations.
6///
7/// This is the core service trait that handles incoming requests.
8/// Implementations should parse the request, dispatch to handlers,
9/// and build responses.
10pub trait RpcService: Send + 'static {
11 /// Handle an incoming request and return a response.
12 ///
13 /// The request and response are Cap'n Proto serialized bytes.
14 fn handle_request(&self, request: &[u8]) -> Result<Vec<u8>>;
15
16 /// Get the service name for logging/debugging.
17 fn name(&self) -> &str;
18}
19
20/// Trait for individual RPC method handlers.
21///
22/// Each RPC method can be implemented as a separate handler,
23/// allowing for cleaner separation of concerns.
24pub trait RpcHandler<Req, Resp>: Send + Sync {
25 /// Handle a typed request and return a typed response.
26 fn handle(&self, request: Req) -> Result<Resp>;
27}
28
29/// Marker trait for request types that can identify their expected response.
30///
31/// Used by generic clients to ensure type safety between requests and responses.
32pub trait RpcRequest {
33 /// The response type expected for this request.
34 type Response;
35
36 /// Get the request variant name (for logging/debugging).
37 fn variant_name(&self) -> &'static str;
38}