#table #cli #format #macro-derive

macro structable_derive

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

4 releases

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

#832 in Command-line interface

Download history 133/week @ 2024-02-10 82/week @ 2024-02-17 175/week @ 2024-02-24 35/week @ 2024-03-02 86/week @ 2024-03-09 47/week @ 2024-03-16 27/week @ 2024-03-23 19/week @ 2024-03-30 9/week @ 2024-04-06 1/week @ 2024-04-13

58 downloads per month
Used in openstack_cli

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.7–1.1MB
~26K SLoC