use crate::runtime::Valence;
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::pin::Pin;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IterEvaluation {
pub should_run: bool,
pub reason: String,
}
impl IterEvaluation {
pub fn run(reason: impl Into<String>) -> Self {
Self {
should_run: true,
reason: reason.into(),
}
}
pub fn skip(reason: impl Into<String>) -> Self {
Self {
should_run: false,
reason: reason.into(),
}
}
}
pub type IterShouldRunFn =
fn(
Valence,
serde_json::Value,
) -> Pin<Box<dyn Future<Output = crate::error::Result<IterEvaluation>> + Send + 'static>>;
pub type IterExecuteFn =
fn(
Valence,
serde_json::Value,
) -> Pin<Box<dyn Future<Output = crate::error::Result<()>> + Send + 'static>>;
#[derive(Copy, Clone)]
pub struct IterDescriptor {
pub iter_type_name: &'static str,
pub table_name: &'static str,
pub should_run: IterShouldRunFn,
pub execute: IterExecuteFn,
}
inventory::collect!(IterDescriptor);
pub fn iter_descriptors() -> Vec<&'static IterDescriptor> {
inventory::iter::<IterDescriptor>.into_iter().collect()
}
pub fn find_iter_descriptor(
table_name: &str,
iter_type_name: &str,
) -> Option<&'static IterDescriptor> {
iter_descriptors()
.into_iter()
.find(|d| d.table_name == table_name && d.iter_type_name == iter_type_name)
}
pub fn iter_descriptors_for_table(table_name: &str) -> Vec<&'static IterDescriptor> {
iter_descriptors()
.into_iter()
.filter(|d| d.table_name == table_name)
.collect()
}