Skip to main content

chronon_executor/
descriptor.rs

1//! Script descriptor for auto-registration via inventory.
2
3use std::future::Future;
4use std::pin::Pin;
5
6use chronon_core::{Result, ScriptContext};
7use serde_json::Value;
8
9/// Type alias for the script invocation function.
10///
11/// Registered via [`ScriptDescriptor`] and called by [`crate::execute_script`] after
12/// context build; must be `Send` because runs execute on the tokio runtime.
13pub type InvokeFn = fn(
14    Box<dyn ScriptContext>,
15    Value,
16) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>;
17
18/// Descriptor for a registered script.
19///
20/// Collected at link time by `quark::inventory` for `#[chronon::script]` handlers or
21/// built manually in tests via [`Self::new`].
22pub struct ScriptDescriptor {
23    /// Unique script name.
24    pub name: &'static str,
25    /// Function to invoke the script with deserialized parameters.
26    pub invoke: InvokeFn,
27    /// JSON schema for parameters (computed at compile time by `chronon-macros`).
28    pub signature_json: &'static str,
29    /// Hash of the signature for version checking.
30    pub signature_hash: u64,
31}
32
33impl ScriptDescriptor {
34    /// Create a descriptor with placeholder signature metadata.
35    pub const fn new(name: &'static str, invoke: InvokeFn) -> Self {
36        Self {
37            name,
38            invoke,
39            signature_json: "{}",
40            signature_hash: 0,
41        }
42    }
43
44    /// Create a descriptor with full signature information.
45    pub const fn with_signature(
46        name: &'static str,
47        invoke: InvokeFn,
48        signature_json: &'static str,
49        signature_hash: u64,
50    ) -> Self {
51        Self {
52            name,
53            invoke,
54            signature_json,
55            signature_hash,
56        }
57    }
58}
59
60impl std::fmt::Debug for ScriptDescriptor {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        f.debug_struct("ScriptDescriptor")
63            .field("name", &self.name)
64            .field("signature_json", &self.signature_json)
65            .field("signature_hash", &self.signature_hash)
66            .field("invoke", &"<fn>")
67            .finish()
68    }
69}
70
71quark::inventory::collect!(ScriptDescriptor);
72
73impl quark::Registrable for ScriptDescriptor {
74    fn registry_key(&self) -> &str {
75        self.name
76    }
77}