1use crate::runtime::Valence;
4use serde::{Deserialize, Serialize};
5use std::future::Future;
6use std::pin::Pin;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct IterEvaluation {
11 pub should_run: bool,
12 pub reason: String,
13}
14
15impl IterEvaluation {
16 pub fn run(reason: impl Into<String>) -> Self {
17 Self {
18 should_run: true,
19 reason: reason.into(),
20 }
21 }
22
23 pub fn skip(reason: impl Into<String>) -> Self {
24 Self {
25 should_run: false,
26 reason: reason.into(),
27 }
28 }
29}
30
31pub type IterShouldRunFn =
33 fn(
34 Valence,
35 serde_json::Value,
36 ) -> Pin<Box<dyn Future<Output = crate::error::Result<IterEvaluation>> + Send + 'static>>;
37
38pub type IterExecuteFn =
40 fn(
41 Valence,
42 serde_json::Value,
43 ) -> Pin<Box<dyn Future<Output = crate::error::Result<()>> + Send + 'static>>;
44
45#[derive(Copy, Clone)]
47pub struct IterDescriptor {
48 pub iter_type_name: &'static str,
49 pub table_name: &'static str,
50 pub should_run: IterShouldRunFn,
51 pub execute: IterExecuteFn,
52}
53
54inventory::collect!(IterDescriptor);
55
56pub fn iter_descriptors() -> Vec<&'static IterDescriptor> {
58 inventory::iter::<IterDescriptor>.into_iter().collect()
59}
60
61pub fn find_iter_descriptor(
63 table_name: &str,
64 iter_type_name: &str,
65) -> Option<&'static IterDescriptor> {
66 iter_descriptors()
67 .into_iter()
68 .find(|d| d.table_name == table_name && d.iter_type_name == iter_type_name)
69}
70
71pub fn iter_descriptors_for_table(table_name: &str) -> Vec<&'static IterDescriptor> {
73 iter_descriptors()
74 .into_iter()
75 .filter(|d| d.table_name == table_name)
76 .collect()
77}