1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! CLI constants, globals, and display macros.
//!
//! This module centralises the values and shared mutable state needed across
//! the CLI layer:
//!
//! - Identity strings (`COMPRESSOR_NAME`, `LZ4_EXTENSION`, …)
//! - Binary size multipliers (`KB`, `MB`, `GB`)
//! - The verbosity level used by [`displaylevel!`] and friends
//! - The legacy-command flag that activates `lz4c`-style short options
//! - The [`displayout!`], [`display!`], [`displaylevel!`], [`debugoutput!`],
//! and [`end_process!`] output macros used throughout the CLI
use ;
// ── Identity strings ────────────────────────────────────────────────────────
/// Primary compressor name, reported in `--version` output and as the default output extension.
pub const COMPRESSOR_NAME: &str = "lz4";
/// Library author credit shown in the welcome banner.
pub const AUTHOR: &str = "Yann Collet";
/// Default file extension appended to compressed output files.
pub const LZ4_EXTENSION: &str = ".lz4";
/// Canonical name for the decompression-only binary alias.
pub const LZ4CAT: &str = "lz4cat";
/// Canonical name for the decompression binary alias.
pub const UNLZ4: &str = "unlz4";
/// Name of the legacy `lz4c` binary whose short-option dialect this library supports.
pub const LZ4_LEGACY: &str = "lz4c";
/// Format string for the startup welcome banner.
///
/// Positional arguments (in order): compressor name, version, pointer-width in bits,
/// threading mode string, author name.
pub const WELCOME_MESSAGE_FMT: &str = "*** {} v{} {}-bit {}, by {} ***\n";
// ── Binary size multipliers ─────────────────────────────────────────────────
/// 1 KiB (1 024 bytes).
pub const KB: u64 = 1 << 10;
/// 1 MiB (1 048 576 bytes).
pub const MB: u64 = 1 << 20;
/// 1 GiB (1 073 741 824 bytes).
pub const GB: u64 = 1 << 30;
// ── Threading-mode label ────────────────────────────────────────────────────
/// Human-readable threading mode inserted into the welcome banner.
///
/// Resolves to `"multithread"` when the `multithread` Cargo feature is enabled,
/// or `"single-thread"` otherwise.
pub const IO_MT: &str = "multithread";
pub const IO_MT: &str = "single-thread";
// ── Verbosity level ──────────────────────────────────────────────────────────
//
// Controls how much output the CLI produces. Semantics:
// 0 — completely silent
// 1 — errors only
// 2 — normal informational output (default; can be suppressed with -q)
// 3 — non-suppressible informational messages
// 4 — verbose / diagnostic
//
// Stored as a process-wide atomic so it is accessible from any module without
// threading through a context struct.
pub static DISPLAY_LEVEL: AtomicU32 = new;
/// Returns the current verbosity level.
/// Sets the verbosity level. Values outside 0–4 are accepted but have no
/// additional effect beyond level 4.
// ── Legacy lz4c command mode ─────────────────────────────────────────────────
//
// When the binary is invoked as "lz4c", this flag is set to enable the
// alternate short-option dialect: `-c0`, `-c1`, `-hc`, `-y`, etc.
//
// Stored as an atomic bool so it is visible across modules. In unit tests,
// prefer passing the flag explicitly rather than relying on this global.
pub static LZ4C_LEGACY_COMMANDS: AtomicBool = new;
/// Returns `true` when the binary is running in legacy `lz4c` command mode.
/// Enables (`true`) or disables (`false`) legacy `lz4c` command mode.
// ── Output macros ────────────────────────────────────────────────────────────
//
// Three tiers of CLI output:
// displayout! — informational output that belongs on stdout (e.g. decompressed data)
// display! — diagnostic output that always goes to stderr
// displaylevel! — conditional stderr output gated on the current verbosity level
/// Write a formatted message to **stdout**.
///
/// Use this for output that is part of the compressed or decompressed data
/// stream (e.g. when writing to a pipe), so it does not pollute stderr.
/// Write a formatted message to **stderr** unconditionally.
///
/// Prefer [`displaylevel!`] when the message should be suppressible.
/// Write a formatted message to **stderr** if the current verbosity level is
/// at least `level`.
///
/// | `level` | meaning |
/// |---------|----------------------------|
/// | 1 | errors only |
/// | 2 | normal (default) |
/// | 3 | non-suppressible info |
/// | 4 | verbose / diagnostic |
// ── Debug and fatal-error macros ─────────────────────────────────────────────
//
// `debugoutput!` — emits to stderr in debug builds only; a no-op in release.
// `end_process!` — prints a diagnostic then terminates the process, used for
// unrecoverable CLI errors (bad arguments, I/O failures, etc.).
/// Write a formatted message to **stderr** in debug builds only.
///
/// Compiled away entirely in release builds (`--release` / no `debug_assertions`).
/// Print an error diagnostic and exit the process with the given code.
///
/// In debug builds, also emits the source file and line number before the
/// message. The error message is only printed when the verbosity level is ≥ 1
/// (i.e. not completely silent).
///
/// # Example
/// ```ignore
/// end_process!(1, "cannot open '{}'", path);
/// ```