mirror of
https://github.com/girlbossceo/ruwuma.git
synced 2025-04-29 06:49:48 -04:00
move or_empty deserializer to ruma-common, add tests
Signed-off-by: morguldir <morguldir@protonmail.com>
This commit is contained in:
parent
6b24abc2d1
commit
d2e1b510a0
3 changed files with 49 additions and 15 deletions
|
@ -26,27 +26,13 @@ pub mod v3 {
|
|||
1.1 => "/_matrix/client/v3/keys/device_signing/upload",
|
||||
}
|
||||
};
|
||||
|
||||
fn or_empty<'de, D: serde::Deserializer<'de>, T: serde::Deserialize<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<T>, D::Error> {
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum OrEmpty<T> {
|
||||
NotEmpty(T),
|
||||
Empty {},
|
||||
}
|
||||
let res = <Option<OrEmpty<T>> as serde::Deserialize<'de>>::deserialize(deserializer)?;
|
||||
Ok(res.and_then(|res| if let OrEmpty::NotEmpty(a) = res { Some(a) } else { None }))
|
||||
}
|
||||
|
||||
/// Request type for the `upload_signing_keys` endpoint.
|
||||
#[request(error = UiaaResponse)]
|
||||
#[derive(Default)]
|
||||
pub struct Request {
|
||||
/// Additional authentication information for the user-interactive authentication API.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(deserialize_with = "or_empty")]
|
||||
#[serde(deserialize_with = "ruma_common::serde::or_empty")]
|
||||
#[serde(default)]
|
||||
pub auth: Option<AuthData>,
|
||||
|
||||
|
|
|
@ -729,3 +729,38 @@ impl OutgoingResponse for UiaaResponse {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use assert_matches2::assert_matches;
|
||||
use serde;
|
||||
|
||||
#[test]
|
||||
fn test_empty_uiaa_serialization() {
|
||||
let input = "{}";
|
||||
let result = serde_json::from_str::<super::AuthData>(input);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde()]
|
||||
struct Request {
|
||||
/// Additional authentication information for the user-interactive authentication API.
|
||||
#[serde(deserialize_with = "ruma_common::serde::or_empty")]
|
||||
pub auth: Option<super::AuthData>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_option_uiaa_serialization() {
|
||||
let input = r#"{"auth": {}}"#;
|
||||
let result = serde_json::from_str::<Request>(input).unwrap();
|
||||
assert_matches!(result.auth, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fail_uiaa_serialization() {
|
||||
let input = r#"{"auth": "aaw"}"#;
|
||||
let result = serde_json::from_str::<Request>(input);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,6 +69,19 @@ pub fn is_true(b: &bool) -> bool {
|
|||
*b
|
||||
}
|
||||
|
||||
/// Returns None if the serialization fails
|
||||
pub fn or_empty<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<T>, D::Error> {
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum OrEmpty<T> {
|
||||
NotEmpty(T),
|
||||
Empty {},
|
||||
}
|
||||
let res = <Option<OrEmpty<T>> as Deserialize<'de>>::deserialize(deserializer)?;
|
||||
Ok(res.and_then(|res| if let OrEmpty::NotEmpty(a) = res { Some(a) } else { None }))
|
||||
}
|
||||
/// Helper function for `serde_json::value::RawValue` deserialization.
|
||||
#[inline(never)]
|
||||
pub fn from_raw_json_value<'a, T, E>(val: &'a RawJsonValue) -> Result<T, E>
|
||||
|
|
Loading…
Add table
Reference in a new issue