Crate json_to_table

source ·
Expand description

The crate contains a json_to_table function which builds a Table from an ordinary json.

You can build the table either generally or in a squash. See the examples below.

use serde_json::json;
use json_to_table::json_to_table;

let value = json!(
    {
        "name": "John Doe",
        "age": 43,
        "address": {
            "street": "10 Downing Street",
            "city": "London"
        },
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    }
);

// recursive table
let table = json_to_table(&value).to_string();

println!("{}", table);

assert_eq!(
    table,
    concat!(
        "+---------+----------------------------------+\n",
        "| address | +--------+---------------------+ |\n",
        "|         | | city   |  London             | |\n",
        "|         | +--------+---------------------+ |\n",
        "|         | | street |  10 Downing Street  | |\n",
        "|         | +--------+---------------------+ |\n",
        "+---------+----------------------------------+\n",
        "| age     |  43                              |\n",
        "+---------+----------------------------------+\n",
        "| name    |  John Doe                        |\n",
        "+---------+----------------------------------+\n",
        "| phones  | +---------------+                |\n",
        "|         | |  +44 1234567  |                |\n",
        "|         | +---------------+                |\n",
        "|         | |  +44 2345678  |                |\n",
        "|         | +---------------+                |\n",
        "+---------+----------------------------------+",
    ),
);

// squash tables together
let table = json_to_table(&value).collapse().to_string();

assert_eq!(
    table,
    concat!(
        "+---------+--------+-------------------+\n",
        "| address | city   | London            |\n",
        "|         +--------+-------------------+\n",
        "|         | street | 10 Downing Street |\n",
        "+---------+--------+-------------------+\n",
        "| age     | 43                         |\n",
        "+---------+----------------------------+\n",
        "| name    | John Doe                   |\n",
        "+---------+----------------------------+\n",
        "| phones  | +44 1234567                |\n",
        "|         +----------------------------+\n",
        "|         | +44 2345678                |\n",
        "+---------+----------------------------+",
    ),
);

Structs

Enums

  • The structure represents a table mode for a given entity, either it will be rendered vertically or horizontally.

Functions