Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: transport generalization for recursive declaration #987

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion crates/context/config/src/client/protocol/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use thiserror::Error;
use url::Url;

use super::Protocol;
use crate::client::transport::{AssociatedTransport, Operation, Transport, TransportRequest};
use crate::client::transport::{
AssociatedTransport, Operation, Transport, TransportLike, TransportRequest,
};

#[derive(Copy, Clone, Debug)]
pub enum Near {}
Expand All @@ -38,6 +40,22 @@ impl AssociatedTransport for NearTransport<'_> {
type Protocol = Near;
}

impl TransportLike for NearTransport<'_> {
type Error = NearError;

async fn try_send(
&self,
request: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>> {
if request.protocol == "near" {
Some(self.send(request, payload.to_vec()).await)
} else {
None
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(try_from = "serde_creds::Credentials")]
pub struct Credentials {
Expand Down
20 changes: 19 additions & 1 deletion crates/context/config/src/client/protocol/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use starknet::signers::{LocalWallet, SigningKey};
use thiserror::Error;

use super::Protocol;
use crate::client::transport::{AssociatedTransport, Operation, Transport, TransportRequest};
use crate::client::transport::{
AssociatedTransport, Operation, Transport, TransportLike, TransportRequest,
};

#[derive(Copy, Clone, Debug)]
pub enum Starknet {}
Expand All @@ -26,6 +28,22 @@ impl AssociatedTransport for StarknetTransport<'_> {
type Protocol = Starknet;
}

impl TransportLike for StarknetTransport<'_> {
type Error = StarknetError;

async fn try_send(
&self,
request: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>> {
if request.protocol == "near" {
Some(self.send(request, payload.to_vec()).await)
} else {
None
}
}
}

#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(try_from = "serde_creds::Credentials")]
pub struct Credentials {
Expand Down
60 changes: 43 additions & 17 deletions crates/context/config/src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::error::Error;
use std::borrow::Cow;

use either::Either;
use serde::ser::StdError;
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand All @@ -18,7 +19,7 @@ pub trait Transport {
) -> Result<Vec<u8>, Self::Error>;
}

#[derive(Debug)]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TransportRequest<'a> {
pub protocol: Cow<'a, str>,
Expand Down Expand Up @@ -72,13 +73,45 @@ impl<L: Transport, R: Transport> Transport for Either<L, R> {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
pub enum Operation<'a> {
Read { method: Cow<'a, str> },
Write { method: Cow<'a, str> },
}

pub trait TransportLike {
type Error;

async fn try_send(
&self,
request: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>>;
}

impl<L, R> TransportLike for Both<L, R>
where
L: TransportLike,
R: TransportLike,
{
type Error = EitherError<L::Error, R::Error>;

async fn try_send(
&self,
req: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>> {
if let Some(result) = self.left.try_send(req.clone(), payload).await {
return Some(result.map_err(EitherError::Left));
}
if let Some(result) = self.right.try_send(req, payload).await {
return Some(result.map_err(EitherError::Right));
}
None
}
}

pub trait AssociatedTransport: Transport {
type Protocol: Protocol;

Expand All @@ -98,8 +131,10 @@ pub struct Both<L, R> {

impl<L, R> Transport for Both<L, R>
where
L: AssociatedTransport,
R: AssociatedTransport,
L: TransportLike,
<L as TransportLike>::Error: StdError,
R: TransportLike,
<R as TransportLike>::Error: StdError, //no idea why
{
type Error = EitherError<L::Error, R::Error>;

Expand All @@ -108,20 +143,11 @@ where
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
if request.protocol == L::protocol() {
self.left
.send(request, payload)
.await
.map_err(EitherError::Left)
} else if request.protocol == R::protocol() {
self.right
.send(request, payload)
.await
.map_err(EitherError::Right)
} else {
return Err(EitherError::UnsupportedProtocol(
match self.try_send(request.clone(), &payload).await {
Some(result) => result,
None => Err(EitherError::UnsupportedProtocol(
request.protocol.into_owned(),
));
)),
}
}
}