Skip to main content

valence_core/
iter.rs

1//! Row-level iter descriptor types for `valence_schema! { iters: [...] }`.
2
3use crate::runtime::Valence;
4use serde::{Deserialize, Serialize};
5use std::future::Future;
6use std::pin::Pin;
7
8/// Result of an iter `should_run` hook.
9#[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
31/// Type-erased `should_run` for orchestration (row as JSON).
32pub type IterShouldRunFn =
33    fn(
34        Valence,
35        serde_json::Value,
36    ) -> Pin<Box<dyn Future<Output = anyhow::Result<IterEvaluation>> + Send + 'static>>;
37
38/// Type-erased `execute` for orchestration (row as JSON).
39pub type IterExecuteFn = fn(
40    Valence,
41    serde_json::Value,
42) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + 'static>>;
43
44/// One registered iter implementation for a table (submitted by `valence_schema!`).
45#[derive(Copy, Clone)]
46pub struct IterDescriptor {
47    pub iter_type_name: &'static str,
48    pub table_name: &'static str,
49    pub should_run: IterShouldRunFn,
50    pub execute: IterExecuteFn,
51}
52
53inventory::collect!(IterDescriptor);