ctoolbox/utilities/
ipc.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3
4use crate::cli::IpcEndpoint;
5
6pub static IPC_ARG: &str = "--76c89de8-96b3-4372-ab16-cd832871fce3";
7
8#[derive(Clone, Serialize, Deserialize, Default, Debug)]
9pub struct Channel {
10    pub name: String,
11    pub port: u16,
12    pub authentication_key: String,
13}
14
15impl Channel {
16    pub fn to_arg_string(&self) -> String {
17        format!("{}:{}", self.port, self.name)
18    }
19
20    // All processes are allowed to call workspace I guess and it avoids
21    // needing to pass around a channel reference to just let it bypass the auth
22    // which doesn't add any security in that case anyway
23    pub fn workspace() -> Self {
24        Channel {
25            name: "workspace".to_string(),
26            port: 0,
27            authentication_key: String::new(),
28        }
29    }
30}
31
32pub fn channel_from_args_and_key(
33    name_and_port: &IpcEndpoint,
34    authentication_key: String,
35) -> Channel {
36    Channel {
37        name: name_and_port.identity.clone(),
38        port: name_and_port.port,
39        authentication_key,
40    }
41}
42
43pub fn channel_from_json_string(json_string: &str) -> Result<Channel> {
44    serde_json::from_str(json_string)
45        .map_err(|e| anyhow::anyhow!("{e}: {json_string}"))
46}