Skip to content

Commit

Permalink
Add impl From<A> for JsonResponse<A>.
Browse files Browse the repository at this point in the history
Co-Authored-By: Jesse Hallett <[email protected]>
  • Loading branch information
SamirTalwar and hallettj committed Oct 10, 2023
1 parent bbf7751 commit 481a09c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions rust-connector-sdk/src/connector/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Connector for Example {
}

async fn get_capabilities() -> JsonResponse<models::CapabilitiesResponse> {
JsonResponse::Value(models::CapabilitiesResponse {
models::CapabilitiesResponse {
versions: "^0.1.0".into(),
capabilities: models::Capabilities {
explain: None,
Expand All @@ -63,7 +63,8 @@ impl Connector for Example {
relation_comparisons: None,
}),
},
})
}
.into()
}

async fn get_schema(
Expand All @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion rust-connector-sdk/src/json_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<A: serde::Serialize + (for<'de> serde::Deserialize<'de>)> {
pub enum JsonResponse<A> {
/// A value that can be serialized to JSON.
Value(A),
/// A serialized JSON bytestring that is assumed to represent a value of
Expand All @@ -33,6 +33,12 @@ impl<A: serde::Serialize + (for<'de> serde::Deserialize<'de>)> JsonResponse<A> {
}
}

impl<A> From<A> for JsonResponse<A> {
fn from(value: A) -> Self {
JsonResponse::Value(value)
}
}

impl<A: serde::Serialize + (for<'de> serde::Deserialize<'de>)> IntoResponse for JsonResponse<A> {
fn into_response(self) -> axum::response::Response {
match self {
Expand Down

0 comments on commit 481a09c

Please sign in to comment.