Accept single object as to for arrays too (#2048)

This commit is contained in:
vpzomtrrfrt 2022-01-20 06:12:45 -08:00 committed by GitHub
parent 19ccaf767c
commit 272dc3e7a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 0 deletions

View file

@ -112,6 +112,25 @@ where
}) })
} }
pub(crate) fn deserialize_one<'de, T, D>(deserializer: D) -> Result<[T; 1], D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum MaybeArray<T> {
Simple(T),
Array([T; 1]),
}
let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?;
Ok(match result {
MaybeArray::Simple(value) => [value],
MaybeArray::Array(value) => value,
})
}
pub enum EndpointType { pub enum EndpointType {
Community, Community,
Person, Person,

View file

@ -12,6 +12,7 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Report { pub struct Report {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubCommunity>; 1], pub(crate) to: [ObjectId<ApubCommunity>; 1],
pub(crate) object: ObjectId<PostOrComment>, pub(crate) object: ObjectId<PostOrComment>,
pub(crate) summary: String, pub(crate) summary: String,

View file

@ -11,6 +11,7 @@ use url::Url;
pub struct CreateOrUpdatePrivateMessage { pub struct CreateOrUpdatePrivateMessage {
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubPerson>; 1], pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) object: ChatMessage, pub(crate) object: ChatMessage,
#[serde(rename = "type")] #[serde(rename = "type")]

View file

@ -11,6 +11,7 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct DeletePrivateMessage { pub struct DeletePrivateMessage {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubPerson>; 1], pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) object: ObjectId<ApubPrivateMessage>, pub(crate) object: ObjectId<ApubPrivateMessage>,
#[serde(rename = "type")] #[serde(rename = "type")]

View file

@ -11,6 +11,7 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UndoDeletePrivateMessage { pub struct UndoDeletePrivateMessage {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubPerson>; 1], pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) object: DeletePrivateMessage, pub(crate) object: DeletePrivateMessage,
#[serde(rename = "type")] #[serde(rename = "type")]

View file

@ -14,6 +14,7 @@ pub struct ChatMessage {
pub(crate) r#type: ChatMessageType, pub(crate) r#type: ChatMessageType,
pub(crate) id: ObjectId<ApubPrivateMessage>, pub(crate) id: ObjectId<ApubPrivateMessage>,
pub(crate) attributed_to: ObjectId<ApubPerson>, pub(crate) attributed_to: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubPerson>; 1], pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) content: String, pub(crate) content: String,
pub(crate) media_type: Option<MediaTypeHtml>, pub(crate) media_type: Option<MediaTypeHtml>,