use activitystreams::{ actor::{ApActor, Person}, context, iri_string::types::IriString, prelude::*, security, unparsed::UnparsedMutExt, }; use activitystreams_ext::{Ext1, UnparsedExtension}; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct PublicKey { public_key: PublicKeyInner, } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct PublicKeyInner { id: IriString, owner: IriString, public_key_pem: String, } impl UnparsedExtension for PublicKey where U: UnparsedMutExt, { type Error = serde_json::Error; fn try_from_unparsed(unparsed_mut: &mut U) -> Result { Ok(PublicKey { public_key: unparsed_mut.remove("publicKey")?, }) } fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> { unparsed_mut.insert("publicKey", self.public_key)?; Ok(()) } } pub type ExtendedPerson = Ext1, PublicKey>; fn main() -> Result<(), anyhow::Error> { let actor = ApActor::new("http://in.box".parse()?, Person::new()); let mut person = Ext1::new( actor, PublicKey { public_key: PublicKeyInner { id: "http://key.id".parse()?, owner: "http://owner.id".parse()?, public_key_pem: "asdfasdfasdf".to_owned(), }, }, ); person.set_context(context()).add_context(security()); let any_base = person.into_any_base()?; println!("any_base: {:#?}", any_base); let person = ExtendedPerson::from_any_base(any_base)?; println!("person: {:#?}", person); Ok(()) }