Skip to main content

khive_pack_template/
pack.rs

1//! Handler table, inventory registration, and runtime dispatch for the template pack.
2
3use async_trait::async_trait;
4use serde_json::Value;
5
6use khive_runtime::pack::PackRuntime;
7use khive_runtime::{KhiveRuntime, NamespaceToken, RuntimeError, VerbRegistry};
8use khive_types::{HandlerDef, ParamDef, Visibility};
9
10use crate::{handlers, TemplatePack, PACK_NAME};
11
12/// Example public handler table; one definition is required per dispatchable verb.
13///
14/// See `crates/khive-pack-template/docs/api/pack-scaffold.md`.
15pub(crate) static TEMPLATE_HANDLERS: [HandlerDef; 1] = [HandlerDef {
16    name: "template.my_verb",
17    description: "Example pack-prefixed verb. Non-kg packs must use pack.verb naming.",
18    visibility: Visibility::Verb,
19    category: khive_types::VerbCategory::Directive,
20    params: &[ParamDef {
21        name: "name",
22        param_type: "string",
23        required: true,
24        description: "Non-empty string field to echo in the template response.",
25    }],
26}];
27
28struct TemplatePackFactory;
29
30impl khive_runtime::PackFactory for TemplatePackFactory {
31    fn name(&self) -> &'static str {
32        PACK_NAME
33    }
34    fn requires(&self) -> &'static [&'static str] {
35        &["kg"]
36    }
37    fn create(&self, runtime: KhiveRuntime) -> Box<dyn khive_runtime::PackRuntime> {
38        Box::new(TemplatePack::new(runtime))
39    }
40}
41
42inventory::submit! { khive_runtime::PackRegistration(&TemplatePackFactory) }
43
44#[async_trait]
45impl PackRuntime for TemplatePack {
46    fn name(&self) -> &str {
47        <TemplatePack as khive_types::Pack>::NAME
48    }
49    fn note_kinds(&self) -> &'static [&'static str] {
50        <TemplatePack as khive_types::Pack>::NOTE_KINDS
51    }
52    fn entity_kinds(&self) -> &'static [&'static str] {
53        <TemplatePack as khive_types::Pack>::ENTITY_KINDS
54    }
55    fn handlers(&self) -> &'static [HandlerDef] {
56        &TEMPLATE_HANDLERS
57    }
58    fn requires(&self) -> &'static [&'static str] {
59        <TemplatePack as khive_types::Pack>::REQUIRES
60    }
61
62    /// Dispatch a declared verb or return invalid-input for an unknown name.
63    async fn dispatch(
64        &self,
65        verb: &str,
66        params: Value,
67        _registry: &VerbRegistry,
68        token: &NamespaceToken,
69    ) -> Result<Value, RuntimeError> {
70        match verb {
71            "template.my_verb" => handlers::handle_my_verb(self.runtime(), token, params).await,
72            _ => Err(RuntimeError::InvalidInput(format!(
73                "{PACK_NAME} pack does not handle verb {verb:?}"
74            ))),
75        }
76    }
77}