#table #cli #format #macro-derive

macro structable_derive

A crate for 'serializing' structs as Vec<Vec<String>> tables

5 releases

0.1.4 Jun 27, 2024
0.1.3 Mar 15, 2024
0.1.2 Feb 26, 2024
0.1.1 Feb 16, 2024
0.1.0 Feb 13, 2024

#943 in Command-line interface

Download history 113/week @ 2024-06-25 43/week @ 2024-07-02 15/week @ 2024-07-09 3/week @ 2024-07-16 25/week @ 2024-07-23 29/week @ 2024-07-30 14/week @ 2024-08-06 18/week @ 2024-08-13 30/week @ 2024-08-20 2/week @ 2024-08-27 14/week @ 2024-09-03 30/week @ 2024-09-10 33/week @ 2024-09-17 41/week @ 2024-09-24 47/week @ 2024-10-01 20/week @ 2024-10-08

158 downloads per month
Used in 2 crates

Apache-2.0

17KB
216 lines

StructTable derive macro

Most likely you do not want to use this crate directly. It is a helper for the OpenStack

This crate implements derive macros for converting structures (or structure vectors) as tables (vector of vector of strings - as rows and columns).

Depending on the OutputConfig it is possible to build a tuple (headers, rows) with selected fields

    use std::collections::BTreeSet;
    use serde_json::Value;
    use serde::Serialize;
    use structable_derive::StructTable;

    #[derive(Serialize, StructTable)]
    struct User {
        #[structable(title = "ID")]
        id: u64,
        first_name: &'static str,
        last_name: &'static str,
        #[structable(title = "Long(only in wide mode)", wide)]
        extra: &'static str,
        #[structable(optional, pretty)]
        complex_data: Option<Value>
    }

    struct OutputConfig {
        /// Limit fields (their titles) to be returned
        fields: BTreeSet<String>,
        /// Wide mode (additional fields requested)
        wide: bool,
        // Pretty-print
        pretty: bool
    }

    trait StructTable {
        fn build(&self, options: &OutputConfig) -> (Vec<String>, Vec<Vec<String>>);
    }

Example

    use std::collections::BTreeSet;
    use serde_json::{json, Value};
    use serde::Serialize;
    use structable_derive::StructTable;

    struct OutputConfig {
        /// Limit fields (their titles) to be returned
        fields: BTreeSet<String>,
        /// Wide mode (additional fields requested)
        wide: bool,
        // Pretty-print
        pretty: bool
    }

    trait StructTable {
        fn build(&self, options: &OutputConfig) -> (Vec<String>, Vec<Vec<String>>);
    }

    #[derive(Serialize, StructTable)]
    struct User {
        #[structable(title = "ID")]
        id: u64,
        first_name: &'static str,
        last_name: &'static str,
        #[structable(title = "Long(only in wide mode)", wide)]
        extra: &'static str,
        #[structable(optional, pretty)]
        complex_data: Option<Value>
    }

    let users = vec![
        User {
            id: 1,
            first_name: "Scooby",
            last_name: "Doo",
            extra: "Foo",
            complex_data: Some(json!({"a": "b", "c": "d"}))
        },
        User {
            id: 2,
            first_name: "John",
            last_name: "Cena",
            extra: "Bar",
            complex_data: None
        },
    ];
    let user = User {
        id: 1,
        first_name: "Scooby",
        last_name: "Doo",
        extra: "XYZ",
        complex_data: Some(json!({"a": "b", "c": "d"}))
    };

    let ln_fields: BTreeSet<String> = BTreeSet::from(["Last Name".to_string()]);

    let config = OutputConfig {
        fields: BTreeSet::new(), // ln_fields,
        wide: false,
        pretty: false
    };
    let data = user.build(&config);
    println!("Single user {:?} => {:?}", data.0, data.1);
    let data2 = users.build(&config);
    println!("multiple users {:?} => {:?}", data2.0, data2.1);

Single user ["Attribute", "Value"] => [["id", "1"], ["first_name", "Scooby"], ["last_name", "Doo"], ["long_only", "XYZ"]]
multiple user ["id", "first_name", "last_name", "long_only"] => [["1", "Scooby", "Doo", "Foo"], ["2", "John", "Cena", "Bar"]]

Dependencies

~0.6–1MB
~24K SLoC