zhiburt / tabled

An easy to use library for pretty print tables of Rust structs and enums.
MIT License
2.01k stars 84 forks source link

Rework `#[tabled(format(""))` macro && refactorings #418

Closed zhiburt closed 3 months ago

zhiburt commented 3 months ago

@strange-dv Look at this

I actually wasn't expecting to achieve it but here we are.

use tabled::{settings::Style, Table, Tabled};

#[derive(Tabled)]
struct User {
    #[tabled(format("{}.{}.{}.{}", self.ip[0], self.ip[1], self.ip[2], self.ip[3]))]
    ip: [u8; 4],
    mask: String,
    password: String,
}

impl User {
    fn new(ip: [u8; 4], mask: &str, password: &str) -> Self {
        Self {
            ip,
            mask: mask.to_string(),
            password: password.to_string(),
        }
    }
}

fn main() {
    let data = [
        User::new([127, 0, 0, 1], "", "11111111"),
        User::new([127, 0, 0, 1], "", "22222222"),
        User::new([127, 0, 0, 1], "", "33333333"),
        User::new([127, 0, 0, 1], "", "44444444"),
    ];

    let mut table = Table::new(data);
    table.with(
        Style::modern_rounded()
            .remove_horizontal()
            .remove_vertical(),
    );

    println!("{table}");
}
╭───────────────────────────╮
│ ip         mask  password │
│ 127.0.0.1        11111111 │
│ 127.0.0.1        22222222 │
│ 127.0.0.1        33333333 │
│ 127.0.0.1        44444444 │
╰───────────────────────────╯
zhiburt commented 3 months ago

OR like this

//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
//! [`format`] to beatifuly castomize the resulting values, be used for table contraction.

use tabled::{settings::Style, Table, Tabled};

#[derive(Tabled)]
struct User {
    #[tabled(format("{}.{}.{}.{}", self.ip[0], self.ip[1], self.ip[2], self.ip[3]))]
    ip: [u8; 4],
    #[tabled(inline)]
    password: Password,
}

#[derive(Tabled)]
enum Password {
    #[tabled(inline)]
    Mask {
        #[tabled(format("T {}", str::to_uppercase(self.text)))]
        text: String,
        #[tabled(format = "F {}")]
        factor: usize,
    },
    #[tabled(inline)]
    Plain(String),
}

impl Password {
    fn mask(s: &str, f: usize) -> Self {
        Self::Mask {
            text: s.to_string(),
            factor: f,
        }
    }

    fn plain(s: &str) -> Self {
        Self::Plain(s.to_string())
    }
}

impl User {
    fn new(ip: [u8; 4], password: Password) -> Self {
        Self { ip, password }
    }
}

fn main() {
    let data = [
        User::new([127, 0, 0, 1], Password::mask("11111111", 0)),
        User::new([127, 0, 0, 1], Password::mask("1", 1000)),
        User::new([127, 0, 0, 3], Password::plain("3333")),
    ];

    let mut table = Table::new(data);
    table.with(
        Style::modern_rounded()
            .remove_horizontal()
            .remove_vertical(),
    );

    println!("{table}");
}
╭─────────────────────────────────────╮
│ ip         text        factor  0    │
│ 127.0.0.1  T 11111111  F 0          │
│ 127.0.0.1  T 1         F 1000       │
│ 127.0.0.3                      3333 │
╰─────────────────────────────────────╯