rumtk_core/cli.rs
1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025 Luis M. Santos, M.D.
5 * Copyright (C) 2025 MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22pub mod cli_utils {
23 use crate::core::RUMResult;
24 use crate::strings::{rumtk_format, EscapeExceptions, RUMString};
25 use crate::types::{RUMBuffer, RUMCLIParser};
26 use compact_str::CompactStringExt;
27 use std::io::{stdin, stdout, Read, StdinLock, Write};
28 use std::num::NonZeroU16;
29
30 const BUFFER_SIZE: usize = 1024 * 4;
31 const BUFFER_CHUNK_SIZE: usize = 512;
32
33 pub static CLI_ESCAPE_EXCEPTIONS: EscapeExceptions =
34 &[("\\n", "\n"), ("\\r", "\r"), ("\\\\", "\\")];
35
36 pub type BufferSlice = Vec<u8>;
37 pub type BufferChunk = [u8; BUFFER_CHUNK_SIZE];
38
39 ///
40 /// Example CLI parser that can be used to paste in your binary and adjust as needed.
41 ///
42 /// Note, this is only an example.
43 ///
44 #[derive(RUMCLIParser, Debug)]
45 #[command(author, version, about, long_about = None)]
46 pub struct RUMTKArgs {
47 ///
48 /// For interface crate only. Specifies the ip address to connect to.
49 ///
50 /// In outbound mode, `--ip` and `--port` are required parameters.
51 ///
52 /// In inbound mode, you can omit either or both parameters.
53 ///
54 #[arg(short, long)]
55 ip: Option<RUMString>,
56 ///
57 /// For interface crate only. Specifies the port to connect to.
58 ///
59 /// In outbound mode, `--ip` and `--port` are required parameters.
60 ///
61 /// In inbound mode, you can omit either or both parameters.
62 ///
63 #[arg(short, long)]
64 port: Option<NonZeroU16>,
65 ///
66 /// For process crate only. Specifies command line script to execute on message.
67 ///
68 #[arg(short, long)]
69 x: Option<RUMString>,
70 ///
71 /// Number of processing threads to allocate for this program.
72 ///
73 #[arg(short, long, default_value_t = 1)]
74 threads: usize,
75 ///
76 /// For interface crate only. Specifies if the interface is in outbound mode.
77 ///
78 /// In outbound mode, `--ip` and `--port` are required parameters.
79 ///
80 /// In inbound mode, you can omit either or both parameters.
81 ///
82 #[arg(short, long)]
83 outbound: bool,
84 ///
85 /// Request program runs in debug mode and log more information.
86 ///
87 #[arg(short, long, default_value_t = false)]
88 debug: bool,
89 ///
90 /// Request program runs in dry run mode and simulate as many steps as possible but not commit
91 /// to a critical non-reversible step.
92 ///
93 /// For example, if it was meant to write contents to a file, stop before doing so.
94 ///
95 #[arg(short, long, default_value_t = false)]
96 dry_run: bool,
97 }
98
99 pub fn read_stdin() -> RUMResult<RUMBuffer> {
100 let mut stdin_lock = stdin().lock();
101 let mut stdin_buffer: Vec<u8> = Vec::with_capacity(BUFFER_SIZE);
102 let mut s = read_some_stdin(&mut stdin_lock, &mut stdin_buffer)?;
103 while s == BUFFER_CHUNK_SIZE {
104 s = read_some_stdin(&mut stdin_lock, &mut stdin_buffer)?;
105 }
106
107 Ok(RUMBuffer::from(stdin_buffer))
108 }
109
110 pub fn read_some_stdin(input: &mut StdinLock, buf: &mut BufferSlice) -> RUMResult<usize> {
111 let mut chunk: BufferChunk = [0; BUFFER_CHUNK_SIZE];
112 match input.read(&mut chunk) {
113 Ok(s) => {
114 buf.extend_from_slice(&chunk);
115 Ok(s)
116 }
117 Err(e) => Err(rumtk_format!("Error reading stdin chunk because {}!", e)),
118 }
119 }
120
121 //TODO: Turn into a RUMBuffer for future tools.
122 pub fn write_stdout(data: &RUMString) -> RUMResult<()> {
123 let mut stdout_handle = stdout();
124 match stdout_handle.write_all(data.as_bytes()) {
125 Ok(_) => match stdout_handle.flush() {
126 Ok(_) => Ok(()),
127 Err(e) => Err(rumtk_format!("Error flushing stdout: {}", e)),
128 },
129 Err(e) => Err(rumtk_format!("Error writing to stdout!")),
130 }
131 }
132
133 pub fn print_license_notice(program: &str, year: &str, author_list: &Vec<&str>) {
134 let authors = author_list.join_compact(", ");
135 let notice = rumtk_format!(
136 " {program} Copyright (C) {year} {authors}
137 This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
138 This is free software, and you are welcome to redistribute it
139 under certain conditions; type `show c' for details."
140 );
141 println!("{}", notice);
142 }
143}
144
145pub mod macros {
146 ///
147 /// Reads STDIN and unescapes the incoming message.
148 /// Return this unescaped message.
149 ///
150 /// # Example
151 /// ```
152 /// use rumtk_core::core::RUMResult;
153 /// use rumtk_core::strings::RUMString;
154 /// use rumtk_core::rumtk_read_stdin;
155 ///
156 /// fn test_read_stdin() -> RUMResult<RUMString> {
157 /// rumtk_read_stdin!()
158 /// }
159 ///
160 /// match test_read_stdin() {
161 /// Ok(s) => (),
162 /// Err(e) => panic!("Error reading stdin because => {}", e)
163 /// }
164 /// ```
165 ///
166 #[macro_export]
167 macro_rules! rumtk_read_stdin {
168 ( ) => {{
169 use $crate::cli::cli_utils::read_stdin;
170 read_stdin()
171 }};
172 }
173
174 ///
175 /// Escapes a message and writes it to stdout via the print! macro.
176 ///
177 /// # Example
178 /// ```
179 /// use rumtk_core::rumtk_write_stdout;
180 ///
181 /// rumtk_write_stdout!("I ❤ my wife!");
182 /// ```
183 ///
184 #[macro_export]
185 macro_rules! rumtk_write_stdout {
186 ( $message:expr ) => {{
187 use $crate::cli::cli_utils::{write_stdout, CLI_ESCAPE_EXCEPTIONS};
188 use $crate::strings::basic_escape;
189 let escaped_message = basic_escape($message, CLI_ESCAPE_EXCEPTIONS);
190 write_stdout(&escaped_message);
191 }};
192 }
193
194 ///
195 /// Prints the mandatory GPL License Notice to terminal!
196 ///
197 /// # Example
198 /// ## Default
199 /// ```
200 /// use rumtk_core::rumtk_print_license_notice;
201 ///
202 /// rumtk_print_license_notice!();
203 /// ```
204 /// ## Program Only
205 /// ```
206 /// use rumtk_core::rumtk_print_license_notice;
207 ///
208 /// rumtk_print_license_notice!("RUMTK");
209 /// ```
210 /// ## Program + Year
211 /// ```
212 /// use rumtk_core::rumtk_print_license_notice;
213 ///
214 /// rumtk_print_license_notice!("RUMTK", "2025");
215 /// ```
216 /// ## Program + Year + Authors
217 /// ```
218 /// use rumtk_core::rumtk_print_license_notice;
219 ///
220 /// rumtk_print_license_notice!("RUMTK", "2025", &vec!["Luis M. Santos, M.D."]);
221 /// ```
222 ///
223 #[macro_export]
224 macro_rules! rumtk_print_license_notice {
225 ( ) => {{
226 use $crate::cli::cli_utils::print_license_notice;
227
228 print_license_notice("RUMTK", "2025", &vec!["Luis M. Santos, M.D."]);
229 }};
230 ( $program:expr ) => {{
231 use $crate::cli::cli_utils::print_license_notice;
232 print_license_notice(&$program, "2025", &vec!["2025", "Luis M. Santos, M.D."]);
233 }};
234 ( $program:expr, $year:expr ) => {{
235 use $crate::cli::cli_utils::print_license_notice;
236 print_license_notice(&$program, &$year, &vec!["Luis M. Santos, M.D."]);
237 }};
238 ( $program:expr, $year:expr, $authors:expr ) => {{
239 use $crate::cli::cli_utils::print_license_notice;
240 print_license_notice(&$program, &$year, &$authors);
241 }};
242 }
243}