Skip to content

Commit

Permalink
fix: added more bounds to rpc types
Browse files Browse the repository at this point in the history
  • Loading branch information
heilhead committed May 28, 2023
1 parent d27ecc2 commit 6483d20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions relay_rpc/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
#[cfg(test)]
mod tests;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, Clone, thiserror::Error)]
pub enum ClientIdDecodingError {
#[error("Invalid issuer multicodec base")]
Base,
Expand All @@ -27,7 +27,7 @@ pub enum ClientIdDecodingError {
Length,
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum DecodingError {
#[error("Invalid encoding")]
Encoding,
Expand Down
19 changes: 14 additions & 5 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use {
crate::domain::{DecodingError, MessageId, SubscriptionId, Topic},
serde::{de::DeserializeOwned, Deserialize, Serialize},
std::sync::Arc,
std::{fmt::Debug, sync::Arc},
};

#[cfg(test)]
Expand All @@ -29,7 +29,7 @@ pub const MAX_FETCH_BATCH_SIZE: usize = 500;
type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// Errors covering payload validation problems.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum ValidationError {
#[error("Topic decoding failed: {0}")]
TopicDecoding(DecodingError),
Expand Down Expand Up @@ -109,14 +109,23 @@ where
}
}

pub trait Serializable:
Debug + Clone + PartialEq + Eq + Serialize + DeserializeOwned + Send + Sync + 'static
{
}
impl<T> Serializable for T where
T: Debug + Clone + PartialEq + Eq + Serialize + DeserializeOwned + Send + Sync + 'static
{
}

/// Trait that adds validation capabilities and strong typing to errors and
/// successful responses. Implemented for all possible RPC request types.
pub trait RequestPayload {
pub trait RequestPayload: Serializable {
/// The error representing a failed request.
type Error: Into<ErrorData>;
type Error: Into<ErrorData> + Send + 'static;

/// The type of a successful response.
type Response: Serialize + DeserializeOwned;
type Response: Serializable;

/// Validates the request parameters.
fn validate(&self) -> Result<(), ValidationError> {
Expand Down

0 comments on commit 6483d20

Please sign in to comment.