Skip to main content

plexus_substrate/activations/health/
methods.rs

1/// Method definitions for the Health activation using JSON Schema
2///
3/// This provides type-safe method definitions with automatic schema generation.
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// All available methods in the Health activation
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10#[serde(tag = "method", content = "params", rename_all = "snake_case")]
11pub enum HealthMethod {
12    /// Check the health status of the hub
13    Check,
14}
15
16impl HealthMethod {
17    /// Get the method name as a string
18    pub fn name(&self) -> &'static str {
19        match self {
20            HealthMethod::Check => "check",
21        }
22    }
23
24    /// Get all available method names
25    pub fn all_names() -> Vec<&'static str> {
26        vec!["check"]
27    }
28
29    /// Get the JSON schema for all Health methods
30    pub fn schema() -> serde_json::Value {
31        let schema = schemars::schema_for!(HealthMethod);
32        serde_json::to_value(schema).unwrap()
33    }
34
35    /// Get a human-readable description of a method
36    pub fn description(method_name: &str) -> Option<&'static str> {
37        match method_name {
38            "check" => Some("Check the health status of the hub and return uptime"),
39            _ => None,
40        }
41    }
42}
43
44impl crate::plexus::MethodEnumSchema for HealthMethod {
45    fn method_names() -> &'static [&'static str] {
46        &["check"]
47    }
48
49    fn schema_with_consts() -> serde_json::Value {
50        // schemars 1.1+ generates const values for adjacently tagged enums
51        serde_json::to_value(schemars::schema_for!(HealthMethod)).expect("Schema should serialize")
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_method_names() {
61        let method = HealthMethod::Check;
62        assert_eq!(method.name(), "check");
63    }
64
65    #[test]
66    fn test_schema_generation() {
67        let schema = HealthMethod::schema();
68        let schema_str = serde_json::to_string(&schema).unwrap();
69        // Should have the method check
70        assert!(schema_str.contains("check"));
71    }
72}