1个不稳定版本
0.1.0 | 2022年4月17日 |
---|
#344 in 值格式化
用于flexgen
21KB
347 行
use-builder
一个通过组合多个(可能重复)的use部分输入来构建源代码use部分的crate。
注意:这是一个相当专业的crate。唯一可能的用途是将源代码片段编译成文件,就像flexgen所做的那样。
用法
[dependencies]
use-builder = "0.1"
示例
use assert_unordered::assert_eq_unordered;
use quote::quote;
use use_builder::{UseBuilder, UseItems};
fn main() {
// #1 - Build a two or more use trees and convert into `UseItems` (wrapped `Vec<ItemUse>`)
let use1 = quote! {
use crate::Test;
use std::error::{Error as StdError};
use std::fmt::Debug;
};
let use2 = quote! {
use syn::ItemUse;
use std::fmt::Display;
use crate::*;
};
let items1: UseItems = syn::parse2(use1).unwrap();
let items2: UseItems = syn::parse2(use2).unwrap();
// #2 - Parse, process, and extract into sections
let builder = UseBuilder::from_uses(vec![items1, items2]);
let (std_use, ext_use, crate_use) = builder.into_items_sections().unwrap();
// #3 - Validate our response matches expectation
let std_expected = quote! {
use std::error::Error as StdError;
use std::fmt::{Debug, Display};
};
let std_expected = syn::parse2::<UseItems>(std_expected).unwrap().into_inner();
let ext_expected = quote! {
use syn::ItemUse;
};
let ext_expected = syn::parse2::<UseItems>(ext_expected).unwrap().into_inner();
let crate_expected = quote! {
use crate::*;
};
let crate_expected = syn::parse2::<UseItems>(crate_expected).unwrap().into_inner();
assert_eq_unordered!(std_expected, std_use);
assert_eq_unordered!(ext_expected, ext_use);
assert_eq_unordered!(crate_expected, crate_use);
}
许可证
本项目可选择在以下许可证下使用:
- Apache许可证,版本2.0,(LICENSE-APACHE或https://www.apache.org/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT或https://opensource.org/licenses/MIT)
依赖
~2.5MB
~51K SLoC