use crate::state::MutationBatch;
use awaken_contract::registry_spec::AgentSpec;
use awaken_contract::{PluginConfigKey, StateError};
use super::{PluginDescriptor, PluginRegistrar};
pub struct ConfigSchema {
pub key: &'static str,
pub json_schema: serde_json::Value,
}
impl ConfigSchema {
pub fn for_key<K: PluginConfigKey>() -> Self {
Self {
key: K::KEY,
json_schema: serde_json::to_value(schemars::schema_for!(K::Config)).unwrap_or_default(),
}
}
}
pub trait Plugin: Send + Sync + 'static {
fn descriptor(&self) -> PluginDescriptor;
fn bind_runtime_context(
&self,
_store: &crate::state::StateStore,
_owner_inbox: Option<&crate::inbox::InboxSender>,
) {
}
fn register(&self, _registrar: &mut PluginRegistrar) -> Result<(), StateError> {
Ok(())
}
fn config_schemas(&self) -> Vec<ConfigSchema> {
Vec::new()
}
fn on_activate(
&self,
_agent_spec: &AgentSpec,
_patch: &mut MutationBatch,
) -> Result<(), StateError> {
Ok(())
}
fn on_deactivate(&self, _patch: &mut MutationBatch) -> Result<(), StateError> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
struct TestConfig {
value: String,
}
struct TestConfigKey;
impl PluginConfigKey for TestConfigKey {
const KEY: &'static str = "test";
type Config = TestConfig;
}
#[test]
fn config_schema_for_key_uses_typed_key_and_config() {
let schema = ConfigSchema::for_key::<TestConfigKey>();
assert_eq!(schema.key, TestConfigKey::KEY);
assert_eq!(schema.json_schema["type"], "object");
assert!(schema.json_schema["properties"].get("value").is_some());
}
}