//! Main dispatcher for domain-specific RPC methods. //! //! This module routes RPC calls to their respective domain handlers (e.g., //! security, memory, local AI). It serves as an extension point for //! domain-level functionality in the OpenHuman platform. use serde::Serialize; use crate::rpc::RpcOutcome; /// Helper to convert an [`RpcOutcome`] into a JSON value compatible with the CLI. fn rpc_json(outcome: RpcOutcome) -> Result { outcome.into_cli_compatible_json() } /// Dispatches an RPC method to its domain-specific handler. /// /// If the method is recognized, it executes the handler and returns the /// result wrapped in a `Some(Result)`. If not recognized, it returns `None`. /// /// # Arguments /// /// * `method` - The name of the RPC method to invoke. /// * `params` - The parameters for the call as a JSON value. /// /// # Returns /// /// `Some(Ok(Value))` if successful, `Some(Err(String))` if the handler failed, /// or `None` if the method was not found in this dispatcher. pub async fn try_dispatch( method: &str, _params: serde_json::Value, ) -> Option> { match method { // Core security policy information. "openhuman.security_policy_info" => Some(rpc_json( crate::openhuman::security::rpc::security_policy_info(), )), _ => None, } } #[cfg(test)] mod tests { use serde_json::json; use super::try_dispatch; /// Unknown methods must return `None` so callers can fall through. #[tokio::test] async fn dispatch_returns_none_for_unknown_method() { let result = try_dispatch("nonexistent.method", json!({})).await; assert!(result.is_none(), "unknown methods should return None"); } #[tokio::test] async fn dispatch_security_policy_info_returns_some() { let result = try_dispatch("openhuman.security_policy_info", json!({})).await; assert!(result.is_some(), "security_policy_info should be handled"); let inner = result.unwrap(); assert!(inner.is_ok(), "security_policy_info should succeed"); } #[tokio::test] async fn dispatch_empty_method_returns_none() { let result = try_dispatch("", json!({})).await; assert!(result.is_none()); } #[tokio::test] async fn dispatch_close_but_wrong_method_returns_none() { let result = try_dispatch("openhuman.security_policy", json!({})).await; assert!(result.is_none()); } }