Crate toml_to_table

source ·
Expand description

The library creates a pretty table out of a toml::Value.

The are 2 types of tables you can create. 1. Embeded (default) 2. Collapsed

You can configure table using TomlTable.

Examples

Embeded table.

let data = r#"
[game-config]
window_size = [800, 600]
window_title = "PAC-MAN"
fullscreen = false
mouse_sensitivity = 1.4

[game-config.key_bindings]
up = "Up"
down = "Down"
left = "Left"
right = "Right"

[game-config.difficulty_options]
start_difficulty = "Easy"
adaptive = false
"#;

let scene = toml::from_str(data).unwrap();
let table = toml_to_table::to_string(&scene);

assert_eq!(
    table,
    "+-------------+---------------------------------------------------------+\n\
     | game-config | +--------------------+--------------------------------+ |\n\
     |             | | difficulty_options | +------------------+---------+ | |\n\
     |             | |                    | | adaptive         |  false  | | |\n\
     |             | |                    | +------------------+---------+ | |\n\
     |             | |                    | | start_difficulty |  Easy   | | |\n\
     |             | |                    | +------------------+---------+ | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     |             | | fullscreen         |  false                         | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     |             | | key_bindings       | +-------+---------+            | |\n\
     |             | |                    | | down  |  Down   |            | |\n\
     |             | |                    | +-------+---------+            | |\n\
     |             | |                    | | left  |  Left   |            | |\n\
     |             | |                    | +-------+---------+            | |\n\
     |             | |                    | | right |  Right  |            | |\n\
     |             | |                    | +-------+---------+            | |\n\
     |             | |                    | | up    |  Up     |            | |\n\
     |             | |                    | +-------+---------+            | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     |             | | mouse_sensitivity  |  1.4                           | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     |             | | window_size        | +-------+                      | |\n\
     |             | |                    | |  800  |                      | |\n\
     |             | |                    | +-------+                      | |\n\
     |             | |                    | |  600  |                      | |\n\
     |             | |                    | +-------+                      | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     |             | | window_title       |  PAC-MAN                       | |\n\
     |             | +--------------------+--------------------------------+ |\n\
     +-------------+---------------------------------------------------------+"
);

Collapsed table.

let data = r#"
[game-config]
window_size = [800, 600]
window_title = "PAC-MAN"
fullscreen = false
mouse_sensitivity = 1.4

[game-config.key_bindings]
up = "Up"
down = "Down"
left = "Left"
right = "Right"

[game-config.difficulty_options]
start_difficulty = "Easy"
adaptive = false
"#;

let scene = toml::from_str(data).unwrap();
let table = toml_to_table::to_string_collapsed(&scene);

assert_eq!(
    table,
    "+-------------+--------------------+------------------+-------+\n\
     | game-config | difficulty_options | adaptive         | false |\n\
     |             |                    +------------------+-------+\n\
     |             |                    | start_difficulty | Easy  |\n\
     |             +--------------------+------------------+-------+\n\
     |             | fullscreen         | false                    |\n\
     |             +--------------------+-------+------------------+\n\
     |             | key_bindings       | down  | Down             |\n\
     |             |                    +-------+------------------+\n\
     |             |                    | left  | Left             |\n\
     |             |                    +-------+------------------+\n\
     |             |                    | right | Right            |\n\
     |             |                    +-------+------------------+\n\
     |             |                    | up    | Up               |\n\
     |             +--------------------+-------+------------------+\n\
     |             | mouse_sensitivity  | 1.4                      |\n\
     |             +--------------------+--------------------------+\n\
     |             | window_size        | 800                      |\n\
     |             |                    +--------------------------+\n\
     |             |                    | 600                      |\n\
     |             +--------------------+--------------------------+\n\
     |             | window_title       | PAC-MAN                  |\n\
     +-------------+--------------------+--------------------------+"
);

TomlTable style configuration (embeded)

use toml_to_table::TomlTable;
use tabled::settings::{Padding, Style};

let data = r#"
[game-config]
window_size = [800, 600]
window_title = "PAC-MAN"
fullscreen = false
mouse_sensitivity = 1.4

[game-config.key_bindings]
up = "Up"
down = "Down"
left = "Left"
right = "Right"

