Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kardeiz committed Jan 13, 2025
1 parent b100b3a commit 53da757
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 285 deletions.
7 changes: 5 additions & 2 deletions examples/actix-web.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web_v4::{guard, web, App, HttpServer};
use jsonrpc_v2::{Data, Error, Params, Server, HttpRequestLocalData};
use jsonrpc_v2::{Data, Error, HttpRequestLocalData, Params, Server};

#[derive(serde::Deserialize)]
struct TwoNums {
Expand All @@ -13,7 +13,10 @@ async fn test(Params(params): Params<serde_json::Value>) -> Result<String, Error
Ok(serde_json::to_string_pretty(&params).unwrap())
}

async fn add(Params(params): Params<TwoNums>, req_path: HttpRequestLocalData<String>) -> Result<usize, Error> {
async fn add(
Params(params): Params<TwoNums>,
req_path: HttpRequestLocalData<String>,
) -> Result<usize, Error> {
dbg!(req_path.0);
Ok(params.a + params.b)
}
Expand Down
8 changes: 5 additions & 3 deletions examples/hyper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyper_util::rt::TokioIo;
use jsonrpc_v2::*;
use std::net::SocketAddr;
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;

#[derive(serde::Deserialize)]
Expand All @@ -9,7 +9,10 @@ struct TwoNums {
b: usize,
}

async fn add(Params(params): Params<TwoNums>, req_path: HttpRequestLocalData<String>) -> Result<usize, Error> {
async fn add(
Params(params): Params<TwoNums>,
req_path: HttpRequestLocalData<String>,
) -> Result<usize, Error> {
dbg!(req_path.0);
Ok(params.a + params.b)
}
Expand Down Expand Up @@ -56,7 +59,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Spawn a tokio task to serve multiple connections concurrently
tokio::task::spawn(async move {

// Finally, we bind the incoming connection to our `hello` service
if let Err(err) = ::hyper::server::conn::http1::Builder::new()
// `service_fn` converts our function in a `Service`
Expand Down
214 changes: 109 additions & 105 deletions src/actix_web.rs
Original file line number Diff line number Diff line change
@@ -1,105 +1,109 @@
pub use futures::future::TryFutureExt;
pub use futures::stream::TryStreamExt;

pub use bytes_v10::Bytes;

use futures::future::FutureExt;
use std::sync::Arc;
use futures::Future;
use serde::Serialize;

use crate::{Factory, Handler, RequestObjectWithData, Router, Error, BoxedSerialize, FromRequest, Server, ResponseObjects};

/// Wrapper around `actix_web::HttpRequest`
#[derive(Clone)]
pub struct HttpRequest(pub actix_web_v4::HttpRequest);

impl std::ops::Deref for HttpRequest {
type Target = actix_web_v4::HttpRequest;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub struct BoxedHandler(
pub Arc<
dyn Fn(
RequestObjectWithData,
)
-> std::pin::Pin<Box<dyn Future<Output = Result<BoxedSerialize, Error>>>>
+ Send
+ Sync,
>,
);

impl<F, S, E, T> From<Handler<F, S, E, T>> for BoxedHandler
where
F: Factory<S, E, T> + 'static + Send + Sync,
S: Serialize + Send + 'static,
Error: From<E>,
E: 'static,
T: FromRequest + 'static + Send,
{
fn from(t: Handler<F, S, E, T>) -> BoxedHandler {
let hnd = Arc::new(t.hnd);

let inner = move |req: RequestObjectWithData| {
let hnd = Arc::clone(&hnd);
Box::pin(async move {
let out = {
let param = T::from_request(&req).await?;
hnd.call(param).await?
};
Ok(Box::new(out) as BoxedSerialize)
}) as std::pin::Pin<Box<dyn Future<Output = Result<BoxedSerialize, Error>>>>
};

BoxedHandler(Arc::new(inner))
}
}


impl<R> Server<R>
where
R: Router + 'static,
{
/// Converts the server into an `actix-web` compatible `NewService`
pub fn into_web_service(
self: Arc<Self>,
) -> impl actix_service_v2::ServiceFactory<
actix_web_v4::dev::ServiceRequest,
Response = actix_web_v4::dev::ServiceResponse,
Error = actix_web_v4::Error,
Config = (),
InitError = (),
> {
let service = Arc::clone(&self);

let inner = move |req: actix_web_v4::dev::ServiceRequest| {
let service = Arc::clone(&service);
let (req, payload) = req.into_parts();
let rt = payload
.map_err(actix_web_v4::Error::from)
.try_fold(actix_web_v4::web::BytesMut::new(), move |mut body, chunk| async move {
body.extend_from_slice(&chunk);
Ok::<_, actix_web_v4::Error>(body)
})
.and_then(move |bytes| {
service.handle_bytes(bytes.freeze(), Some(HttpRequest(req.clone()))).map(|res| match res {
ResponseObjects::Empty => Ok(actix_web_v4::dev::ServiceResponse::new(
req,
actix_web_v4::HttpResponse::NoContent().finish(),
)),
json => Ok(actix_web_v4::dev::ServiceResponse::new(
req,
actix_web_v4::HttpResponse::Ok().json(json),
)),
})
});
rt
};

actix_service_v2::fn_service::<_, _, _, _, _, _>(inner)
}
}
pub use futures::future::TryFutureExt;
pub use futures::stream::TryStreamExt;

pub use bytes_v10::Bytes;

use futures::future::FutureExt;
use futures::Future;
use serde::Serialize;
use std::sync::Arc;

use crate::{
BoxedSerialize, Error, Factory, FromRequest, Handler, RequestObjectWithData, ResponseObjects,
Router, Server,
};

/// Wrapper around `actix_web::HttpRequest`
#[derive(Clone)]
pub struct HttpRequest(pub actix_web_v4::HttpRequest);

impl std::ops::Deref for HttpRequest {
type Target = actix_web_v4::HttpRequest;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub struct BoxedHandler(
pub Arc<
dyn Fn(
RequestObjectWithData,
)
-> std::pin::Pin<Box<dyn Future<Output = Result<BoxedSerialize, Error>>>>
+ Send
+ Sync,
>,
);

impl<F, S, E, T> From<Handler<F, S, E, T>> for BoxedHandler
where
F: Factory<S, E, T> + 'static + Send + Sync,
S: Serialize + Send + 'static,
Error: From<E>,
E: 'static,
T: FromRequest + 'static + Send,
{
fn from(t: Handler<F, S, E, T>) -> BoxedHandler {
let hnd = Arc::new(t.hnd);

let inner = move |req: RequestObjectWithData| {
let hnd = Arc::clone(&hnd);
Box::pin(async move {
let out = {
let param = T::from_request(&req).await?;
hnd.call(param).await?
};
Ok(Box::new(out) as BoxedSerialize)
}) as std::pin::Pin<Box<dyn Future<Output = Result<BoxedSerialize, Error>>>>
};

BoxedHandler(Arc::new(inner))
}
}

impl<R> Server<R>
where
R: Router + 'static,
{
/// Converts the server into an `actix-web` compatible `NewService`
pub fn into_web_service(
self: Arc<Self>,
) -> impl actix_service_v2::ServiceFactory<
actix_web_v4::dev::ServiceRequest,
Response = actix_web_v4::dev::ServiceResponse,
Error = actix_web_v4::Error,
Config = (),
InitError = (),
> {
let service = Arc::clone(&self);

let inner = move |req: actix_web_v4::dev::ServiceRequest| {
let service = Arc::clone(&service);
let (req, payload) = req.into_parts();
let rt = payload
.map_err(actix_web_v4::Error::from)
.try_fold(actix_web_v4::web::BytesMut::new(), move |mut body, chunk| async move {
body.extend_from_slice(&chunk);
Ok::<_, actix_web_v4::Error>(body)
})
.and_then(move |bytes| {
service.handle_bytes(bytes.freeze(), Some(HttpRequest(req.clone()))).map(
|res| match res {
ResponseObjects::Empty => Ok(actix_web_v4::dev::ServiceResponse::new(
req,
actix_web_v4::HttpResponse::NoContent().finish(),
)),
json => Ok(actix_web_v4::dev::ServiceResponse::new(
req,
actix_web_v4::HttpResponse::Ok().json(json),
)),
},
)
});
rt
};

actix_service_v2::fn_service::<_, _, _, _, _, _>(inner)
}
}
22 changes: 11 additions & 11 deletions src/factory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(feature = "actix-web-v4")]
mod actix_web;

#[cfg(feature = "actix-web-v4")]
pub use actix_web::*;

#[cfg(feature = "hyper-integration")]
mod hyper;

#[cfg(feature = "hyper-integration")]
pub use hyper::*;
#[cfg(feature = "actix-web-v4")]
mod actix_web;

#[cfg(feature = "actix-web-v4")]
pub use actix_web::*;

#[cfg(feature = "hyper-integration")]
mod hyper;

#[cfg(feature = "hyper-integration")]
pub use hyper::*;
Loading

0 comments on commit 53da757

Please sign in to comment.