oxide-batch-cli 0.5.0

Minimal guarded operator command line for OxideBatch
Documentation
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Bounded human and machine output.
//!
//! Both forms are rendered from one already-redacted projection, so a redaction
//! rule cannot hold in the machine form while the human form leaks. Output is
//! written only after a mutating command's durable effect is committed, so a
//! display failure can never lose an effect.

use serde_json::{Map, Value, json};

use crate::args::OutputForm;
use crate::command::Command;
use crate::exit::{ExitCategory, Outcome};
use crate::host::Host;

/// The integer CLI output schema version.
pub const OUTPUT_SCHEMA_VERSION: u64 = 1;

/// Largest encoded response written for one invocation.
pub const MAX_OUTPUT_BYTES: usize = 256 * 1024;

/// Pagination fields of a paginated command.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PageInfo {
    /// The bound the traversal requested.
    pub page_size: u16,
    /// The number of rows this page returned.
    pub returned: usize,
    /// The opaque token that continues the traversal.
    pub next_cursor: Option<String>,
}

/// One bounded, redacted diagnostic record.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Diagnostic {
    /// Stable machine code.
    pub code: String,
    /// Safe-to-display detail. Never a secret, SQL, or user error text.
    pub detail: String,
}

impl Diagnostic {
    /// Builds one diagnostic record.
    #[must_use]
    pub fn new(code: impl Into<String>, detail: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            detail: detail.into(),
        }
    }
}

/// The complete result of one invocation.
#[derive(Clone, Debug)]
pub struct Response {
    command: Command,
    category: ExitCategory,
    data: Value,
    page: Option<PageInfo>,
    diagnostics: Vec<Diagnostic>,
}

impl Response {
    /// Builds a successful response carrying one redacted projection.
    #[must_use]
    pub const fn success(command: Command, data: Value) -> Self {
        Self {
            command,
            category: ExitCategory::Success,
            data,
            page: None,
            diagnostics: Vec::new(),
        }
    }

    /// Builds a response that reports a non-success category.
    #[must_use]
    pub const fn failed(command: Command, category: ExitCategory, data: Value) -> Self {
        Self {
            command,
            category,
            data,
            page: None,
            diagnostics: Vec::new(),
        }
    }

    /// Attaches the pagination fields of a paginated command.
    #[must_use]
    pub fn with_page(mut self, page: PageInfo) -> Self {
        self.page = Some(page);
        self
    }

    /// Attaches one bounded diagnostic record.
    #[must_use]
    pub fn with_diagnostic(mut self, diagnostic: Diagnostic) -> Self {
        self.diagnostics.push(diagnostic);
        self
    }

    /// Returns the exit category this response reports.
    #[must_use]
    pub const fn category(&self) -> ExitCategory {
        self.category
    }

    /// Returns the envelope outcome this response reports.
    #[must_use]
    pub const fn outcome(&self) -> Outcome {
        self.category.outcome()
    }
}

/// A failure to write the result.
///
/// The operation identifier lets the caller re-read or replay the request, so
/// the CLI never repeats a mutating call to recover from a display failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OutputFailure;

/// Renders responses in the selected form.
#[derive(Clone, Copy, Debug)]
pub struct Writer {
    form: OutputForm,
    color: bool,
}

impl Writer {
    /// Binds one output form and styling decision.
    #[must_use]
    pub const fn new(form: OutputForm, color: bool) -> Self {
        Self { form, color }
    }

    /// Returns the selected output form.
    #[must_use]
    pub const fn form(&self) -> OutputForm {
        self.form
    }

    /// Writes one response.
    ///
    /// Output stops at the first write failure and performs no further write,
    /// so a closed pipe produces one bounded attempt rather than a loop.
    ///
    /// # Errors
    ///
    /// Returns [`OutputFailure`] when standard output cannot be written.
    pub fn emit<H: Host>(&self, host: &mut H, response: &Response) -> Result<(), OutputFailure> {
        let rendered = match self.form {
            OutputForm::Json => fit(&response.data, |data, truncated| {
                render_json(response, data, truncated)
            }),
            OutputForm::Human => fit(&response.data, |data, truncated| {
                self.render_human(response, data, truncated)
            }),
        };
        // Every write failure is the same observable event: the result could
        // not be delivered. The underlying error is deliberately not inspected,
        // because its message may name a path or a device.
        host.write_stdout(rendered.as_bytes())
            .and_then(|()| host.flush_stdout())
            .map_err(|_| OutputFailure)
    }

