Crates.io | macro_macro |
lib.rs | macro_macro |
version | 0.1.0 |
source | src |
created_at | 2020-05-02 00:38:21.073936 |
updated_at | 2020-05-02 00:38:21.073936 |
description | Macro templating library for DRYer structs |
homepage | |
repository | https://github.com/sjones4/macro-macro/ |
max_upload_size | |
id | 236425 |
size | 12,309 |
Rust macro templating library for cleaner code.
Declare a named template with the macros you want to use. The template treats some items specially:
__
: one double underscore can be used alone as a name wildcard or for prefix and/or suffix matching!
: for a type wildcardName prefix matching on struct and field name:
macro_template!(prefix_match = {
#...
struct STRUCT_PREFIX__ {
#... FIELD_PREFIX__: !,
}
});
Type matching for field macros:
macro_template!(field_type_match = {
struct __ {
#... __: i32,
}
});
which would add the declared attribute macro(s) to any fields in a struct with type i32
.
Full example for serde annotation macros:
use macro_macro::*;
use serde::{Deserialize, Serialize};
macro_template!(serde_service_model = {
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct __ {
#[serde(skip_serializing_if = "Option::is_none")] __: Option<!>,
}
});
#[macro_macro(serde_service_model)]
pub struct Service {
pub version: String,
pub metadata: Option<Metadata>,
...
}
#[macro_macro(serde_service_model)]
pub struct Metadata {
pub api_version: Option<String>,
pub endpoint_prefix: Option<String>,
...
}
which is equivalent to annotating each struct directly with:
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
and each field of type Option
with:
#[serde(skip_serializing_if = "Option::is_none")]
Licensed under:
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.