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 e41f52e commit f12d5e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 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
14 changes: 10 additions & 4 deletions rust-connector-sdk/src/json_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub enum JsonResponse<A> {
Serialized(Bytes),
}

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

impl<A: (for<'de> serde::Deserialize<'de>)> JsonResponse<A> {
/// Unwraps the value, deserializing if necessary.
///
Expand All @@ -25,8 +31,8 @@ impl<A: (for<'de> serde::Deserialize<'de>)> JsonResponse<A> {
self,
) -> Result<A, E> {
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)))
}
}
Expand All @@ -36,8 +42,8 @@ impl<A: (for<'de> serde::Deserialize<'de>)> JsonResponse<A> {
impl<A: serde::Serialize> IntoResponse for JsonResponse<A> {
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()),
Expand Down

0 comments on commit f12d5e1

Please sign in to comment.