Crates.io | acts-tag |
lib.rs | acts-tag |
version | 0.2.0 |
source | src |
created_at | 2023-04-11 11:46:54.745788 |
updated_at | 2023-04-17 11:12:45.416176 |
description | A simple trait to markup the struct and help recognize the struct's attributes |
homepage | |
repository | https://github.com/yaojianpin/acts-tag.git |
max_upload_size | |
id | 835967 |
size | 6,546 |
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.
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);
}