#table #format #serialize #struct #derive

tablefy

An easy way to display any struct as a table!

4 releases

0.1.3 Jul 29, 2019
0.1.2 Jul 26, 2019
0.1.1 Jul 25, 2019
0.1.0 Jul 25, 2019

#18 in #struct

Download history 91/week @ 2020-12-24 95/week @ 2020-12-31 97/week @ 2021-01-07 128/week @ 2021-01-14 149/week @ 2021-01-21 96/week @ 2021-01-28 78/week @ 2021-02-04 123/week @ 2021-02-11 161/week @ 2021-02-18 136/week @ 2021-02-25 97/week @ 2021-03-04 130/week @ 2021-03-11 95/week @ 2021-03-18 198/week @ 2021-03-25 6/week @ 2021-04-01 7/week @ 2021-04-08

425 downloads per month

MPL-2.0 license

9KB

tablefy

tablefy tablefy

To check for updates see the changelog.

use tablefy_derive::Tablefy;
use tablefy::Tablefy;

// This struct now implements the tablefy trait
#[derive(Tablefy)]
pub struct Basic {

    #[header(name = "Hmm... Nice Header")]
    pub something: String,

    #[header(name = "We Have Here!")]
    pub otherthing: i16,

    #[header(name = "Don't You Agree?")]
    pub newthing: i8,

    pub maybe: Option<String>
}

fn main() {
    // Creating a vector of structs...
    let basic = vec![Basic {
        something: String::from("a"),
        otherthing: 2,
        newthing: 3,
        maybe: None
    }, Basic {
        something: String::from("b"),
        otherthing: 3,
        newthing: 4,
        maybe: Some(String::from("x"))
    }, Basic {
        something: String::from("c"),
        otherthing: 5,
        newthing: 8,
        maybe: None
    }];

    // Turning them into a Table struct...
    let table = tablefy::into_table(&basic);

    // Or if you just want the string...
    println!("{}", tablefy::into_string(&basic));
}
+--------------------+---------------+------------------+-------+
| Hmm... Nice Header | We Have Here! | Don't You Agree? | maybe |
+====================+===============+==================+=======+
| a                  | 2             | 3                |       |
+--------------------+---------------+------------------+-------+
| b                  | 3             | 4                | x     |
+--------------------+---------------+------------------+-------+
| c                  | 5             | 8                |       |
+--------------------+---------------+------------------+-------+

This crate serves as an extension of prettytable by specifying a Tablefy trait in order to turn any struct (whose members implement Display) to turn into a Table object.

As a result, this means that prettytable is a dependency. You won't be able to use this crate without also adding prettytable.

If you'd like to get the full functionality of this crate (with proc_macros), be sure to check out tablefy_derive!.

Future updates

Currently there are two major improvements I have in mind for this crate.

  • Fields can be tagged to customize the header name.
    • This has now been implemented! Be sure to update tablefy_derive.
  • Fields can be tagged to print using {:?} and {:#?} instead of {}

License: MPL-2.0

Dependencies

~2.5MB
~32K SLoC