request_response/request.rs
1//! This file contains the [`Request`] and [`Response`] traits. Any upstream
2//! that wants to use the [`RequestResponseProtocol`] needs to implement these
3//! traits for their specific types.
4
5use std::fmt::Debug;
6
7use anyhow::Result;
8use async_trait::async_trait;
9
10use super::Serializable;
11
12/// A trait for a request. Associates itself with a response type.
13#[async_trait]
14#[cfg(not(test))]
15pub trait Request: Send + Sync + Serializable + 'static + Clone + Debug {
16 /// The response type associated with this request
17 type Response: Send + Sync + Serializable + Clone + Debug;
18
19 /// Validate the request, returning an error if it is not valid
20 ///
21 /// # Errors
22 /// If the request is not valid
23 async fn validate(&self) -> Result<()>;
24}
25
26/// A trait for a request. Associates itself with a response type.
27#[async_trait]
28#[cfg(test)]
29pub trait Request: Send + Sync + Serializable + 'static + Clone + Debug {
30 /// The response type associated with this request
31 type Response: Send + Sync + Serializable + Clone + Debug + PartialEq + Eq;
32
33 /// Validate the request, returning an error if it is not valid
34 ///
35 /// # Errors
36 /// If the request is not valid
37 async fn validate(&self) -> Result<()>;
38}