avocado-schema-derive

Crates.ioavocado-schema-derive
lib.rsavocado-schema-derive
version0.8.0
sourcesrc
created_at2023-11-02 06:04:49.926352
updated_at2023-11-03 20:33:46.942034
descriptionA derive macro to support runtime reflection of struct values
homepage
repositoryhttps://github.com/zwnormal/avocado-schema/
max_upload_size
id1022311
size7,564
Wei Zheng (zwnormal)

documentation

README

Avocado Schema Derive

Avocado Schema defines the following enum FieldValue for runtime reflection of struct's structure and values:

Crates.io MIT licensed

#[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))
        ]))
    )
}
Commit count: 93

cargo fmt