From e5fe0b6b59ec8d9a397eaa09707e5d6ffc972cc7 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Thu, 22 Dec 2022 12:29:13 -0500 Subject: [PATCH] Add event-logging example --- Cargo.toml | 1 + examples/log_events.rs | 37 +++++++++++++++++++++++++++++++++++++ examples/register/mod.rs | 26 ++++++++++++++------------ 3 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 examples/log_events.rs diff --git a/Cargo.toml b/Cargo.toml index 8e4eb07..44377f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ indoc = "1.0" pretty_env_logger = "0.3.0" skeptic = "0.13" tempfile = "3" +femme = "2.2.1" [build-dependencies.skeptic] version = "0.13" diff --git a/examples/log_events.rs b/examples/log_events.rs new file mode 100644 index 0000000..575af4a --- /dev/null +++ b/examples/log_events.rs @@ -0,0 +1,37 @@ +#![cfg_attr(not(feature = "toml"), allow(dead_code))] +#![cfg_attr(not(feature = "toml"), allow(unused_imports))] +mod register; + +use elefren::Result; +use futures_util::TryStreamExt; +use log::{as_serde, info}; + +#[cfg(feature = "toml")] +#[tokio::main] +async fn main() -> Result<()> { + use elefren::entities::prelude::Event; + use log::warn; + + femme::with_level(log::LevelFilter::Info); + let mastodon = register::get_mastodon_data().await?; + let stream = mastodon.stream_user().await?; + info!("watching mastodon for events. This will run forever, press Ctrl+C to kill the program."); + stream + .try_for_each(|event| async move { + match event { + // fill in how you want to handle events here. + _ => warn!(event = as_serde!(event); "unrecognized event received"), + } + Ok(()) + }) + .await?; + Ok(()) +} + +#[cfg(not(feature = "toml"))] +fn main() { + println!( + "examples require the `toml` feature, run this command for this example:\n\ncargo run \ + --example log_events --features toml\n" + ); +} diff --git a/examples/register/mod.rs b/examples/register/mod.rs index d8dbaff..7ac2356 100644 --- a/examples/register/mod.rs +++ b/examples/register/mod.rs @@ -3,47 +3,49 @@ pub use elefren::prelude::*; -use std::{error::Error, io}; +use std::io; -use elefren::helpers::cli; #[cfg(feature = "toml")] use elefren::helpers::toml; +use elefren::{helpers::cli, Result}; #[allow(dead_code)] #[cfg(feature = "toml")] -fn main() -> Result<(), Box> { - register()?; +#[tokio::main] +async fn main() -> Result<()> { + register().await?; Ok(()) } #[allow(dead_code)] #[cfg(feature = "toml")] -pub fn get_mastodon_data() -> Result> { +pub async fn get_mastodon_data() -> Result { if let Ok(data) = toml::from_file("mastodon-data.toml") { Ok(Mastodon::from(data)) } else { - register() + register().await } } #[cfg(feature = "toml")] -pub fn register() -> Result> { +pub async fn register() -> Result { let website = read_line("Please enter your mastodon instance url:")?; let registration = Registration::new(website.trim()) .client_name("elefren-examples") .scopes(Scopes::all()) - .website("https://github.com/pwoolcoc/elefren") - .build()?; - let mastodon = cli::authenticate(registration)?; + .website("https://github.com/dscottboggs/mastodon-async") + .build() + .await?; + let mastodon = cli::authenticate(registration).await?; // Save app data for using on the next run. - toml::to_file(&*mastodon, "mastodon-data.toml")?; + toml::to_file(&mastodon.data, "mastodon-data.toml")?; Ok(mastodon) } #[cfg(feature = "toml")] -pub fn read_line(message: &str) -> Result> { +pub fn read_line(message: &str) -> Result { println!("{}", message); let mut input = String::new();