Crates.io | synext |
lib.rs | synext |
version | 0.3.0 |
source | src |
created_at | 2024-07-27 13:07:11.039225 |
updated_at | 2024-07-28 10:22:01.40698 |
description | A simple extension library for syn crate to help developers quickly develop derive macros |
homepage | https://github.com/photowey/synext |
repository | https://github.com/photowey/synext |
max_upload_size | |
id | 1317284 |
size | 49,450 |
synext
A simple extension library for syn
crate to help developers quickly develop derive macros
Acknowledgment
This project, synext
, was developed with significant inspiration from the open-source
project proc_macros. Special
thanks to the contributors of proc_macros for their excellent work.
Usage
Add this to your Cargo.toml
:
[dependencies]
synext = "0.2"
APIs
Fields
named
// input = TokenStream
let derive_input = try_derive_input(input);
let named_fields = try_parse_named_fields( & derive_input);
unnamed
// input = TokenStream
let derive_input = try_derive_input(input);
let unnamed_fields = try_parse_unnamed_fields( & derive_input);
match
// input = TokenStream
let derive_input = try_derive_input(input);
let fields = try_match_fields( & derive_input);
Types
Option
unwrap Option
inner type.
pub fn try_unwrap_option(ty: &Type) -> &Type { ... }
Vec
unwrap Vec
inner type.
pub fn try_unwrap_vec(ty: &Type) -> &Type { ... }
unwrap_types
pub fn try_unwrap_types<'a>(ident: &str, target_types: usize, ty: &'a Type) -> Option<Vec<&'a Type>> { ... }
inner_types
pub fn try_extract_inner_types(ty: &Type) -> Option<Vec<&Type>> { ... }
Predicate
Option
pub fn try_predicate_is_option(ty: &Type) -> bool { ... }
// @since 0.2.0
pub fn try_predicate_is_not_option(ty: &Type) -> bool { ... }
Vec
pub fn try_predicate_is_vec(ty: &Type) -> bool { ... }
// @since 0.2.0
pub fn try_predicate_is_not_vec(ty: &Type) -> bool { ... }
Ident
pub fn try_predicate_is_ident(ident: &str, path: &Path) -> bool { ... }
pub fn try_predicate_is_not_ident(ident: &str, path: &Path) -> bool { ... }
segments
pub fn try_predicate_path_segments_is_not_empty(path: &Path) -> bool { ... }
pub fn try_predicate_path_segments_is_empty(path: &Path) -> bool { ... }
Derive attribute
Try to extract the specified path attribute value from a field's attributes.
// @since 0.2.0
pub fn try_extract_field_attribute_path_attribute(...) -> syn::Result<Option<syn::Ident>> { ... }
Attribute macro
kv
extern crate proc_macro;
use proc_macro::TokenStream;
use std::sync::Arc;
#[proc_macro_attribute]
pub fn component(args: TokenStream, item: TokenStream) -> TokenStream {
// ...
}
pub struct HelloService {
// ...
}
#[component(value = "helloController")] // kv
pub struct HelloController {
hello_service: Arc<HelloService>,
}
->
try_extract_attribute_args("value", args);
first
extern crate proc_macro;
use proc_macro::TokenStream;
use std::sync::Arc;
#[proc_macro_attribute]
pub fn component(args: TokenStream, item: TokenStream) -> TokenStream {
// ...
}
pub struct HelloService {
// ...
}
#[component("helloController")] // first
pub struct HelloController {
hello_service: Arc<HelloService>,
}
->
try_extract_attribute_first_args(args);