diff --git a/README.md b/README.md index 96d2fe7..55c7cee 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,15 @@ use elefren::prelude::*; use elefren::helpers::toml; // requires `features = ["toml"]` use elefren::helpers::cli; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") { Mastodon::from(data) } else { register()? }; - let you = mastodon.verify_credentials()?; + let you = mastodon.verify_credentials().await?; println!("{:#?}", you); @@ -80,25 +81,21 @@ use elefren::entities::event::Event; use std::error::Error; -fn main() -> Result<(), Box> { - let data = Data { - base: "".into(), - client_id: "".into(), - client_secret: "".into(), - redirect: "".into(), - token: "".into(), - }; +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Mastodon::from(Data::default()); - let client = Mastodon::from(data); - - for event in client.streaming_user()? { - match event { - Event::Update(ref status) => { /* .. */ }, - Event::Notification(ref notification) => { /* .. */ }, - Event::Delete(ref id) => { /* .. */ }, - Event::FiltersChanged => { /* .. */ }, - } - } + client.stream_user() + .await? + .try_for_each(|event| { + match event { + Event::Update(ref status) => { /* .. */ }, + Event::Notification(ref notification) => { /* .. */ }, + Event::Delete(ref id) => { /* .. */ }, + Event::FiltersChanged => { /* .. */ }, + } + }) + .await?; Ok(()) } ``` diff --git a/src/errors.rs b/src/errors.rs index 754d274..cbe85a0 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -12,7 +12,6 @@ use serde_urlencoded::ser::Error as UrlEncodedError; use tomlcrate::de::Error as TomlDeError; #[cfg(feature = "toml")] use tomlcrate::ser::Error as TomlSerError; -use tungstenite::{error::Error as WebSocketError, Message as WebSocketMessage}; use url::ParseError as UrlError; /// Convience type over `std::result::Result` with `Error` as the error type. @@ -62,16 +61,12 @@ pub enum Error { Envy(EnvyError), /// Error serializing to a query string SerdeQs(SerdeQsError), - /// WebSocket error - WebSocket(WebSocketError), /// An integer conversion was attempted, but the value didn't fit into the /// target type. /// /// At the time of writing, this can only be triggered when a file is /// larger than the system's usize allows. IntConversion(TryFromIntError), - /// A stream message was received that wasn't recognized - UnrecognizedStreamMessage(WebSocketMessage), /// Other errors Other(String), } @@ -100,14 +95,12 @@ impl error::Error for Error { #[cfg(feature = "env")] Error::Envy(ref e) => e, Error::SerdeQs(ref e) => e, - Error::WebSocket(ref e) => e, Error::IntConversion(ref e) => e, Error::Client(..) | Error::Server(..) => return None, Error::ClientIdRequired => return None, Error::ClientSecretRequired => return None, Error::AccessTokenRequired => return None, Error::MissingField(_) => return None, - Error::UnrecognizedStreamMessage(_) => return None, Error::Other(..) => return None, }) } @@ -157,10 +150,8 @@ from! { HeaderParseError => HeaderParseError, #[cfg(feature = "env")] EnvyError => Envy, SerdeQsError => SerdeQs, - WebSocketError => WebSocket, String => Other, TryFromIntError => IntConversion, - WebSocketMessage => UnrecognizedStreamMessage, } #[macro_export] diff --git a/src/event_stream.rs b/src/event_stream.rs index 963025d..9f5c11d 100644 --- a/src/event_stream.rs +++ b/src/event_stream.rs @@ -14,7 +14,7 @@ use tokio_util::io::StreamReader; /// Return a stream of events from the given response by parsing Server-Sent /// Events as they come in. /// -/// See https://docs.joinmastodon.org/methods/streaming/ for more info +/// See for more info pub fn event_stream( response: Response, location: String, @@ -28,14 +28,14 @@ pub fn event_stream( let (ref mut lines_iter, ref location) = this; let mut lines = vec![]; while let Some(line) = lines_iter.next_line().await? { - debug!(message = line, location = &location; "received websocket message"); + debug!(message = line, location = &location; "received message"); let line = line.trim().to_string(); if line.starts_with(":") || line.is_empty() { continue; } lines.push(line); if let Ok(event) = make_event(&lines) { - info!(event = as_serde!(event), location = location; "received websocket event"); + info!(event = as_serde!(event), location = location; "received event"); lines.clear(); return Ok(Some((event, this))); } else { @@ -66,7 +66,7 @@ fn make_event(lines: &[String]) -> Result { data = message.payload; } let event: &str = &event; - trace!(event = event, payload = data; "websocket message parsed"); + trace!(event = event, payload = data; "SSE message parsed"); Ok(match event { "notification" => { let data = data diff --git a/src/lib.rs b/src/lib.rs index b778e96..0ada43a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ //! let data = Data::default(); //! let client = Mastodon::from(data); //! tokio_test::block_on(async { -//! let stream = client.streaming_user().await.unwrap(); +//! let stream = client.stream_user().await.unwrap(); //! stream.try_for_each(|event| async move { //! match event { //! Event::Update(ref status) => { /* .. */ },