Skip to content

Commit

Permalink
Fix response parsing for ()
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 25, 2024
1 parent 7122db8 commit 9ed7cb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::client::ResponseTransaction;
use crate::response::ValueResponse;
use crate::{ASCOMError, ASCOMErrorCode, ASCOMResult};
use mime::Mime;
use serde::de::value::UnitDeserializer;
use serde::de::DeserializeOwned;
use std::any::TypeId;

pub(crate) trait Response: Sized {
fn prepare_reqwest(request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
Expand All @@ -30,15 +32,21 @@ impl ResponseTransaction {
}
}

impl<T: DeserializeOwned> Response for ASCOMResult<T> {
impl<T: 'static + DeserializeOwned> Response for ASCOMResult<T> {
fn from_reqwest(mime_type: Mime, bytes: &[u8]) -> eyre::Result<ResponseWithTransaction<Self>> {
let transaction = ResponseTransaction::from_reqwest(mime_type, bytes)?;
let ascom_error = serde_json::from_slice::<ASCOMError>(bytes)?;

Ok(ResponseWithTransaction {
transaction,
response: match ascom_error.code {
ASCOMErrorCode::OK => Ok(serde_json::from_slice::<ValueResponse<T>>(bytes)?.value),
ASCOMErrorCode::OK => Ok(if TypeId::of::<T>() == TypeId::of::<()>() {
// Specialization: avoid failure when trying to parse `()` from JSON object with no `Value`.
T::deserialize(UnitDeserializer::new())
} else {
serde_json::from_slice::<ValueResponse<T>>(bytes)
.map(|value_response| value_response.value)
}?),
_ => Err(ascom_error),
},
})
Expand Down
5 changes: 3 additions & 2 deletions src/server/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use http::{Method, StatusCode};
use indexmap::IndexMap;
use serde::de::{DeserializeOwned, IntoDeserializer};
use serde::Deserialize;
use std::any::TypeId;
use std::fmt::Debug;
use std::hash::Hash;

Expand Down Expand Up @@ -39,11 +40,11 @@ where
.swap_remove(name.as_ref())
.map(|mut value| {
// Specialization: optimized path to avoid cloning the string.
if std::any::TypeId::of::<T>() == std::any::TypeId::of::<String>() {
if TypeId::of::<T>() == TypeId::of::<String>() {
return T::deserialize(value.into_deserializer());
}
// Specialization: booleans in ASCOM must be case-insensitive.
if std::any::TypeId::of::<T>() == std::any::TypeId::of::<bool>() {
if TypeId::of::<T>() == TypeId::of::<bool>() {
value.make_ascii_lowercase();
}
serde_plain::from_str(&value)
Expand Down

0 comments on commit 9ed7cb2

Please sign in to comment.