Add parse example to readme/lib
This commit is contained in:
parent
32b4ce2eef
commit
7fc9708a7d
2 changed files with 92 additions and 2 deletions
47
README.md
47
README.md
|
@ -196,7 +196,7 @@ And this type would only deserialize for JSON where `"type":"Person"`
|
|||
|
||||
## Examples
|
||||
|
||||
### Basic
|
||||
### Create
|
||||
|
||||
```rust
|
||||
use activitystreams_new::{
|
||||
|
@ -232,6 +232,51 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
}
|
||||
```
|
||||
|
||||
### Parse
|
||||
|
||||
```rust
|
||||
use activitystreams_new::{activity::ActorAndObject, prelude::*};
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub enum AcceptedTypes {
|
||||
Accept,
|
||||
Announce,
|
||||
Create,
|
||||
Delete,
|
||||
Follow,
|
||||
Reject,
|
||||
Update,
|
||||
Undo,
|
||||
}
|
||||
|
||||
pub type AcceptedActivity = ActorAndObject<AcceptedTypes>;
|
||||
|
||||
pub fn handle_activity(activity: AcceptedActivity) -> Result<(), anyhow::Error> {
|
||||
println!("Actor: {:?}", activity.actor());
|
||||
println!("Object: {:?}", activity.object());
|
||||
|
||||
match activity.kind() {
|
||||
Some(AcceptedTypes::Accept) => println!("Accept"),
|
||||
Some(AcceptedTypes::Announce) => println!("Announce"),
|
||||
Some(AcceptedTypes::Create) => println!("Create"),
|
||||
Some(AcceptedTypes::Delete) => println!("Delete"),
|
||||
Some(AcceptedTypes::Follow) => println!("Follow"),
|
||||
Some(AcceptedTypes::Reject) => println!("Reject"),
|
||||
Some(AcceptedTypes::Update) => println!("Update"),
|
||||
Some(AcceptedTypes::Undo) => println!("Undo"),
|
||||
None => return Err(anyhow::Error::msg("No activity type provided")),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
static EXAMPLE_JSON: &str = r#"{"actor":"https://asonix.dog/users/asonix","object":"https://asonix.dog/users/asonix/posts/1","type":"Announce"}"#;
|
||||
|
||||
fn main() -> Result<(), anyhow::Error> {
|
||||
handle_activity(serde_json::from_str(EXAMPLE_JSON)?)
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.
|
||||
|
||||
|
|
47
src/lib.rs
47
src/lib.rs
|
@ -196,7 +196,7 @@
|
|||
//!
|
||||
//! ## Examples
|
||||
//!
|
||||
//! ### Basic
|
||||
//! ### Create
|
||||
//!
|
||||
//! ```rust
|
||||
//! use activitystreams_new::{
|
||||
|
@ -232,6 +232,51 @@
|
|||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Parse
|
||||
//!
|
||||
//! ```rust
|
||||
//! use activitystreams_new::{activity::ActorAndObject, prelude::*};
|
||||
//!
|
||||
//! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
//! pub enum AcceptedTypes {
|
||||
//! Accept,
|
||||
//! Announce,
|
||||
//! Create,
|
||||
//! Delete,
|
||||
//! Follow,
|
||||
//! Reject,
|
||||
//! Update,
|
||||
//! Undo,
|
||||
//! }
|
||||
//!
|
||||
//! pub type AcceptedActivity = ActorAndObject<AcceptedTypes>;
|
||||
//!
|
||||
//! pub fn handle_activity(activity: AcceptedActivity) -> Result<(), anyhow::Error> {
|
||||
//! println!("Actor: {:?}", activity.actor());
|
||||
//! println!("Object: {:?}", activity.object());
|
||||
//!
|
||||
//! match activity.kind() {
|
||||
//! Some(AcceptedTypes::Accept) => println!("Accept"),
|
||||
//! Some(AcceptedTypes::Announce) => println!("Announce"),
|
||||
//! Some(AcceptedTypes::Create) => println!("Create"),
|
||||
//! Some(AcceptedTypes::Delete) => println!("Delete"),
|
||||
//! Some(AcceptedTypes::Follow) => println!("Follow"),
|
||||
//! Some(AcceptedTypes::Reject) => println!("Reject"),
|
||||
//! Some(AcceptedTypes::Update) => println!("Update"),
|
||||
//! Some(AcceptedTypes::Undo) => println!("Undo"),
|
||||
//! None => return Err(anyhow::Error::msg("No activity type provided")),
|
||||
//! }
|
||||
//!
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! static EXAMPLE_JSON: &str = r#"{"actor":"https://asonix.dog/users/asonix","object":"https://asonix.dog/users/asonix/posts/1","type":"Announce"}"#;
|
||||
//!
|
||||
//! fn main() -> Result<(), anyhow::Error> {
|
||||
//! handle_activity(serde_json::from_str(EXAMPLE_JSON)?)
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ## Contributing
|
||||
//! Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.
|
||||
//!
|
||||
|
|
Loading…
Reference in a new issue