Skip to main content

envelope_custom_fields/
envelope_custom_fields.rs

1//! Custom envelope field names.
2//!
3//! Override the default `ok`, `format`, `content`, and `meta` field names to
4//! match your existing API conventions.
5//!
6//! Run with:
7//! ```sh
8//! cargo run --example envelope_custom_fields
9//! ```
10
11use scriba::{
12    Format, Output,
13    Ui,
14    envelope::{EnvelopeConfig, EnvelopeFields, EnvelopeLayout, EnvelopeMode, Meta},
15};
16
17fn main() -> scriba::Result<()> {
18    let output = Output::new()
19        .title("Users")
20        .data("count", 42u64)
21        .paragraph("Query executed successfully.");
22
23    // --- Custom flat field names ---
24    let flat_ui = Ui::new()
25        .with_format(Format::Json)
26        .with_envelope(
27            EnvelopeConfig::default()
28                .with_mode(EnvelopeMode::Json)
29                .with_layout(EnvelopeLayout::Flat)
30                .with_fields(EnvelopeFields {
31                    ok_field: "success".into(),
32                    format_field: "type".into(),
33                    content_field: "result".into(),
34                    meta_field: "context".into(),
35                }),
36        );
37
38    println!("=== Custom field names, flat ===");
39    println!(r#"  fields: success / type / result / context"#);
40    flat_ui.print(&output)?;
41
42    // --- Custom nested field names ---
43    let nested_ui = Ui::new()
44        .with_format(Format::Json)
45        .with_envelope(
46            EnvelopeConfig::default()
47                .with_mode(EnvelopeMode::Json)
48                .with_layout(EnvelopeLayout::Nested)
49                .with_fields(EnvelopeFields {
50                    ok_field: "success".into(),
51                    format_field: "type".into(),
52                    content_field: "data".into(),
53                    meta_field: "header".into(),
54                }),
55        );
56
57    let meta = Meta::default()
58        .with_command("users list".into())
59        .with_extra("region", "eu-west-1");
60
61    println!("\n=== Custom field names, nested ===");
62    println!(r#"  fields: success / type / data / header"#);
63    nested_ui.print_with_meta(&output, Some(&meta), true)?;
64
65    // --- Omit ok and format fields entirely ---
66    let minimal_ui = Ui::new()
67        .with_format(Format::Json)
68        .with_envelope(
69            EnvelopeConfig::default()
70                .with_mode(EnvelopeMode::Json)
71                .with_show_ok(false)
72                .with_show_format(false),
73        );
74
75    println!("\n=== Envelope without ok or format fields ===");
76    minimal_ui.print(&output)?;
77
78    Ok(())
79}