From f12d5e16e5a6da925f2325376a4d2694ddb8b5ae Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Wed, 11 Oct 2023 00:17:18 +0200 Subject: [PATCH] Add `impl From for JsonResponse`. Co-Authored-By: Jesse Hallett <9622+hallettj@users.noreply.github.com> --- rust-connector-sdk/src/connector/example.rs | 10 ++++++---- rust-connector-sdk/src/json_response.rs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/rust-connector-sdk/src/connector/example.rs b/rust-connector-sdk/src/connector/example.rs index a5438939..42ffb10b 100644 --- a/rust-connector-sdk/src/connector/example.rs +++ b/rust-connector-sdk/src/connector/example.rs @@ -51,7 +51,7 @@ impl Connector for Example { } async fn get_capabilities() -> JsonResponse { - JsonResponse::Value(models::CapabilitiesResponse { + models::CapabilitiesResponse { versions: "^0.1.0".into(), capabilities: models::Capabilities { explain: None, @@ -63,7 +63,8 @@ impl Connector for Example { relation_comparisons: None, }), }, - }) + } + .into() } async fn get_schema( @@ -75,13 +76,14 @@ impl Connector for Example { .instrument(info_span!("tracing example")) .await; - Ok(JsonResponse::Value(models::SchemaResponse { + Ok(models::SchemaResponse { collections: vec![], functions: vec![], procedures: vec![], object_types: BTreeMap::new(), scalar_types: BTreeMap::new(), - })) + } + .into()) } async fn explain( diff --git a/rust-connector-sdk/src/json_response.rs b/rust-connector-sdk/src/json_response.rs index f6a20274..85be6e89 100644 --- a/rust-connector-sdk/src/json_response.rs +++ b/rust-connector-sdk/src/json_response.rs @@ -16,6 +16,12 @@ pub enum JsonResponse { Serialized(Bytes), } +impl From for JsonResponse { + fn from(value: A) -> Self { + Self::Value(value) + } +} + impl serde::Deserialize<'de>)> JsonResponse { /// Unwraps the value, deserializing if necessary. /// @@ -25,8 +31,8 @@ impl serde::Deserialize<'de>)> JsonResponse { self, ) -> Result { match self { - JsonResponse::Value(value) => Ok(value), - JsonResponse::Serialized(bytes) => { + Self::Value(value) => Ok(value), + Self::Serialized(bytes) => { serde_json::de::from_slice(&bytes).map_err(|err| E::from(Box::new(err))) } } @@ -36,8 +42,8 @@ impl serde::Deserialize<'de>)> JsonResponse { impl IntoResponse for JsonResponse { fn into_response(self) -> axum::response::Response { match self { - JsonResponse::Value(value) => axum::Json(value).into_response(), - JsonResponse::Serialized(bytes) => ( + Self::Value(value) => axum::Json(value).into_response(), + Self::Serialized(bytes) => ( [( header::CONTENT_TYPE, HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()),