activitystreams/examples/de.rs

69 lines
2.3 KiB
Rust
Raw Normal View History

use activitystreams::{
2020-03-18 23:51:04 +00:00
collection::{properties::CollectionProperties, OrderedCollection},
2020-04-02 14:41:06 +00:00
ext::Ext,
object::{properties::ApObjectProperties, ObjectBox, Page},
};
use anyhow::Error;
fn main() -> Result<(), Error> {
let collection_json = r#"{
"type": "OrderedCollection",
"id": "http://lemmy_alpha:8540/federation/c/main",
"context": "https://www.w3.org/ns/activitystreams",
"items": [
{
"type": "Page",
"id": "http://lemmy_alpha:8540/federation/post/2",
"attributedTo": "http://lemmy_alpha:8540/federation/u/2",
"content": "test",
"context": "https://www.w3.org/ns/activitystreams",
"name": "test",
"published": "2020-03-13T00:14:41.188634+00:00"
},
{
"type": "Page",
"id": "http://lemmy_alpha:8540/federation/post/1",
"attributedTo": "http://lemmy_alpha:8540/federation/u/2",
"context": "https://www.w3.org/ns/activitystreams",
"name": "test",
"published": "2020-03-13T00:13:56.311479+00:00"
}
],
"totalItems": 2
}"#;
let page_json = r#"{
"type": "Page",
"id": "http://lemmy_alpha:8540/federation/post/2",
"attributedTo": "http://lemmy_alpha:8540/federation/u/2",
"content": "test",
"name": "test",
"published": "2020-03-13T00:14:41.188634+00:00"
}"#;
2020-04-02 14:41:06 +00:00
let page: Ext<Page, ApObjectProperties> = serde_json::from_str(page_json)?;
println!("{:#?}", page);
2020-03-13 20:57:11 +00:00
let obox = ObjectBox::from_concrete(page)?;
println!("{:#?}", obox);
let obox_string = serde_json::to_string(&obox)?;
println!("{}", obox_string);
let obox: ObjectBox = serde_json::from_str(&obox_string)?;
println!("{:#?}", obox);
2020-04-02 14:41:06 +00:00
let mut collection: OrderedCollection = serde_json::from_str(collection_json)?;
println!("{:#?}", collection);
2020-03-14 20:27:43 +00:00
let cprops: &CollectionProperties = collection.as_ref();
2020-04-02 14:41:06 +00:00
let v: Vec<Ext<Page, ApObjectProperties>> = cprops
2020-04-02 16:20:17 +00:00
.get_many_items_base_boxes()
2020-03-14 20:27:43 +00:00
.unwrap()
2020-04-26 01:25:02 +00:00
.map(|base_box| base_box.clone().into_concrete())
2020-03-14 20:27:43 +00:00
.collect::<Result<Vec<_>, std::io::Error>>()?;
2020-04-02 14:41:06 +00:00
let cprops: &mut CollectionProperties = collection.as_mut();
2020-04-02 16:20:17 +00:00
cprops.set_many_items_base_boxes(v.clone())?;
2020-04-02 14:41:06 +00:00
2020-03-14 20:27:43 +00:00
println!("{:#?}", v);
Ok(())
}