Crates.io | avocado-schema-derive |
lib.rs | avocado-schema-derive |
version | 0.8.0 |
source | src |
created_at | 2023-11-02 06:04:49.926352 |
updated_at | 2023-11-03 20:33:46.942034 |
description | A derive macro to support runtime reflection of struct values |
homepage | |
repository | https://github.com/zwnormal/avocado-schema/ |
max_upload_size | |
id | 1022311 |
size | 7,564 |
Avocado Schema defines the following enum FieldValue
for runtime reflection of struct
's structure and values:
#[derive(Debug, Clone, PartialEq)]
pub enum FieldValue {
String(String),
Integer(i64),
UInteger(u64),
Float(f64),
Boolean(bool),
Object(BTreeMap<String, FieldValue>),
Array(Vec<FieldValue>),
Email(EmailAddress),
DateTime(DateTime<Utc>),
Date(NaiveDate),
Time(NaiveTime),
Null,
}
This macro Reflect
is for deriving FieldValue
enum for struct
:
#[derive(Reflect)]
struct Client {
#[reflect("firstName")]
first_name: String,
#[reflect("lastName")]
last_name: String,
age: u64,
#[reflect(ignore)]
email: String
}
#[test]
fn main() {
let client = Client {
first_name: "Robert".to_string(),
last_name: "Li".to_string(),
age: 30,
email: "admin@avocado.com".to_string(),
};
assert_eq!(
client.field_value(),
FieldValue::Object(BTreeMap::from([
(
"firstName".to_string(),
FieldValue::String("Robert".to_string())
),
("lastName".to_string(), FieldValue::String("Li".to_string())),
("age".to_string(), FieldValue::UInteger(30))
]))
)
}