Skip to main content

confium_daemon/methods/
meta.rs

1//! Meta methods: `version`, `shutdown`.
2//!
3//! `version` returns the daemon's package version (same as
4//! `confium-core`'s). `shutdown` signals the listen loop to stop
5//! accepting new connections and exit after in-flight requests drain.
6
7use serde_json::{Value, json};
8
9use crate::error::RpcError;
10use crate::server::SharedConfium;
11
12/// `version()` → `{"version": "0.3.0", "major": 0, "minor": 3, "patch": 0}`
13///
14/// The version string is the daemon's own `CARGO_PKG_VERSION`, which is
15/// kept in sync with the workspace version via `version.workspace =
16/// true`.
17pub async fn version(_cfm: SharedConfium, _params: Value) -> std::result::Result<Value, RpcError> {
18    let v = env!("CARGO_PKG_VERSION");
19    let parts: Vec<u32> = v
20        .split('.')
21        .map(|s| s.parse::<u32>().unwrap_or(0))
22        .collect();
23    Ok(json!({
24        "version": v,
25        "major": parts.first().copied().unwrap_or(0),
26        "minor": parts.get(1).copied().unwrap_or(0),
27        "patch": parts.get(2).copied().unwrap_or(0),
28    }))
29}
30
31/// `shutdown()` → `{"ok": true}`
32///
33/// The handler itself only acknowledges the request. The actual
34/// shutdown is triggered by the connection layer watching for this
35/// method name — when it sees `shutdown`, it initiates graceful
36/// teardown after replying.
37pub async fn shutdown(_cfm: SharedConfium, _params: Value) -> std::result::Result<Value, RpcError> {
38    Ok(json!({ "ok": true }))
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::test_util::test_confium;
45
46    #[tokio::test]
47    async fn version_returns_pkg_version() {
48        let result = version(test_confium(), json!({})).await.unwrap();
49        let obj = result.as_object().unwrap();
50        assert_eq!(
51            obj.get("version").unwrap().as_str().unwrap(),
52            env!("CARGO_PKG_VERSION")
53        );
54        assert!(obj.get("major").unwrap().is_u64());
55    }
56
57    #[tokio::test]
58    async fn shutdown_acknowledges() {
59        let result = shutdown(test_confium(), json!({})).await.unwrap();
60        assert_eq!(result, json!({ "ok": true }));
61    }
62}