Context doesnt have to be an array (#90)

This commit is contained in:
Nutomic 2023-12-20 11:21:33 +01:00 committed by GitHub
parent fec0af2406
commit 709f29b7f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,11 +15,11 @@
//! }; //! };
//! let note_with_context = WithContext::new_default(note); //! let note_with_context = WithContext::new_default(note);
//! let serialized = serde_json::to_string(&note_with_context)?; //! let serialized = serde_json::to_string(&note_with_context)?;
//! assert_eq!(serialized, r#"{"@context":["https://www.w3.org/ns/activitystreams"],"content":"Hello world"}"#); //! assert_eq!(serialized, r#"{"@context":"https://www.w3.org/ns/activitystreams","content":"Hello world"}"#);
//! Ok::<(), serde_json::error::Error>(()) //! Ok::<(), serde_json::error::Error>(())
//! ``` //! ```
use crate::{config::Data, protocol::helpers::deserialize_one_or_many, traits::ActivityHandler}; use crate::{config::Data, traits::ActivityHandler};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use url::Url; use url::Url;
@ -31,8 +31,7 @@ const DEFAULT_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct WithContext<T> { pub struct WithContext<T> {
#[serde(rename = "@context")] #[serde(rename = "@context")]
#[serde(deserialize_with = "deserialize_one_or_many")] context: Value,
context: Vec<Value>,
#[serde(flatten)] #[serde(flatten)]
inner: T, inner: T,
} }
@ -40,12 +39,12 @@ pub struct WithContext<T> {
impl<T> WithContext<T> { impl<T> WithContext<T> {
/// Create a new wrapper with the default Activitypub context. /// Create a new wrapper with the default Activitypub context.
pub fn new_default(inner: T) -> WithContext<T> { pub fn new_default(inner: T) -> WithContext<T> {
let context = vec![Value::String(DEFAULT_CONTEXT.to_string())]; let context = Value::String(DEFAULT_CONTEXT.to_string());
WithContext::new(inner, context) WithContext::new(inner, context)
} }
/// Create new wrapper with custom context. Use this in case you are implementing extensions. /// Create new wrapper with custom context. Use this in case you are implementing extensions.
pub fn new(inner: T, context: Vec<Value>) -> WithContext<T> { pub fn new(inner: T, context: Value) -> WithContext<T> {
WithContext { context, inner } WithContext { context, inner }
} }