ctb_io/io.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//! IO subsystem coordinating `WebUI`, IPC, and peripheral communications.
21
22#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace crate prelude"
26)]
27pub(crate) use ctb_utilities::*;
28
29#[allow(
30 unused_imports,
31 clippy::wildcard_imports,
32 reason = "Standard workspace prelude"
33)]
34pub use ctb_utilities::ipc::service_prelude::*;
35
36pub use ctb_io_environment as environment;
37pub use ctb_io_file as file;
38pub use ctb_io_system as system;
39pub use ctb_io_webui as webui;
40
41/// Start a local web UI server, returning the port number.
42#[ipc_method]
43pub fn start_local_webui() -> u16 {
44 webui::start_webui()
45}
46
47/// Print raw bytes as UTF-8 (lossy) to stdout.
48#[ipc_method]
49pub fn print(document: Vec<u8>) -> Result<()> {
50 let string = String::from_utf8_lossy(&document).to_string();
51 let string = string.as_str();
52
53 println!("{string}");
54 Ok(())
55}
56
57/// Print a string to stdout.
58#[ipc_method]
59pub fn print_string(document: String) -> Result<()> {
60 print(strtovec(&document))?;
61 Ok(())
62}
63
64#[cfg(test)]
65#[allow(
66 clippy::panic,
67 clippy::expect_used,
68 clippy::unwrap_used,
69 clippy::unwrap_in_result,
70 clippy::panic_in_result_fn,
71 clippy::indexing_slicing,
72 clippy::arithmetic_side_effects,
73 reason = "Standard repository test boilerplate"
74)]
75mod tests {
76 use super::*;
77
78 #[crate::ctb_test]
79 fn can_start() {
80 // put("key".to_string(), "value".to_string());
81 // assert_eq!("key", String::from_utf8_lossy(&get("key").unwrap()));
82 }
83}