serde: Add Raw::get_field

This commit is contained in:
Jonas Platte 2021-05-05 11:22:01 +02:00
parent 448e29ce24
commit cc455057f7
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 84 additions and 2 deletions

View File

@ -1,6 +1,23 @@
use std::{borrow::Cow, str};
use serde::de::{self, Deserializer, Unexpected, Visitor};
use serde::de::{self, Deserialize, Deserializer, Unexpected, Visitor};
pub(crate) struct MyCowStr<'a>(Cow<'a, str>);
impl<'a> MyCowStr<'a> {
pub(crate) fn get(self) -> Cow<'a, str> {
self.0
}
}
impl<'de> Deserialize<'de> for MyCowStr<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_cow_str(deserializer).map(Self)
}
}
/// Deserialize a `Cow<'de, str>`.
///

View File

@ -5,11 +5,13 @@ use std::{
};
use serde::{
de::{Deserialize, DeserializeOwned, Deserializer},
de::{Deserialize, DeserializeOwned, Deserializer, IgnoredAny, MapAccess, Visitor},
ser::{Serialize, Serializer},
};
use serde_json::value::RawValue;
use crate::cow::MyCowStr;
/// A wrapper around `Box<RawValue>`, to be used in place of any type in the Matrix endpoint
/// definition to allow request and response types to contain that said type represented by
/// the generic argument `Ev`.
@ -52,6 +54,69 @@ impl<T> Raw<T> {
pub fn into_json(self) -> Box<RawValue> {
self.json
}
/// Try to access a given field inside this `Raw`, assuming it contains an
/// object.
///
/// Returns `Err(_)` when the contained value is not an object, or the field
/// exists but is fails to deserialize to the expected type.
///
/// Returns `Ok(None)` when the field doesn't exist or is `null`.
///
/// # Example
///
/// ```no_run
/// # let raw_event: ruma_serde::Raw = todo!();
/// if raw_event.get_field::<String>("type")? == "org.custom.matrix.event" {
/// let event: CustomMatrixEvent = serde_json::from_str(raw_event.json().get())?;
/// // ...
/// }
/// ```
pub fn get_field<'a, U>(&'a self, field_name: &str) -> serde_json::Result<Option<U>>
where
U: Deserialize<'a>,
{
struct SingleFieldVisitor<'b, T> {
field_name: &'b str,
_phantom: PhantomData<T>,
}
impl<'b, T> SingleFieldVisitor<'b, T> {
fn new(field_name: &'b str) -> Self {
Self { field_name, _phantom: PhantomData }
}
}
impl<'b, 'de, T> Visitor<'de> for SingleFieldVisitor<'b, T>
where
T: Deserialize<'de>,
{
type Value = Option<T>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut res = None;
while let Some(key) = map.next_key::<MyCowStr>()? {
if key.get() == self.field_name {
res = Some(map.next_value()?);
} else {
map.next_value::<IgnoredAny>()?;
}
}
Ok(res)
}
}
let mut deserializer = serde_json::Deserializer::from_str(self.json().get());
deserializer.deserialize_map(SingleFieldVisitor::new(field_name))
}
}
impl<T> Raw<T>