[game-config.difficulty_options]
start_difficulty = "Easy"
adaptive = false
"#;

let scene = toml::from_str(data).unwrap();

let table = TomlTable::new(&scene)
    .with(Padding::zero())
    .with(Style::modern())
    .to_string();

assert_eq!(
    table,
    "┌───────────┬─────────────────────────────────────────────┐\n\
     │game-config│┌──────────────────┬────────────────────────┐│\n\
     │           ││difficulty_options│┌────────────────┬─────┐││\n\
     │           ││                  ││adaptive        │false│││\n\
     │           ││                  │├────────────────┼─────┤││\n\
     │           ││                  ││start_difficulty│Easy │││\n\
     │           ││                  │└────────────────┴─────┘││\n\
     │           │├──────────────────┼────────────────────────┤│\n\
     │           ││fullscreen        │false                   ││\n\
     │           │├──────────────────┼────────────────────────┤│\n\
     │           ││key_bindings      │┌─────┬─────┐           ││\n\
     │           ││                  ││down │Down │           ││\n\
     │           ││                  │├─────┼─────┤           ││\n\
     │           ││                  ││left │Left │           ││\n\
     │           ││                  │├─────┼─────┤           ││\n\
     │           ││                  ││right│Right│           ││\n\
     │           ││                  │├─────┼─────┤           ││\n\
     │           ││                  ││up   │Up   │           ││\n\
     │           ││                  │└─────┴─────┘           ││\n\
     │           │├──────────────────┼────────────────────────┤│\n\
     │           ││mouse_sensitivity │1.4                     ││\n\
     │           │├──────────────────┼────────────────────────┤│\n\
     │           ││window_size       │┌───┐                   ││\n\
     │           ││                  ││800│                   ││\n\
     │           ││                  │├───┤                   ││\n\
     │           ││                  ││600│                   ││\n\
     │           ││                  │└───┘                   ││\n\
     │           │├──────────────────┼────────────────────────┤│\n\
     │           ││window_title      │PAC-MAN                 ││\n\
     │           │└──────────────────┴────────────────────────┘│\n\
     └───────────┴─────────────────────────────────────────────┘"
);

TomlTable style configuration (collapsed)

use toml_to_table::TomlTable;
use tabled::settings::{Padding, Style};

let data = r#"
[game-config]
window_size = [800, 600]
window_title = "PAC-MAN"
fullscreen = false
mouse_sensitivity = 1.4

[game-config.key_bindings]
up = "Up"
down = "Down"
left = "Left"
right = "Right"

[game-config.difficulty_options]
start_difficulty = "Easy"
adaptive = false
"#;

let scene = toml::from_str(data).unwrap();

let table = TomlTable::new(&scene)
    .with(Padding::zero())
    .with(Style::modern())
    .collapse()
    .to_string();

assert_eq!(
    table,
    "┌───────────┬──────────────────┬────────────────┬─────┐\n\
     │game-config│difficulty_options│adaptive        │false│\n\
     │           │                  ├────────────────┼─────┤\n\
     │           │                  │start_difficulty│Easy │\n\
     │           ├──────────────────┼────────────────┴─────┤\n\
     │           │fullscreen        │false                 │\n\
     │           ├──────────────────┼─────┬────────────────┤\n\
     │           │key_bindings      │down │Down            │\n\
     │           │                  ├─────┼────────────────┤\n\
     │           │                  │left │Left            │\n\
     │           │                  ├─────┼────────────────┤\n\
     │           │                  │right│Right           │\n\
     │           │                  ├─────┼────────────────┤\n\
     │           │                  │up   │Up              │\n\
     │           ├──────────────────┼─────┴────────────────┤\n\
     │           │mouse_sensitivity │1.4                   │\n\
     │           ├──────────────────┼──────────────────────┤\n\
     │           │window_size       │800                   │\n\
     │           │                  ├──────────────────────┤\n\
     │           │                  │600                   │\n\
     │           ├──────────────────┼──────────────────────┤\n\
     │           │window_title      │PAC-MAN               │\n\
     └───────────┴──────────────────┴──────────────────────┘"
);

Structs

Enums

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

Functions

  • The function converts a given Value to a pretty table, recursively creating new tables if nessary.
  • The function converts a given Value to a pretty table, recursively creating new tables if nessary.