Skip to main content

kanade_shared/ipc/
transport.rs

1//! KLP transport endpoints shared by the agent and client.
2
3/// SPEC ยง2.12.1 Windows endpoint.
4pub const PIPE_NAME: &str = r"\\.\pipe\kanade-agent";
5/// Non-empty values override the Unix endpoint for development and tests.
6pub const ENV_SOCKET_PATH: &str = "KANADE_KLP_SOCKET_PATH";
7
8#[cfg(target_os = "linux")]
9pub const DEFAULT_SOCKET_PATH: &str = "/run/kanade/agent.sock";
10#[cfg(target_os = "macos")]
11pub const DEFAULT_SOCKET_PATH: &str = "/var/run/kanade/agent.sock";
12
13#[cfg(any(target_os = "linux", target_os = "macos"))]
14/// Resolve an injected override without mutating process-wide environment.
15pub fn socket_path_from_override(value: Option<std::ffi::OsString>) -> std::path::PathBuf {
16    match value {
17        Some(p) if !p.is_empty() => p.into(),
18        _ => DEFAULT_SOCKET_PATH.into(),
19    }
20}
21
22#[cfg(any(target_os = "linux", target_os = "macos"))]
23/// Resolve the production endpoint using the shared environment override.
24pub fn socket_path() -> std::path::PathBuf {
25    socket_path_from_override(std::env::var_os(ENV_SOCKET_PATH))
26}
27
28#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn socket_override_and_empty_default() {
34        assert_eq!(
35            socket_path_from_override(Some("test.sock".into())),
36            std::path::PathBuf::from("test.sock")
37        );
38        assert_eq!(
39            socket_path_from_override(None),
40            std::path::PathBuf::from(DEFAULT_SOCKET_PATH)
41        );
42        assert_eq!(
43            socket_path_from_override(Some("".into())),
44            std::path::PathBuf::from(DEFAULT_SOCKET_PATH)
45        );
46    }
47}