has_fields 0.1.1

Some macros helpful for processing forms with optional fields
Documentation
  • Coverage
  • 0%
    0 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 5.94 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 256.37 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • lihe07/has_fields
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • lihe07

has_fields

Some macros helpful for processing forms with optional fields.

Usage

For instance, if you got a form like this:

let form = MyForm {
    id: 1,
    name: Some("name".to_string()),
    email: Some("email@example.com".to_string()),
    phone: None,
}

Here are some macros that might help you:

  1. has_fields::has_fields!: Check if a struct has some fields. Returns a boolean.

    has_fields!(form, "name", "email") // true
    
  2. has_fields::require_fields: Check if a struct has some fields. Returns a Result<(), Vec<&'static str>>.

    require_fields!(form, "name", "email") // Ok(())
    require_fields!(form, "name", "email", "phone") // Err(vec!["phone"])
    

Moreover, you can derive HasFields trait for your struct, and use these methods:

  1. num_fields: Get the number of Some(...) or non-optional fields in a struct.

    form.num_fields() // 2
    

License

The Unlicense

Contributing

If you have any ideas, feel free to open an issue or a PR.