fastmash 0.4.0

Fast command-line statistics on text data, compatible with GNU datamash
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Failures and their exit statuses: an Error (1), a Refusal (77) or an
//! Internal failure (70; other internal failures refuse with 77), and which
//! status a command exits with when one failure follows another.

use super::terminal_style::{Role, Style};
use std::io::{self, Write};

#[derive(Clone)]
pub(super) struct Failure {
    pub(super) status: i32,
    pub(super) message: Vec<u8>,
}
#[cfg(test)]
thread_local! {
    /// Leaves the next refusal no memory for its message.
    pub(super) static MESSAGE_FAILS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub(super) fn unsupported(reason: &str) -> Failure {
    // An allocation failure can leave no memory for its own message: the
    // refusal then has none, and its report names the cause (`reported`).
    let mut message = Vec::new();
    #[cfg(test)]
    let fails = MESSAGE_FAILS.with(|fails| fails.replace(false));
    #[cfg(not(test))]
    let fails = false;
    if !fails && message.try_reserve_exact(reason.len() + 1).is_ok() {
        message.extend_from_slice(reason.as_bytes());
        message.push(b'\n');
    }
    Failure {
        status: 77,
        message,
    }
}
/// What a refusal that had no memory for its message reports.
const NO_MEMORY: &[u8] = b"memory allocation failed\n";
/// The text that reports `error`: its message, or the cause of a refusal
/// that had no memory for one.
pub(super) fn reported(error: &Failure) -> &[u8] {
    if error.message.is_empty() {
        NO_MEMORY
    } else {
        &error.message
    }
}

/// Writes the existing failure prefix without allocating, resetting even if
/// the prefix fails. Startup can still attempt its fallback body independently.
pub(super) fn write_prefix(
    writer: &mut impl Write,
    program: &[u8],
    style: Style,
) -> io::Result<()> {
    let prefix = writer
        .write_all(style.start(Role::Failure))
        .and_then(|()| writer.write_all(program))
        .and_then(|()| writer.write_all(b": "));
    // A partially written styled prefix can leave a terminal colored. Attempt
    // reset even on failure, while retaining the original write error.
    let reset = writer.write_all(style.reset());
    prefix?;
    reset
}

/// Writes a failure's existing prefix and body without allocating.
/// Reset before the body so supplied byte content and hints stay unstyled.
pub(super) fn write(
    writer: &mut impl Write,
    program: &[u8],
    error: &Failure,
    style: Style,
) -> io::Result<()> {
    write_prefix(writer, program, style)?;
    writer.write_all(reported(error))?;
    writer.flush()
}
/// Appends `parts` to `error`'s message, where there is memory for them. A
/// refusal that had no memory for its message first takes the cause its
/// report names (`reported`), so that the parts follow the cause instead of
/// standing in for it.
pub(super) fn append(error: &mut Failure, parts: &[&[u8]]) {
    let cause = if error.message.is_empty() {
        NO_MEMORY
    } else {
        b""
    };
    let size = parts
        .iter()
        .fold(cause.len(), |size, part| size.saturating_add(part.len()));
    if error.message.try_reserve_exact(size).is_ok() {
        error.message.extend_from_slice(cause);
        for part in parts {
            error.message.extend_from_slice(part);
        }
    }
}
pub(super) fn unsupported_hint(reason: &str, hint: &str) -> Failure {
    let mut error = unsupported(reason);
    append(&mut error, &[b"hint: ", hint.as_bytes(), b"\n"]);
    error
}
pub(super) fn failure(message: Vec<u8>) -> Failure {
    Failure { status: 1, message }
}
pub(super) fn numeric_failure(error: fastmash_portable_numerics::NumericFailure) -> Failure {
    use fastmash_portable_numerics::NumericFailure;
    let (status, message) = match error {
        NumericFailure::UnsupportedDomain => (
            77,
            "numerical operation result is outside the supported domain\n",
        ),
        NumericFailure::Capacity => (77, "numerical arithmetic capacity exceeded\n"),
        NumericFailure::Allocation => (77, "numerical arithmetic memory allocation failed\n"),
        NumericFailure::Invariant => (70, "internal numerical invariant failure\n"),
        NumericFailure::TableIdentity => (70, "numerical table identity check failed\n"),
    };
    Failure {
        status,
        message: message.as_bytes().to_vec(),
    }
}
pub(super) fn conversion_failure(error: fastmash_conversion::profile::Error) -> Failure {
    use fastmash_conversion::profile::Error;
    unsupported(match error {
        Error::InputCapacity => "numeric conversion input exceeds 8192-byte limit",
        Error::OutputCapacity => "numeric output exceeds 16384-byte limit",
        Error::CoefficientCapacity => "numeric conversion coefficient capacity exceeded",
        Error::IntermediateCapacity => "numeric conversion intermediate capacity exceeded",
        Error::PowerCapacity => "numeric conversion power capacity exceeded",
        Error::ShiftCapacity => "numeric conversion shift capacity exceeded",
        Error::UnsupportedProfile => "unsupported numerical conversion profile",
        Error::UnsupportedFormat => "unsupported numerical output format",
        Error::Allocation => "numeric conversion memory allocation failed",
        Error::InvalidView | Error::InternalInvariant | Error::Encoding(_) => {
            "internal numeric conversion failure"
        }
    })
}
pub(super) fn os_failure(error: &io::Error, reading: bool) -> Failure {
    match (reading, error.raw_os_error()) {
        (true, Some(9)) => failure(b"read error: Bad file descriptor\n".to_vec()),
        (false, Some(9)) => failure(b"write error: Bad file descriptor\n".to_vec()),
        (true, Some(21)) => failure(b"read error: Is a directory\n".to_vec()),
        (false, Some(28)) => failure(b"write error: No space left on device\n".to_vec()),
        (true, _) => unsupported("unsupported input I/O error"),
        (false, _) => unsupported("unsupported output I/O error"),
    }
}

/// The exit status when a failure with status `later` follows an earlier
/// outcome with status `earlier` (0 for success), such as a failure to
/// complete the output after the command's own failure: an Internal failure
/// (70) outranks a Refusal (77), which outranks an Error (1). A failure to
/// report a diagnostic is not combined: it makes the status 1 whatever came
/// before, as GNU's `close_stdout` does.
pub(super) fn combine(earlier: i32, later: i32) -> i32 {
    let rank = |status| match status {
        0 => 0,
        70 => 3,
        77 => 2,
        _ => 1,
    };
    if rank(later) > rank(earlier) {
        later
    } else {
        earlier
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::terminal_style::{ColorMode, Destination, Detection};

    fn style(mode: ColorMode) -> Style {
        Detection::default().style(mode, Destination::Stderr)
    }

    struct Sink {
        bytes: Vec<u8>,
        fail_at: Option<usize>,
        zero: bool,
        fail_flush: bool,
    }
    impl Write for Sink {
        fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
            let remaining = self.fail_at.map_or(usize::MAX, |at| at - self.bytes.len());
            if remaining == 0 {
                return Err(io::Error::from_raw_os_error(28));
            }
            if self.zero {
                return Ok(0);
            }
            let count = bytes.len().min(2).min(remaining);
            self.bytes.extend_from_slice(&bytes[..count]);
            Ok(count)
        }
        fn flush(&mut self) -> io::Result<()> {
            if self.fail_flush {
                Err(io::Error::from_raw_os_error(28))
            } else {
                Ok(())
            }
        }
    }

    #[test]
    fn failure_presentation_keeps_all_body_bytes_and_outcome_text() {
        for status in [1, 77, 70] {
            let error = Failure {
                status,
                message: b"first\nsecond\0\xff\x1b[36m\nhint: use --help\n".to_vec(),
            };
            for (mode, prefix) in [
                (ColorMode::Always, b"\x1b[31mfastmash: \x1b[0m".as_slice()),
                (ColorMode::Never, b"fastmash: "),
            ] {
                let mut bytes = Vec::new();
                write(&mut bytes, b"fastmash", &error, style(mode)).unwrap();
                assert_eq!(bytes, [prefix, &error.message].concat());
                assert_eq!(error.status, status);
            }
        }
        let mut bytes = Vec::new();
        write(
            &mut bytes,
            b"\xffprogram",
            &unsupported("checked limit"),
            style(ColorMode::Always),
        )
        .unwrap();
        assert_eq!(bytes, b"\x1b[31m\xffprogram: \x1b[0mchecked limit\n");
    }

    #[test]
    fn failure_presentation_keeps_the_allocation_failure_fallback() {
        MESSAGE_FAILS.with(|fails| fails.set(true));
        let refusal = unsupported("unavailable memory for this diagnostic");
        assert!(refusal.message.is_empty());
        let mut bytes = Vec::new();
        write(&mut bytes, b"fastmash", &refusal, style(ColorMode::Always)).unwrap();
        assert_eq!(
            bytes,
            b"\x1b[31mfastmash: \x1b[0mmemory allocation failed\n"
        );
        assert_eq!(refusal.status, 77);
    }

    #[test]
    fn allocation_failure_fallback_uses_default_stderr_detection() {
        for stderr_terminal in [false, true] {
            for (environment, eligible) in [
                (vec![], true),
                (vec![("NO_COLOR", "")], true),
                (vec![("NO_COLOR", "1")], false),
                (vec![("TERM", "dumb")], false),
            ] {
                let mut command = std::process::Command::new(std::env::current_exe().unwrap());
                command
                    .args([
                        "--exact",
                        "failure::tests::argument_collection_child",
                        "--nocapture",
                    ])
                    .env_clear()
                    .env("LC_ALL", "C")
                    .env("TERM", "xterm")
                    .env("FASTMASH_UNIT_ARGUMENT_FAILURE", "1")
                    .envs(environment.iter().copied());
                let output =
                    crate::test_terminal::output(&mut command, b"", false, stderr_terminal);
                assert_eq!(output.status.code(), Some(77), "{output:?}");
                assert_eq!(
                    output.stderr.starts_with(b"\x1b[31mfastmash: \x1b[0m"),
                    stderr_terminal && eligible,
                    "{environment:?}: {output:?}"
                );
                assert_eq!(
                    crate::test_terminal::strip_styles(&output.stderr),
                    b"fastmash: memory allocation failed\n"
                );
            }
        }
    }

    #[test]
    fn argument_collection_child() {
        if std::env::var_os("FASTMASH_UNIT_ARGUMENT_FAILURE").is_none() {
            return;
        }
        crate::command_memory::FAIL_RESERVATION.with(|at| at.set(Some(0)));
        MESSAGE_FAILS.with(|fails| fails.set(true));
        crate::run_process();
        panic!("argument collection failure must exit");
    }

    #[test]
    fn failure_presentation_preserves_short_zero_write_and_flush_failures() {
        let error = numeric_failure(fastmash_portable_numerics::NumericFailure::Invariant);
        for mode in [ColorMode::Never, ColorMode::Always] {
            let mut expected = Vec::new();
            write(&mut expected, b"fastmash", &error, style(mode)).unwrap();
            for fail_at in 0..expected.len() {
                let mut sink = Sink {
                    bytes: Vec::new(),
                    fail_at: Some(fail_at),
                    zero: false,
                    fail_flush: false,
                };
                assert_eq!(
                    write(&mut sink, b"fastmash", &error, style(mode))
                        .unwrap_err()
                        .raw_os_error(),
                    Some(28)
                );
                assert_eq!(sink.bytes, expected[..fail_at]);
            }
            let mut sink = Sink {
                bytes: Vec::new(),
                fail_at: None,
                zero: true,
                fail_flush: false,
            };
            assert_eq!(
                write(&mut sink, b"fastmash", &error, style(mode))
                    .unwrap_err()
                    .kind(),
                io::ErrorKind::WriteZero
            );
            assert!(sink.bytes.is_empty());
            sink.zero = false;
            sink.fail_flush = true;
            assert_eq!(
                write(&mut sink, b"fastmash", &error, style(mode))
                    .unwrap_err()
                    .raw_os_error(),
                Some(28)
            );
            assert_eq!(sink.bytes, expected);
        }
    }

    #[test]
    fn a_partial_prefix_attempts_reset_and_keeps_the_first_error() {
        struct Transient {
            bytes: Vec<u8>,
            fail_at: Option<usize>,
            fail_reset: bool,
        }
        impl Write for Transient {
            fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
                if self.fail_at == Some(self.bytes.len()) {
                    self.fail_at = None;
                    return Err(io::Error::from_raw_os_error(28));
                }
                if self.fail_at.is_none() && self.fail_reset {
                    return Err(io::Error::from_raw_os_error(5));
                }
                let remaining = self.fail_at.map_or(usize::MAX, |at| at - self.bytes.len());
                let count = bytes.len().min(2).min(remaining);
                self.bytes.extend_from_slice(&bytes[..count]);
                Ok(count)
            }
            fn flush(&mut self) -> io::Result<()> {
                panic!("a failed prefix stops before the body and flush")
            }
        }
        let prefix = b"\x1b[31mfastmash: ";
        for fail_at in 0..prefix.len() {
            for fail_reset in [false, true] {
                let mut sink = Transient {
                    bytes: Vec::new(),
                    fail_at: Some(fail_at),
                    fail_reset,
                };
                assert_eq!(
                    write(
                        &mut sink,
                        b"fastmash",
                        &unsupported("checked limit"),
                        style(ColorMode::Always),
                    )
                    .unwrap_err()
                    .raw_os_error(),
                    Some(28)
                );
                let reset = if fail_reset {
                    b"".as_slice()
                } else {
                    b"\x1b[0m"
                };
                assert_eq!(sink.bytes, [&prefix[..fail_at], reset].concat());
                let mut startup_sink = Transient {
                    bytes: Vec::new(),
                    fail_at: Some(fail_at),
                    fail_reset,
                };
                assert_eq!(
                    write_prefix(&mut startup_sink, b"fastmash", style(ColorMode::Always))
                        .unwrap_err()
                        .raw_os_error(),
                    Some(28)
                );
                // Startup keeps the fallback body independent of prefix errors.
                let refusal = Failure {
                    status: 77,
                    message: Vec::new(),
                };
                let body = reported(&refusal);
                let result = startup_sink.write_all(body);
                if fail_reset {
                    assert_eq!(result.unwrap_err().raw_os_error(), Some(5));
                } else {
                    result.unwrap();
                    assert_eq!(
                        startup_sink.bytes,
                        [&prefix[..fail_at], reset, body].concat()
                    );
                }
            }
        }
    }

    #[test]
    fn styled_internal_failure_completion_keeps_precedence_and_later_attempts() {
        for mode in [ColorMode::Never, ColorMode::Always] {
            for diagnostic_fails in [false, true] {
                for close_error in [28, 5] {
                    let mut output = Vec::new();
                    let mut closed = false;
                    let mut diagnostics = Vec::new();
                    let mut attempts = 0;
                    let buffer =
                        crate::buffered_stdout::BufferedStdout::new(&mut output, 128, false)
                            .unwrap();
                    let internal =
                        numeric_failure(fastmash_portable_numerics::NumericFailure::Invariant);
                    let status = crate::command_output::complete(
                        buffer,
                        Err(internal),
                        |_| {
                            closed = true;
                            Err(io::Error::from_raw_os_error(close_error))
                        },
                        &mut |failure| {
                            attempts += 1;
                            if diagnostic_fails {
                                let mut sink = Sink {
                                    bytes: Vec::new(),
                                    fail_at: Some(2),
                                    zero: false,
                                    fail_flush: false,
                                };
                                write(&mut sink, b"fastmash", failure, style(mode)).is_ok()
                            } else {
                                write(&mut diagnostics, b"fastmash", failure, style(mode)).is_ok()
                            }
                        },
                    );
                    assert_eq!(status, if diagnostic_fails { 1 } else { 70 });
                    assert!(closed);
                    assert_eq!(attempts, 2);
                    if !diagnostic_fails {
                        let prefix = if mode == ColorMode::Always {
                            b"\x1b[31mfastmash: \x1b[0m".as_slice()
                        } else {
                            b"fastmash: ".as_slice()
                        };
                        let later = if close_error == 28 {
                            b"write error: No space left on device\n".as_slice()
                        } else {
                            b"unsupported output I/O error\n".as_slice()
                        };
                        assert_eq!(
                            diagnostics,
                            [
                                prefix,
                                b"internal numerical invariant failure\n",
                                prefix,
                                later
                            ]
                            .concat()
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn an_internal_failure_outranks_a_refusal_which_outranks_an_error() {
        for (earlier, later, status) in [
            (0, 1, 1),
            (0, 77, 77),
            (0, 70, 70),
            (1, 1, 1),
            (1, 77, 77),
            (77, 1, 77),
            (70, 1, 70),
            (70, 77, 70),
            (77, 70, 70),
        ] {
            assert_eq!(combine(earlier, later), status, "{earlier} then {later}");
        }
    }
}