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 =
14    fn(Box<dyn ScriptContext>, Value) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>;
15
16/// Descriptor for a registered script.
17///
18/// Collected at link time by `quark::inventory` for `#[chronon::script]` handlers or
19/// built manually in tests via [`Self::new`].
20pub struct ScriptDescriptor {
21    /// Unique script name.
22    pub name: &'static str,
23    /// Function to invoke the script with deserialized parameters.
24    pub invoke: InvokeFn,
25    /// JSON schema for parameters (computed at compile time by `chronon-macros`).
26    pub signature_json: &'static str,
27    /// Hash of the signature for version checking.
28    pub signature_hash: u64,
29}
30
31impl ScriptDescriptor {
32    /// Create a descriptor with placeholder signature metadata.
33    pub const fn new(name: &'static str, invoke: InvokeFn) -> Self {
34        Self {
35            name,
36            invoke,
37            signature_json: "{}",
38            signature_hash: 0,
39        }
40    }
41
42    /// Create a descriptor with full signature information.
43    pub const fn with_signature(
44        name: &'static str,
45        invoke: InvokeFn,
46        signature_json: &'static str,
47        signature_hash: u64,
48    ) -> Self {
49        Self {
50            name,
51            invoke,
52            signature_json,
53            signature_hash,
54        }
55    }
56}
57
58impl std::fmt::Debug for ScriptDescriptor {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        f.debug_struct("ScriptDescriptor")
61            .field("name", &self.name)
62            .field("signature_json", &self.signature_json)
63            .field("signature_hash", &self.signature_hash)
64            .field("invoke", &"<fn>")
65            .finish()
66    }
67}
68
69quark::inventory::collect!(ScriptDescriptor);
70
71impl quark::Registrable for ScriptDescriptor {
72    fn registry_key(&self) -> &str {
73        self.name
74    }
75}