ctb_workspace_ipc/
ipc.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! IPC library root exporting router, auth, connection, and types.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace crate prelude"
26)]
27pub(crate) use ctb_utilities::*;
28
29use crate::protocol::Response;
30
31pub mod auth;
32pub mod connection;
33pub mod data_plane;
34pub mod error;
35pub mod multiplex;
36pub mod peer;
37pub mod peer_clients;
38pub mod process_manager;
39pub mod protocol;
40pub mod router;
41pub mod services;
42pub mod transport;
43pub mod types;
44pub mod workspace_runner;
45
46pub(crate) fn ensure_response_ok(
47    response: &Response,
48    context: &str,
49) -> Result<()> {
50    if response.ok {
51        return Ok(());
52    }
53
54    // Reason for fallback: JSON-RPC error payload missing a detailed error message defaults to generic "unknown error"
55    let msg = response
56        .error
57        .as_ref()
58        .map_or_else(|| "unknown error".to_string(), |e| e.message.clone());
59    bail!("{context} failed: {msg}")
60}
61
62pub(crate) fn response_result_bytes(
63    response: Response,
64    context: &str,
65) -> Result<Vec<u8>> {
66    ensure_response_ok(&response, context)?;
67    let Some(bytes) = response.result else {
68        bail!("{context} missing result");
69    };
70    Ok(bytes)
71}
72
73#[cfg(test)]
74pub fn assert_ipc_response_ok(response: &Response) {
75    assert!(response.ok, "IPC response indicates failure: {response:?}");
76}
77
78#[cfg(test)]
79pub fn assert_ipc_response_error(response: &Response) {
80    assert!(
81        !response.ok,
82        "IPC response indicates success when error expected: {response:?}"
83    );
84}