All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
46 lines
937 B
Rust
46 lines
937 B
Rust
use std::sync::Arc;
|
|
|
|
#[async_trait::async_trait]
|
|
pub trait Task {
|
|
async fn id(&self) -> anyhow::Result<String>;
|
|
async fn should_run(&self) -> anyhow::Result<bool> {
|
|
Ok(true)
|
|
}
|
|
async fn execute(&self) -> anyhow::Result<()>;
|
|
}
|
|
|
|
pub trait IntoTask {
|
|
fn into_task(self) -> ConcreteTask;
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ConcreteTask {
|
|
inner: Arc<dyn Task + Sync + Send + 'static>,
|
|
}
|
|
|
|
impl ConcreteTask {
|
|
pub fn new<T: Task + Sync + Send + 'static>(t: T) -> Self {
|
|
Self { inner: Arc::new(t) }
|
|
}
|
|
}
|
|
|
|
impl std::ops::Deref for ConcreteTask {
|
|
type Target = Arc<dyn Task + Sync + Send + 'static>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.inner
|
|
}
|
|
}
|
|
|
|
impl IntoTask for ConcreteTask {
|
|
fn into_task(self) -> ConcreteTask {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<T: Task + Sync + Send + 'static> IntoTask for T {
|
|
fn into_task(self) -> ConcreteTask {
|
|
ConcreteTask::new(self)
|
|
}
|
|
}
|