From 481a09c36e8d36f6db9171284c5fc557c839cd26 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 | 8 +++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/rust-connector-sdk/src/connector/example.rs b/rust-connector-sdk/src/connector/example.rs index a5438939d..42ffb10b5 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 cc96b4ac3..461269234 100644 --- a/rust-connector-sdk/src/json_response.rs +++ b/rust-connector-sdk/src/json_response.rs @@ -7,7 +7,7 @@ use http::{header, HeaderValue}; /// The value may be of a type that implements `serde::Serialize`, or it may be /// a contiguous sequence of bytes, which are _assumed_ to be valid JSON. #[derive(Debug, Clone)] -pub enum JsonResponse serde::Deserialize<'de>)> { +pub enum JsonResponse { /// A value that can be serialized to JSON. Value(A), /// A serialized JSON bytestring that is assumed to represent a value of @@ -33,6 +33,12 @@ impl serde::Deserialize<'de>)> JsonResponse { } } +impl From for JsonResponse { + fn from(value: A) -> Self { + JsonResponse::Value(value) + } +} + impl serde::Deserialize<'de>)> IntoResponse for JsonResponse { fn into_response(self) -> axum::response::Response { match self {