#output #json #structured #value #displayed #cli #line #printed

structured-output

A library for structured CLI output that can be displayed as either printed lines or JSON

2 releases

0.1.1 May 10, 2023
0.1.0 May 10, 2023

#1916 in Encoding

Download history 122/week @ 2025-03-09 36/week @ 2025-03-16 2/week @ 2025-03-23 35/week @ 2025-03-30 94/week @ 2025-04-06 30/week @ 2025-04-13 36/week @ 2025-04-20 61/week @ 2025-04-27 32/week @ 2025-05-04 57/week @ 2025-05-11 16/week @ 2025-05-18 31/week @ 2025-05-25 43/week @ 2025-06-01 19/week @ 2025-06-08 9/week @ 2025-06-15 39/week @ 2025-06-22

110 downloads per month
Used in 8 crates

Apache-2.0

5KB

structured-output Rust crate

StructuredOutput is a Rust library that provides a struct for handling structured output with both a string and a JSON value.

Installation

To use this library in your Rust project, add the following to your Cargo.toml file:

[dependencies]
structured_output = "0.1.0"

Usage

You can then create a new StructuredOutput instance using the new method, which takes a string and a JSON value as arguments:

let lines = "Hello world!";
let json = serde_json::json!({ "message": lines });
let output = StructuredOutput::new(lines, json.clone());

You can retrieve the string and JSON value separately using the lines and json methods:

assert_eq!(output.lines(), "Hello world!");
assert_eq!(output.json(), &json);

You can also get the JSON value as a JSON string or a pretty-printed JSON string using the to_json_string and to_json_pretty methods:

assert_eq!(output.to_json_string(), "{\"message\":\"Hello world!\"}");
assert_eq!(output.to_json_pretty(), "{\n  \"message\": \"Hello world!\"\n}");

The StructuredOutput implementation of the Display trait defaults to the string value. You can print the string value directly with the println! macro:

println!("{}", output);

License

This library is licensed under the Apache 2.0 License.

Dependencies

~0.6–1.5MB
~31K SLoC