Skip to content

Commit

Permalink
Reuse JsonResponse helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 16, 2024
1 parent 584a56c commit 5b7b000
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/api/image_array/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
COLOUR_AXIS, IMAGE_BYTES_TYPE,
};
use crate::api::TransmissionElementType;
use crate::client::{Response, ResponseTransaction, ResponseWithTransaction};
use crate::client::{JsonResponse, Response, ResponseTransaction, ResponseWithTransaction};
use crate::{ASCOMError, ASCOMErrorCode, ASCOMResult};
use bytemuck::PodCastError;
use mime::Mime;
Expand Down Expand Up @@ -79,9 +79,7 @@ impl<'de> Visitor<'de> for ResponseVisitor {
}
}

struct JsonImageArray(ImageArray);

impl<'de> Deserialize<'de> for JsonImageArray {
impl<'de> Deserialize<'de> for JsonResponse<ImageArray> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_map(ResponseVisitor).map(Self)
}
Expand All @@ -102,7 +100,7 @@ impl Response for ASCOMResult<ImageArray> {

fn from_reqwest(mime_type: Mime, bytes: &[u8]) -> eyre::Result<ResponseWithTransaction<Self>> {
if mime_type.essence_str() != IMAGE_BYTES_TYPE {
return <ASCOMResult<JsonImageArray>>::from_reqwest(mime_type, bytes)
return <ASCOMResult<JsonResponse<_>>>::from_reqwest(mime_type, bytes)
.map(|response| response.map(|response| response.map(|json| json.0)));
}
let metadata = bytes
Expand Down
2 changes: 1 addition & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod params;
pub(crate) use params::{ActionParams, Method};

mod response;
pub(crate) use response::Response;
pub(crate) use response::{JsonResponse, Response};

use crate::api::{ConfiguredDevice, DevicePath, FallibleDeviceType, ServerInfo, TypedDevice};
use crate::response::ValueResponse;
Expand Down
4 changes: 3 additions & 1 deletion src/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ pub(crate) trait Response: Sized {
fn from_reqwest(mime_type: Mime, bytes: &[u8]) -> eyre::Result<ResponseWithTransaction<Self>>;
}

struct JsonResponse<T>(T);
// This wrapper exists solely to work around Rust's lack of specialization (for now).
// It allows to implement traits that can't be implemented on e.g. `ASCOMResult<T>` because `Result` is a built-in type.
pub(crate) struct JsonResponse<T>(pub(crate) T);

impl<T: FromJsonBytes> Response for JsonResponse<T> {
fn from_reqwest(mime_type: Mime, bytes: &[u8]) -> eyre::Result<ResponseWithTransaction<Self>> {
Expand Down

0 comments on commit 5b7b000

Please sign in to comment.