#描述 #序列化 #结构体

descriptor

一个简单易用的结构体描述符

4 个版本

0.0.4 2021 年 9 月 15 日
0.0.3 2021 年 9 月 15 日
0.0.2 2021 年 9 月 15 日
0.0.1 2021 年 9 月 14 日
0.0.1-patch-readme 2021 年 9 月 15 日

#11 in #序列化器

Download history 150/week @ 2024-04-18 128/week @ 2024-04-25 85/week @ 2024-05-02 114/week @ 2024-05-09 134/week @ 2024-05-16 211/week @ 2024-05-23 154/week @ 2024-05-30 172/week @ 2024-06-06 125/week @ 2024-06-13 256/week @ 2024-06-20 120/week @ 2024-06-27 148/week @ 2024-07-04 170/week @ 2024-07-11 106/week @ 2024-07-18 126/week @ 2024-07-25 108/week @ 2024-08-01

547 每月下载

Apache-2.0

34KB
432

简单的结构体描述符

Crates.io Crates.io License Build Status Coverage Status

轻松将 Rust 结构体格式化为单个元素或表格

示例

use descriptor::{object_describe_to_string, table_describe_to_string, Descriptor};

#[derive(Descriptor)]
struct User {
    name: String,
    age: i32,
    address: Address,
}

#[derive(Descriptor)]
struct Address {
    street: String,
    town: String,
}

fn main() {
    let user1 = User {
        name: "Adrien".to_string(),
        age: 32,
        address: Address {
            street: "Main street".to_string(),
            town: "NY".to_string()
        }
    };
    let user2 = User {
        name: "Corentin".to_string(),
        age: 40,
        address: Address {
            street: "10 rue de la paix".to_string(),
            town: "Paris".to_string()
        }
    };
    let description = object_describe_to_string(&user1).unwrap();

    assert_eq!(r#"
     Name:    Adrien
     Age:     32
     Address:
       Street: Main street
       Town:   NY
     "#, description);

    let table = table_describe_to_string(&vec![user1, user2]).unwrap();

    assert_eq!(r#"
     NAME     AGE ADDRESS.STREET    ADDRESS.TOWN
     Adrien   32  Main street       NY
     Corentin 40  10 rue de la paix Paris
     "#, format!("\n{}", table));
}

依赖项

~2MB
~41K SLoC