142 lines
4.2 KiB
Rust
142 lines
4.2 KiB
Rust
use std::{fmt, marker::PhantomData};
|
|
|
|
use serde::{de::Visitor, Deserialize, Deserializer};
|
|
|
|
pub trait IriPartial: Default {
|
|
fn set_id(&mut self, new_id: &str);
|
|
}
|
|
|
|
// Allows a value that's a string to be expanded into an object (move string into id property via From<IRI>)
|
|
// AND the serialization of that object itself
|
|
pub(super) fn expand_partial<'de, D, Out>(deserializer: D) -> Result<Out, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
Out: for<'dt> serde::Deserialize<'dt> + IriPartial,
|
|
{
|
|
struct ResolveVisitor<Out> {
|
|
_type: PhantomData<Out>,
|
|
}
|
|
|
|
impl<'de, Out> Visitor<'de> for ResolveVisitor<Out>
|
|
where
|
|
Out: for<'dt> serde::Deserialize<'dt> + IriPartial,
|
|
{
|
|
type Value = Out;
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
formatter.write_str(std::any::type_name::<Out>())
|
|
}
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: serde::de::Error,
|
|
{
|
|
let mut out = Out::default();
|
|
out.set_id(value);
|
|
Ok(out)
|
|
}
|
|
|
|
fn visit_seq<S>(self, visitor: S) -> Result<Self::Value, S::Error>
|
|
where
|
|
S: serde::de::SeqAccess<'de>,
|
|
{
|
|
Deserialize::deserialize(serde::de::value::SeqAccessDeserializer::new(visitor))
|
|
}
|
|
}
|
|
|
|
let resolve_visitor = ResolveVisitor { _type: PhantomData };
|
|
deserializer.deserialize_map_string(resolve_visitor, |des| Out::deserialize(des))
|
|
}
|
|
|
|
// Allows deserialization of a single item into a vector of that item
|
|
// As long as they implement the From<String> trait
|
|
pub(super) fn pull_single_into_vec<'de, D, Out>(deserializer: D) -> Result<Vec<Out>, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
Out: From<String> + serde::Deserialize<'de>,
|
|
{
|
|
struct VecVisitor<Out>(PhantomData<Vec<Out>>);
|
|
|
|
impl<'de, Out> Visitor<'de> for VecVisitor<Out>
|
|
where
|
|
Out: From<String> + serde::Deserialize<'de>,
|
|
{
|
|
type Value = Vec<Out>;
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
let name = std::any::type_name::<Out>();
|
|
formatter.write_str(format!("{} or Vec<{}>", name, name).as_str())
|
|
}
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: serde::de::Error,
|
|
{
|
|
Ok(vec![value.to_string().into()])
|
|
}
|
|
|
|
fn visit_seq<S>(self, visitor: S) -> Result<Self::Value, S::Error>
|
|
where
|
|
S: serde::de::SeqAccess<'de>,
|
|
{
|
|
Deserialize::deserialize(serde::de::value::SeqAccessDeserializer::new(visitor))
|
|
}
|
|
}
|
|
|
|
deserializer.deserialize_any(VecVisitor(PhantomData))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use flabk_derive::IRI;
|
|
use serde::Deserialize;
|
|
|
|
use crate::astreams::serde_ext::IriPartial;
|
|
|
|
use super::expand_partial;
|
|
|
|
#[test]
|
|
fn expand_partial_populates_iri_from_string() {
|
|
#[derive(Deserialize, IRI, Default)]
|
|
struct Context {
|
|
pub id: String,
|
|
pub context: bool,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct WithContext {
|
|
#[serde(deserialize_with = "expand_partial")]
|
|
pub context: Context,
|
|
}
|
|
|
|
const JSONLD_INPUT: &str = r#"{"context": "https://www.w3.org/ns/activitystreams"}"#;
|
|
let result =
|
|
serde_json::from_str::<WithContext>(JSONLD_INPUT).expect("deserializing with expand");
|
|
|
|
assert!(result.context.id == "https://www.w3.org/ns/activitystreams");
|
|
assert!(result.context.context == false);
|
|
}
|
|
|
|
#[test]
|
|
fn expand_partial_expands_into_object_fully() {
|
|
#[derive(Deserialize, IRI, Default)]
|
|
struct Expanded {
|
|
pub id: String,
|
|
pub truth: bool,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Expandable {
|
|
#[serde(deserialize_with = "expand_partial")]
|
|
pub expansive: Expanded,
|
|
}
|
|
|
|
const JSONLD_INPUT: &str = r#"{"expansive": { "id": "1", "truth": true }}"#;
|
|
let result =
|
|
serde_json::from_str::<Expandable>(JSONLD_INPUT).expect("deserializing with expand");
|
|
|
|
assert!(result.expansive.id == "1");
|
|
assert!(result.expansive.truth);
|
|
}
|
|
}
|