diff options
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/mattermost.rs | 10 |
4 files changed, 32 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock index f17306e..0588ad8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" [[package]] +name = "async-trait" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d1d8ab452a3936018a687b20e6f7cf5363d713b732b8884001317b0e48aa3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "async-tungstenite" version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1200,6 +1211,7 @@ name = "vavbot" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "async-tungstenite", "futures-util", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 855f0bb..43bc0e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] anyhow = "1.0.68" +async-trait = "0.1.60" async-tungstenite = { version = "*", features = ["tokio-runtime","tokio-native-tls"] } futures-util = { version = "0.3.25", features = ["tokio-io"] } reqwest = { version = "0.11.13", features = ["serde_json", "json"] } diff --git a/src/main.rs b/src/main.rs index d4f30c4..e2a9028 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,17 @@ mod mattermost; use tokio::{self}; use tracing::{debug, info}; -#[tokio::main(worker_threads = 2)] +struct Vav{ +} +#[async_trait::async_trait] +impl mattermost::Handler for Vav{ + async fn handle(&self,message:serde_json::Value,client: & mattermost::Client) { + debug!("{message:?}"); + } +} + +#[tokio::main(worker_threads = 2)] async fn main() -> Result<(), Box<dyn std::error::Error>> { tracing_subscriber::fmt::init(); let login = std::env::var("USER_MAIL")?; @@ -12,6 +21,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { let auth = mattermost::AuthData { login, password }; let mut client = mattermost::Client::new(auth, "https://mattermost.continuum.ii.uni.wroc.pl"); client.update_bearer_token().await?; - client.handle_websocket_stream().await?; + client.handle_websocket_stream(Vav{}).await?; Ok(()) } diff --git a/src/mattermost.rs b/src/mattermost.rs index a062887..9b811d1 100644 --- a/src/mattermost.rs +++ b/src/mattermost.rs @@ -13,6 +13,12 @@ pub struct Client { bearer_token: Option<String>, client: reqwest::Client, } + +#[async_trait::async_trait] +pub trait Handler { + async fn handle(&self, message:serde_json::Value,client: &Client); +} + impl Client { pub(crate) fn new(auth: AuthData, url: &str) -> Self { Self { @@ -42,7 +48,7 @@ impl Client { debug!("Successfully updated bearer token"); Ok(()) } - pub(crate) async fn handle_websocket_stream(&self) -> Result<(), anyhow::Error> { + pub(crate) async fn handle_websocket_stream<T:Handler>(&mut self, handler: T) -> Result<(), anyhow::Error> { let url = format!("{}/api/v4/websocket", self.url.replacen("http", "ws", 1)); let (mut ws_stream, _) = connect_async(url).await?; let token = self @@ -61,7 +67,7 @@ impl Client { Message::Text(text) => { let json: Result<serde_json::Value, _> = serde_json::from_str(&text); match json { - Ok(json) => debug!("Got json {json}"), + Ok(json) => {debug!("Got json {json}");handler.handle(json,&self).await;}, Err(err) => warn!("Error while deserializing {err}"), } } |