Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Created September 18, 2022 23:48
Show Gist options
  • Save jayhuang75/372805eddcd86306ae86c72643e47fd1 to your computer and use it in GitHub Desktop.
Save jayhuang75/372805eddcd86306ae86c72643e47fd1 to your computer and use it in GitHub Desktop.
rust_serverless_trading_view_slack_alert
use async_trait::async_trait;
use core::fmt::Debug;
use crate::error::AppError;
pub mod slack;
#[async_trait(?Send)]
pub trait Producer {
async fn run(&self, msg: &str) -> Result<(), AppError>;
}
impl Debug for dyn Producer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Producer{{{:?}}}", self)
}
}
#[derive(Debug)]
pub struct AppAlert {
// any type inside a Box must implement the Producer trait.
producer: Box<dyn Producer>,
}
impl AppAlert {
pub async fn producer(&self, msg: &str) -> Result<(), AppError> {
self.producer.run(msg).await
}
pub fn new(producer: Box<dyn Producer>) -> Self {
AppAlert { producer }
}
}
#[cfg(test)]
mod tests {
use super::*;
use slack::Slack;
use dotenv::dotenv;
#[tokio::test]
async fn app_alert() {
dotenv().ok();
//***********************
// Configuration Trading View Alert - Slack
// calling example : trading_view_alert_slack.producer("hello from bot").await;
//***********************
let trading_view_alert_slack = AppAlert::new(Box::new(Slack {
webhook_url: "test_url".into(),
client: reqwest::Client::new(),
}));
let res = trading_view_alert_slack.producer("msg").await;
let _ = match res {
Ok(()) => unimplemented!(),
Err(error) => {
assert_eq!(
error.to_string(),
"builder error: relative URL without a base"
)
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment