use async_trait::async_trait;
use tokio::sync::watch;
use crate::token_source::{TokenSource, TokenSourceError};
pub struct StaticTokenSource {
token: String,
watch_tx: watch::Sender<Option<Result<String, TokenSourceError>>>,
}
impl From<String> for StaticTokenSource {
fn from(token: String) -> Self {
let (watch_tx, _watch_rx) = watch::channel(Some(Ok(token.clone())));
StaticTokenSource { token, watch_tx }
}
}
impl From<&'static str> for StaticTokenSource {
fn from(token: &'static str) -> Self {
let (watch_tx, _watch_rx) = watch::channel(Some(Ok(token.to_string())));
StaticTokenSource {
token: token.to_string(),
watch_tx,
}
}
}
#[async_trait]
impl TokenSource for StaticTokenSource {
async fn get_token(&self) -> Result<String, TokenSourceError> {
Ok(self.token.clone())
}
fn watch(&self) -> watch::Receiver<Option<Result<String, TokenSourceError>>> {
self.watch_tx.subscribe()
}
}