acts-tag

Crates.ioacts-tag
lib.rsacts-tag
version0.2.0
sourcesrc
created_at2023-04-11 11:46:54.745788
updated_at2023-04-17 11:12:45.416176
descriptionA simple trait to markup the struct and help recognize the struct's attributes
homepage
repositoryhttps://github.com/yaojianpin/acts-tag.git
max_upload_size
id835967
size6,546
(yaojianpin)

documentation

README

acts-tag

A simple trait to mark up the struct and help recognize the struct's attributes. A simple trait to mark up the struct and help recognize the struct's attributes.

Example

use acts_tag::{Tags, Value};
use std::any::Any;
#[derive(Tags)]
pub struct MyTags {
    // tag(id) change the key to id
    #[tag(id)]
    pub my_id: String,
    #[tag]
    pub tag1: String,
    #[tag]
    pub tag2: i32,
    pub others: String,
}
fn main() {
    let t = MyTags {
        my_id: "my id".to_string(),
        tag1: "a".to_string(),
        tag2: 5,
        others: "others".to_string(),
    };
    assert_eq!(MyTags::keys(), vec![ "id", "tag1", "tag2" ]);
    
    for tag_key in &MyTags::keys() {
        let value = t.value(tag_key).unwrap();
        println!("value {} = {:?}", tag_key, value);
    }
    let value = t.value("id").unwrap();
    assert_eq!(value.real::<String>().unwrap(), t.my_id);

    let value = t.value("tag1").unwrap();
    assert_eq!(value.real::<String>().unwrap(), t.tag1);

    let value = t.value("tag2").unwrap();
    assert_eq!(value.real::<i32>().unwrap(), t.tag2);

    let value = t.value("others");
    assert_eq!(value, None);

    let is_tag = t.is_tag("others");
    assert_eq!(is_tag, false);

    let is_tag = t.is_tag("tag2");
    assert_eq!(is_tag, true);
}
Commit count: 6

cargo fmt