From c99f716cbec8fe57f9b163398b8b7c616b06399e Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Fri, 19 Jul 2024 20:49:31 +0530 Subject: [PATCH] refactor: add lift (#2472) Co-authored-by: Kiryl Mialeshka <8974488+meskill@users.noreply.github.com> --- src/core/jit/error.rs | 21 +++++++++------------ src/core/jit/graphql_executor.rs | 1 - src/core/jit/response.rs | 6 +++--- src/core/lift.rs | 24 ++++++++++++++++++++++++ src/core/mod.rs | 1 + 5 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 src/core/lift.rs diff --git a/src/core/jit/error.rs b/src/core/jit/error.rs index 82f3564a68..6342529ce4 100644 --- a/src/core/jit/error.rs +++ b/src/core/jit/error.rs @@ -2,6 +2,8 @@ use async_graphql::parser::types::OperationType; use async_graphql::{ErrorExtensions, PathSegment, Pos, Positioned, ServerError}; use thiserror::Error; +use crate::core::lift::Lift; + #[derive(Error, Debug, Clone, PartialEq, Eq)] #[error("Error while building the plan")] pub enum BuildError { @@ -78,14 +80,8 @@ impl Error { server_error } -} -pub trait IntoServerError { - fn into_server_error(self) -> ServerError; -} - -impl IntoServerError for Error { - fn into_server_error(self) -> ServerError { + pub fn into_server_error(self) -> ServerError { match self { // async_graphql::parser::Error has special conversion to ServerError Error::ParseError(error) => error.into(), @@ -94,12 +90,13 @@ impl IntoServerError for Error { } } -impl IntoServerError for Positioned { - fn into_server_error(self) -> ServerError { - match self.node { +impl From> for Lift { + fn from(a: Positioned) -> Self { + (match a.node { // async_graphql::parser::Error already has builtin positioning Error::ParseError(error) => error.into(), - error => error.into_server_error_with_pos(Some(self.pos)), - } + error => error.into_server_error_with_pos(Some(a.pos)), + }) + .into() } } diff --git a/src/core/jit/graphql_executor.rs b/src/core/jit/graphql_executor.rs index 42a573e61e..abdc0e1b84 100644 --- a/src/core/jit/graphql_executor.rs +++ b/src/core/jit/graphql_executor.rs @@ -4,7 +4,6 @@ use std::sync::Arc; use async_graphql::{Data, Executor, Response}; use futures_util::stream::BoxStream; -use super::IntoServerError; use crate::core::app_context::AppContext; use crate::core::http::RequestContext; use crate::core::jit; diff --git a/src/core/jit/response.rs b/src/core/jit/response.rs index dc91d049ab..57324b2dbf 100644 --- a/src/core/jit/response.rs +++ b/src/core/jit/response.rs @@ -1,9 +1,9 @@ -use async_graphql::Positioned; +use async_graphql::{Positioned, ServerError}; use derive_setters::Setters; use serde::Serialize; -use super::IntoServerError; use crate::core::jit; +use crate::core::lift::Lift; #[derive(Setters, Serialize)] pub struct Response { @@ -35,7 +35,7 @@ impl Response { resp = resp.extension(name, value); } for error in self.errors { - resp.errors.push(error.into_server_error()); + resp.errors.push(Lift::::from(error).take()); } resp } diff --git a/src/core/lift.rs b/src/core/lift.rs new file mode 100644 index 0000000000..382d4041c6 --- /dev/null +++ b/src/core/lift.rs @@ -0,0 +1,24 @@ +use std::ops::Deref; + +/// +/// Just an empty wrapper around a value used to implement `From` for foreign +/// types. +pub struct Lift(A); +impl Deref for Lift { + type Target = A; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Lift { + pub fn take(self) -> A { + self.0 + } +} + +impl From for Lift { + fn from(a: A) -> Self { + Lift(a) + } +} diff --git a/src/core/mod.rs b/src/core/mod.rs index ab7c9126f3..a055c275a8 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -21,6 +21,7 @@ pub mod http; pub mod ir; pub mod jit; pub mod json; +mod lift; pub mod merge_right; pub mod mustache; pub mod path;