    /// Renders the unversioned presentation form from already-bounded data.
    fn render_human(self, response: &Response, data: &Value, truncated: bool) -> String {
        use std::fmt::Write as _;

        let mut text = String::new();
        if !matches!(response.outcome(), Outcome::Success) {
            self.heading(response.category.as_str(), &mut text);
            text.push('\n');
        }
        render_value(data, 0, self, &mut text);
        if let Some(page) = &response.page {
            self.heading("page", &mut text);
            text.push('\n');
            // Writing into a `String` cannot fail, so the formatting result
            // carries no information a caller could act on.
            let _ = writeln!(
                text,
                "  page_size: {}\n  returned: {}",
                page.page_size, page.returned
            );
            match &page.next_cursor {
                Some(cursor) => {
                    let _ = writeln!(text, "  next_cursor: {cursor}");
                }
                None => text.push_str("  next_cursor: -\n"),
            }
        }
        for diagnostic in &response.diagnostics {
            let _ = writeln!(text, "{}: {}", diagnostic.code, diagnostic.detail);
        }
        if truncated {
            text.push_str("truncated: true\n");
        }
        text
    }

    /// Appends a heading, styled when styling is enabled.
    fn heading(self, text: &str, target: &mut String) {
        if self.color {
            target.push_str("\u{1b}[1m");
            target.push_str(text);
            target.push_str("\u{1b}[0m");
        } else {
            target.push_str(text);
        }
    }
}

/// Renders the versioned machine envelope from already-bounded data.
fn render_json(response: &Response, data: &Value, truncated: bool) -> String {
    let mut envelope = Map::new();
    envelope.insert("schema_version".to_owned(), json!(OUTPUT_SCHEMA_VERSION));
    envelope.insert("command".to_owned(), json!(response.command.as_str()));
    envelope.insert("outcome".to_owned(), json!(response.outcome().as_str()));
    envelope.insert("data".to_owned(), data.clone());
    if let Some(page) = &response.page {
        envelope.insert(
            "page".to_owned(),
            json!({
                "page_size": page.page_size,
                "returned": page.returned,
                "next_cursor": page.next_cursor,
            }),
        );
    }
    envelope.insert(
        "diagnostics".to_owned(),
        Value::Array(
            response
                .diagnostics
                .iter()
                .map(|diagnostic| json!({ "code": diagnostic.code, "detail": diagnostic.detail }))
                .collect(),
        ),
    );
    envelope.insert("truncated".to_owned(), json!(truncated));
    let mut encoded = Value::Object(envelope).to_string();
    encoded.push('\n');
    encoded
}

/// Renders one JSON value as indented human text.
fn render_value(value: &Value, depth: usize, writer: Writer, text: &mut String) {
    use std::fmt::Write as _;

    let indent = "  ".repeat(depth);
    match value {
        Value::Object(entries) => {
            for (key, entry) in entries {
                match entry {
                    Value::Object(_) | Value::Array(_) => {
                        text.push_str(&indent);
                        writer.heading(key, text);
                        text.push('\n');
                        render_value(entry, depth + 1, writer, text);
                    }
                    _ => {
                        let _ = writeln!(text, "{indent}{key}: {}", scalar(entry));
                    }
                }
            }
        }
        Value::Array(rows) => {
            if rows.is_empty() {
                let _ = writeln!(text, "{indent}-");
            }
            for row in rows {
                match row {
                    Value::Object(_) | Value::Array(_) => {
                        let _ = writeln!(text, "{indent}-");
                        render_value(row, depth + 1, writer, text);
                    }
                    _ => {
                        let _ = writeln!(text, "{indent}- {}", scalar(row));
                    }
                }
            }
        }
        _ => {
            let _ = writeln!(text, "{indent}{}", scalar(value));
        }
    }
}

/// Renders one scalar without quoting noise.
fn scalar(value: &Value) -> String {
    match value {
        Value::Null => "-".to_owned(),
        Value::String(text) => text.clone(),
        other => other.to_string(),
    }
}

