#syn #def #parser #args #ident #array #formal

syn-args

syn-args 是一个用于解析函数参数的库

3 个不稳定版本

新版本 0.2.1 2024年8月25日
0.2.0 2024年8月25日
0.1.0 2024年8月24日

#1053 in 开发工具

Download history 92/week @ 2024-08-18

92 每月下载量

MIT 许可证

31KB
749

SynArgs

这是一个字符串模式匹配和解析到相应数据结构的工具。

安装

cargo add syn-args

使用

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::Ident),
    F4(def::Array<def::Ident>),
    F5(ModuleSubObj),
    F6(def::Array<ModuleSubObj>),
}


#[derive(Debug, PartialEq, ArgsParse)]
pub struct ModuleSubObj {
    pub imports: def::Array<def::Ident>,
}

fn test_formal_f3() {
    let res = ModuleArgs::parse("F(Hello)").unwrap();
    println!("{:?}", res);

    assert_eq!(res, ModuleArgs::F3(def::Ident("Hello".to_string())));
}

fn test_formal_f4() {
    let res = ModuleArgs::parse("F([Ident1, Ident2])").unwrap();
    println!("{:?}", res);

    assert_eq!(res, ModuleArgs::F4(def::Array(vec![def::Ident("Ident1".to_string()), def::Ident("Ident2".to_string())])));
}
fn test_formal_f5() {
    let res = ModuleArgs::parse("F({ imports: [Ident1, Ident2] })").unwrap();
    println!("{:?}", res);

    assert_eq!(res, ModuleArgs::F5(ModuleSubObj { imports: def::Array(vec![def::Ident("Ident1".to_string()), def::Ident("Ident2".to_string())]) }));
}

fn test_formal_f6() {
    let res = ModuleArgs::parse("F([{ imports: [Ident1, Ident2] }, { imports: [Ident3, Ident4] }])").unwrap();
    println!("{:?}", res);

    assert_eq!(
        res,
        ModuleArgs::F6(def::Array(vec![
            ModuleSubObj { imports: def::Array(vec![def::Ident("Ident1".to_string()), def::Ident("Ident2".to_string())]) },
            ModuleSubObj { imports: def::Array(vec![def::Ident("Ident3".to_string()), def::Ident("Ident4".to_string())]) }
        ]))
    );
}
fn test_formal_f6_3() {
    let res = ModuleArgs::parse("F([{ imports: [Ident1, Ident2], global: true, sub: { value: true } }, { imports: [Ident3, Ident4] }])").unwrap();
    println!("{:?}", res);

    assert_eq!(
        res,
        ModuleArgs::F6(def::Array(vec![
            ModuleSubObj {
                imports: def::Array(vec![def::Ident("Ident1".to_string()), def::Ident("Ident2".to_string())]),
                global: def::Option(Some(Box::new(def::Bool(true)))),
                sub: def::Option(Some(Box::new(Sub { value: def::Bool(true) })))
            },
            ModuleSubObj {
                imports: def::Array(vec![def::Ident("Ident3".to_string()), def::Ident("Ident4".to_string())]),
                global: def::Option(None),
                sub: def::Option(None)
            }
        ]))
    );
}
fn main() {
    test_formal_f3();
    test_formal_f4();
    test_formal_f5();
    test_formal_f6();
}

更多示例…

关于

MIT

阅读变更日志

依赖项

~335–800KB
~18K SLoC