macro_rules! define_attr_grammar {
($($grammar:tt)*) => { ... };
}Expand description
Define an attribute grammar with type-safe parsing.
This macro generates:
- The attribute types (enum + structs)
- A
__parse_attr!macro for parsing attribute tokens - Re-exports for the necessary proc-macros
§Example
ⓘ
facet::define_attr_grammar! {
pub enum Attr {
/// Skip this field entirely
Skip,
/// Rename to a different name
Rename(&'static str),
/// Database column configuration
Column(Column),
}
pub struct Column {
/// Override the database column name
pub name: Option<&'static str>,
/// Mark as primary key
pub primary_key: bool,
}
}This generates an Attr enum and Column struct with the specified fields,
along with a __parse_attr! macro that can parse attribute syntax like:
skip→Attr::Skiprename("users")→Attr::Rename("users")column(name = "user_id", primary_key)→Attr::Column(Column { name: Some("user_id"), primary_key: true })
§Supported Field Types
| Grammar Type | Rust Type | Syntax |
|---|---|---|
bool | bool | flag or flag = true |
&'static str | &'static str | name = "value" |
Option<&'static str> | Option<&'static str> | name = "value" (optional) |
Option<bool> | Option<bool> | flag = true (optional) |