Crates.io | has_fields |
lib.rs | has_fields |
version | 0.1.1 |
source | src |
created_at | 2023-01-04 02:40:45.618655 |
updated_at | 2023-01-08 07:41:09.771507 |
description | Some macros helpful for processing forms with optional fields |
homepage | |
repository | https://github.com/lihe07/has_fields |
max_upload_size | |
id | 750547 |
size | 5,943 |
Some macros helpful for processing forms with optional fields.
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:
has_fields::has_fields!
: Check if a struct has some fields. Returns a boolean.
has_fields!(form, "name", "email") // true
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:
num_fields
: Get the number of Some(...)
or non-optional fields in a struct.
form.num_fields() // 2
The Unlicense
If you have any ideas, feel free to open an issue or a PR.