Crates.io | syn-args |
lib.rs | syn-args |
version | 0.4.0 |
source | src |
created_at | 2024-08-24 10:20:02.883262 |
updated_at | 2024-09-25 14:55:41.93135 |
description | syn-args is a library for parsing function arguments. |
homepage | https://github.com/nidrs/nidrs.git |
repository | https://github.com/nidrs/nidrs.git |
max_upload_size | |
id | 1350161 |
size | 684,666 |
SynArgs is a powerful and easy-to-use string pattern matching and parsing tool that can parse strings into corresponding data structures. It is widely applicable to various parameter parsing needs and can be flexibly extended to suit complex parsing scenarios.
F(P1, P2)
(P1, P2)
P1, P2
ArgsParse
syn_args::derive::declare
syn_args::derive::proc_attribute
Add the dependencies to your project:
cargo add syn
cargo add syn-args
syn-args = { features = ["loose_mode"] }
def::Bool
and def::Array
can be used without def::Options
if they are optional parameters.Below are some basic examples of string parsing:
For more examples, see the GitHub sample file
use syn::Error;
use syn_args::{def, derive::ArgsParse, ArgsParse, Formal};
#[derive(Debug, PartialEq, ArgsParse)]
pub enum ModuleArgs {
F1(def::Int, def::Int),
F2(def::Int),
F3(def::Expr),
F4(def::Array<def::Expr>),
F5(ModuleSubObj),
F6(def::Array<ModuleSubObj>),
}
#[derive(Debug, PartialEq, ArgsParse)]
pub struct ModuleSubObj {
pub imports: def::Array<def::Expr>,
}
// Sample test function
fn test_formal_f3() {
let res = ModuleArgs::parse("F(Hello)").unwrap();
println!("{:?}", res);
assert_eq!(res, ModuleArgs::F3(def::Expr("Hello".to_string())));
}
// More test functions...
fn main() {
test_formal_f3();
}
TokenStream parsing examples show how to use syn_args to parse TokenStreams with complex nested structures:
View the full example: GitHub Link
Type definition:
#[derive(Debug, Clone, ArgsParse)]
pub struct ModuleOptions {
pub imports: def::Array<def::Expr>,
pub controllers: def::Array<def::Expr>,
pub services: def::Array<def::Expr>,
pub exports: def::Array<def::Expr>,
pub interceptors: def::Array<def::Expr>,
}
Usage example:
let module_args = attr.meta.to_token_stream();
let module_options = syn::parse2::<syn_args::SynArgs>(module_args).unwrap().arguments::<syn_args::Arguments>().unwrap();
let module_options: ModuleOptions = module_options.try_into().unwrap();
Macro usage greatly simplifies the parameter parsing process and is the recommended advanced usage:
View the full example: GitHub Link
Usage example:
#[default_uses(LogInterceptor, LogInterceptorB, LogInterceptorC)]
pub struct AppModule;
Macro definition:
#[syn_args::derive::declare(def::Expr, def::Extends<def::Expr>)]
#[syn_args::derive::proc_attribute]
pub fn default_uses(args: Args, input: TokenStream) -> TokenStream {
let args: Vec<def::Expr> = match args {
Args::F1(first, other) => {
// first => LogInterceptor
// other => [LogInterceptorB, LogInterceptorC]
let mut args = vec![first];
args.append(&mut other.clone());
args
}
_ => panic!("Invalid argument"),
};
let inter_names = args.iter().map(|arg| arg.to_path_name().unwrap()).collect::<Vec<String>>();
DEFAULT_INTERS.lock().unwrap().append(&mut inter_names.clone());
return input;
}
Note: This method is only applicable in the macro library development environment with
[lib] proc-macro = true
configuration.
For specific type definitions, refer to the Type Definition Documentation.
License: MIT