/// Applies the response bound to the complete rendered output.
///
/// The bound covers everything written, not only the projection: an envelope
/// carrying a large page or diagnostic list cannot push the result past the
/// bound while still reporting `truncated: false`.
///
/// Rows are dropped from the end of an array, and keys from the end of an
/// object, until the rendered output fits, so a bounded page degrades to fewer
/// rows rather than to silently missing content without the flag. The largest
/// fitting prefix is found by bisection rather than by removing one element at
/// a time, so a full page costs a handful of renders instead of one per row.
fn fit(data: &Value, render: impl Fn(&Value, bool) -> String) -> String {
    let full = render(data, false);
    if full.len() <= MAX_OUTPUT_BYTES {
        return full;
    }
    match data {
        Value::Array(rows) => {
            let keep = largest_fitting(rows.len(), |count| {
                render(&Value::Array(rows[..count].to_vec()), true).len() <= MAX_OUTPUT_BYTES
            });
            match keep {
                Some(count) => render(&Value::Array(rows[..count].to_vec()), true),
                None => omitted(&render),
            }
        }
        Value::Object(entries) => {
            let keys: Vec<String> = entries.keys().cloned().collect();
            let prefix = |count: usize| {
                let mut kept = Map::new();
                for key in keys.iter().take(count) {
                    if let Some(value) = entries.get(key) {
                        kept.insert(key.clone(), value.clone());
                    }
                }
                Value::Object(kept)
            };
            let keep = largest_fitting(keys.len(), |count| {
                render(&prefix(count), true).len() <= MAX_OUTPUT_BYTES
            });
            match keep {
                Some(count) => render(&prefix(count), true),
                None => omitted(&render),
            }
        }
        _ => omitted(&render),
    }
}

/// Renders the marker used when nothing of the projection fits.
fn omitted(render: &impl Fn(&Value, bool) -> String) -> String {
    render(
        &json!({ "omitted": "the value exceeds the response bound" }),
        true,
    )
}

