-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve organization, naming, and documentation
- Loading branch information
1 parent
4635133
commit 4ef6083
Showing
5 changed files
with
121 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use std::marker::PhantomData; | ||
|
||
use serde::{de::Visitor, Deserialize, Deserializer}; | ||
|
||
use crate::OperationResponse; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub(super) struct DummyEnvelope<T> | ||
where | ||
T: OperationResponse, | ||
{ | ||
#[serde(deserialize_with = "deserialize_body")] | ||
pub body: T, | ||
} | ||
|
||
fn deserialize_body<'de, D, T>(body: D) -> Result<T, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: OperationResponse, | ||
{ | ||
body.deserialize_map(BodyVisitor::<T>(PhantomData)) | ||
} | ||
|
||
/// A visitor for custom name-based deserialization of operation responses. | ||
struct BodyVisitor<T>(PhantomData<T>); | ||
|
||
impl<'de, T> Visitor<'de> for BodyVisitor<T> | ||
where | ||
T: OperationResponse, | ||
{ | ||
type Value = T; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("EWS operation response body") | ||
} | ||
|
||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: serde::de::MapAccess<'de>, | ||
{ | ||
match map.next_key::<String>()? { | ||
Some(name) => { | ||
// We expect the body of the response to contain a single | ||
// element with the name of the expected operation response. | ||
let expected = T::name(); | ||
if name.as_str() != expected { | ||
return Err(serde::de::Error::custom(format_args!( | ||
"unknown field `{}`, expected {}", | ||
name, expected | ||
))); | ||
} | ||
|
||
let value = map.next_value()?; | ||
|
||
// To satisfy quick-xml's serde impl, we need to consume the | ||
// final `None` key value in order to successfully complete. | ||
match map.next_key::<String>()? { | ||
Some(name) => { | ||
// The response body contained more than one element, | ||
// which violates our expectations. | ||
Err(serde::de::Error::custom(format_args!( | ||
"unexpected field `{}`", | ||
name | ||
))) | ||
} | ||
None => Ok(value), | ||
} | ||
} | ||
None => Err(serde::de::Error::invalid_type( | ||
serde::de::Unexpected::Map, | ||
&self, | ||
)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters