pub mod handlers;
pub mod vocab;
use async_trait::async_trait;
use serde_json::Value;
use khive_runtime::pack::PackRuntime;
use khive_runtime::{KhiveRuntime, NamespaceToken, RuntimeError, VerbRegistry};
use khive_types::{HandlerDef, Pack, Visibility};
const PACK_NAME: &str = "template";
pub struct TemplatePack {
runtime: KhiveRuntime,
}
impl Pack for TemplatePack {
const NAME: &'static str = PACK_NAME;
const NOTE_KINDS: &'static [&'static str] = vocab::NOTE_KINDS;
const ENTITY_KINDS: &'static [&'static str] = vocab::ENTITY_KINDS;
const HANDLERS: &'static [HandlerDef] = &TEMPLATE_HANDLERS;
const REQUIRES: &'static [&'static str] = &["kg"];
}
static TEMPLATE_HANDLERS: [HandlerDef; 1] = [HandlerDef {
name: "my_verb",
description: "Replace with your verb's description.",
visibility: Visibility::Verb,
category: khive_types::VerbCategory::Directive,
params: &[],
}];
impl TemplatePack {
pub fn new(runtime: KhiveRuntime) -> Self {
Self { runtime }
}
#[allow(dead_code)]
fn runtime(&self) -> &KhiveRuntime {
&self.runtime
}
}
struct TemplatePackFactory;
impl khive_runtime::PackFactory for TemplatePackFactory {
fn name(&self) -> &'static str {
PACK_NAME
}
fn requires(&self) -> &'static [&'static str] {
&["kg"]
}
fn create(&self, runtime: KhiveRuntime) -> Box<dyn khive_runtime::PackRuntime> {
Box::new(TemplatePack::new(runtime))
}
}
inventory::submit! { khive_runtime::PackRegistration(&TemplatePackFactory) }
#[async_trait]
impl PackRuntime for TemplatePack {
fn name(&self) -> &str {
<TemplatePack as Pack>::NAME
}
fn note_kinds(&self) -> &'static [&'static str] {
<TemplatePack as Pack>::NOTE_KINDS
}
fn entity_kinds(&self) -> &'static [&'static str] {
<TemplatePack as Pack>::ENTITY_KINDS
}
fn handlers(&self) -> &'static [HandlerDef] {
&TEMPLATE_HANDLERS
}
fn requires(&self) -> &'static [&'static str] {
<TemplatePack as Pack>::REQUIRES
}
async fn dispatch(
&self,
verb: &str,
params: Value,
_registry: &VerbRegistry,
token: &NamespaceToken,
) -> Result<Value, RuntimeError> {
match verb {
"my_verb" => handlers::handle_my_verb(self.runtime(), token, params).await,
_ => Err(RuntimeError::InvalidInput(format!(
"{PACK_NAME} pack does not handle verb {verb:?}"
))),
}
}
}