/// Returns the largest `count` in `0..=len` for which `fits` holds.
///
/// `fits` is monotonic: a shorter prefix of the same value never renders
/// larger than a longer one. Returns `None` when even the empty prefix does not
/// fit, which means the envelope alone exceeds the bound.
fn largest_fitting(len: usize, fits: impl Fn(usize) -> bool) -> Option<usize> {
    if !fits(0) {
        return None;
    }
    let (mut low, mut high) = (0_usize, len);
    while low < high {
        let middle = low + (high - low).div_ceil(2);
        if fits(middle) {
            low = middle;
        } else {
            high = middle - 1;
        }
    }
    Some(low)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::panic)]

    use serde_json::json;

    use super::{
        Diagnostic, MAX_OUTPUT_BYTES, OUTPUT_SCHEMA_VERSION, PageInfo, Response, Writer,
        render_json,
    };
    use crate::args::OutputForm;
    use crate::command::Command;
    use crate::exit::ExitCategory;
    use crate::host::testing::TestHost;

    fn json_writer() -> Writer {
        Writer::new(OutputForm::Json, false)
    }

    #[test]
    fn the_envelope_carries_every_published_field() {
        let mut host = TestHost::new();
        let response = Response::success(Command::JobList, json!(["orders"]))
            .with_page(PageInfo {
                page_size: 50,
                returned: 1,
                next_cursor: None,
            })
            .with_diagnostic(Diagnostic::new("NOTE", "one page returned"));
        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let value: serde_json::Value =
            serde_json::from_str(&host.stdout_text()).expect("the output is JSON");
        assert_eq!(value["schema_version"], json!(OUTPUT_SCHEMA_VERSION));
        assert_eq!(value["command"], json!("job list"));
        assert_eq!(value["outcome"], json!("success"));
        assert_eq!(value["data"], json!(["orders"]));
        assert_eq!(value["page"]["page_size"], json!(50));
        assert_eq!(value["page"]["returned"], json!(1));
        assert_eq!(value["page"]["next_cursor"], json!(null));
        assert_eq!(value["diagnostics"][0]["code"], json!("NOTE"));
        assert_eq!(value["truncated"], json!(false));
    }

    #[test]
    fn one_object_is_emitted_per_invocation() {
        let mut host = TestHost::new();
        let response = Response::success(Command::JobList, json!([]));
        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");
        assert_eq!(host.stdout_text().trim_end().lines().count(), 1);
    }

    #[test]
    fn a_failed_category_maps_to_its_outcome() {
        let mut host = TestHost::new();
        let response = Response::failed(
            Command::ExecutionStop,
            ExitCategory::OptimisticConflict,
            json!({ "rejection": "OPTIMISTIC_CONFLICT" }),
        );
        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let value: serde_json::Value =
            serde_json::from_str(&host.stdout_text()).expect("the output is JSON");
        assert_eq!(value["outcome"], json!("conflict"));
    }

    #[test]
    fn exceeding_the_bound_sets_the_truncation_flag() {
        let mut host = TestHost::new();
        let row = "x".repeat(1024);
        let rows: Vec<serde_json::Value> = (0..512).map(|_| json!(row)).collect();
        let response = Response::success(Command::JobList, json!(rows));
        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let value: serde_json::Value =
            serde_json::from_str(&host.stdout_text()).expect("the output is JSON");
        assert_eq!(value["truncated"], json!(true));
        let kept = value["data"].as_array().expect("data is an array").len();
        assert!(kept < 512, "the bound removed no row");
        assert!(value["data"].to_string().len() <= MAX_OUTPUT_BYTES);
    }

    #[test]
    fn the_bound_covers_the_whole_envelope_not_only_the_projection() {
        // The projection alone fits the bound; only the pagination and
        // diagnostic fields push the rendered envelope past it. Bounding just
        // the projection would therefore report `truncated: false` while
        // writing more than the bound allows.
        let mut host = TestHost::new();
        let row = "y".repeat(1024);
        let rows: Vec<serde_json::Value> = (0..250).map(|_| json!(row)).collect();
        let data = json!(rows);
        assert!(
            data.to_string().len() <= MAX_OUTPUT_BYTES,
            "the fixture is not discriminating: the projection alone already exceeds the bound"
        );

        let response = Response::success(Command::JobList, data.clone())
            .with_page(PageInfo {
                page_size: 500,
                returned: 250,
                next_cursor: Some("c".repeat(512)),
            })
            .with_diagnostic(Diagnostic::new("NOTE", "z".repeat(12 * 1024)));
        assert!(
            render_json(&response, &data, false).len() > MAX_OUTPUT_BYTES,
            "the fixture is not discriminating: the envelope already fits"
        );

        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");

        let written = host.stdout_text();
        assert!(
            written.len() <= MAX_OUTPUT_BYTES,
            "the envelope exceeded the bound at {} bytes",
            written.len()
        );
        let value: serde_json::Value = serde_json::from_str(&written).expect("the output is JSON");
        assert_eq!(value["truncated"], json!(true));
        // The fields that are not the projection survive truncation, because
        // the page and diagnostics are what a caller needs to continue.
        assert_eq!(value["page"]["page_size"], json!(500));
        assert_eq!(value["diagnostics"][0]["code"], json!("NOTE"));
        assert!(
            !value["data"]
                .as_array()
                .expect("data is an array")
                .is_empty(),
            "truncation removed the whole projection"
        );
    }

    #[test]
    fn a_result_within_the_bound_is_never_flagged_truncated() {
        let mut host = TestHost::new();
        let response = Response::success(Command::JobList, json!(["orders"])).with_page(PageInfo {
            page_size: 50,
            returned: 1,
            next_cursor: None,
        });
        json_writer()
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let value: serde_json::Value =
            serde_json::from_str(&host.stdout_text()).expect("the output is JSON");
        assert_eq!(value["truncated"], json!(false));
        assert_eq!(value["data"], json!(["orders"]));
    }

    #[test]
    fn the_human_form_obeys_the_same_bound() {
        let mut host = TestHost::new();
        let row = "y".repeat(1024);
        let rows: Vec<serde_json::Value> = (0..512).map(|_| json!(row)).collect();
        let response = Response::success(Command::JobList, json!(rows));
        Writer::new(OutputForm::Human, false)
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let written = host.stdout_text();
        assert!(written.len() <= MAX_OUTPUT_BYTES);
        assert!(written.contains("truncated: true"));
    }

    #[test]
    fn a_closed_pipe_reports_an_output_failure() {
        let mut host = TestHost::new().with_stdout_capacity(4);
        let response = Response::success(Command::JobList, json!(["orders", "invoices"]));
        json_writer()
            .emit(&mut host, &response)
            .expect_err("the pipe is closed");
        assert!(host.stdout_text().is_empty());
    }

    #[test]
    fn the_human_form_renders_without_styling_when_disabled() {
        let mut host = TestHost::new();
        let response = Response::success(
            Command::ExecutionShow,
            json!({ "execution_id": 4, "status": "COMPLETED" }),
        );
        Writer::new(OutputForm::Human, false)
            .emit(&mut host, &response)
            .expect("the write succeeds");
        let text = host.stdout_text();
        assert!(text.contains("execution_id: 4"));
        assert!(text.contains("status: COMPLETED"));
        assert!(!text.contains('\u{1b}'), "styling leaked into plain output");
    }
}