#expand #source-code #inline-modules #rust

syn-file-expand

用于加载多文件crates完整源代码的库

4个版本 (2个重大更改)

0.3.0 2023年7月12日
0.2.0 2022年5月28日
0.1.1 2022年3月28日
0.1.0 2022年3月26日

#171 in 过程宏

Download history 223/week @ 2024-03-13 342/week @ 2024-03-20 419/week @ 2024-03-27 445/week @ 2024-04-03 489/week @ 2024-04-10 241/week @ 2024-04-17 324/week @ 2024-04-24 329/week @ 2024-05-01 264/week @ 2024-05-08 292/week @ 2024-05-15 194/week @ 2024-05-22 431/week @ 2024-05-29 274/week @ 2024-06-05 256/week @ 2024-06-12 498/week @ 2024-06-19 233/week @ 2024-06-26

每月下载量 1,299
21 个crates中使用 (直接使用3个)

MIT/Apache

38KB
602 代码行

syn-file-expand

此库允许您将多文件crates的完整源代码加载到单个 syn::File

特性

  • 基于 syn crate。
  • 处理 #[path] 属性
  • 处理 #[cfg],它影响模块到文件系统的映射
  • 提供了两种函数:一种是低级、无I/O的函数,另一种是更简单的函数,只需从 std::fs::Path 加载crate。

限制

read_crate 函数开始探索库。

额外功能:命令行工具

syn-file-expand-cli 工具扩展Rust源代码,类似于 cargo expand,但无需宏展开,仅适用于模块。不涉及 rustc。建议使用 rustfmt 过滤以进行调试。

$ syn-file-expand-cli -fTp src/lib.rs
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
...
mod attrs {
    use proc_macro2;
    ...
}
mod expand_impl {
    use std::path::PathBuf;
    ...
}

$ syn-file-expand-cli --help
Usage: syn-file-expand-cli [OPTIONS]

Use `syn-file-expand-cli -fTp src/lib.rs` as a starting point.

Reads rust source file, including referred modules and expands them into a single source with all modules inline
Apart from respective dedicated command line arguments, conditional paths like
`#[cfg_attr(feature="qqq"),path=...)` are resolved using
environment variables like SYN_FILE_EXPAND_FEATURE_QQQ=1
Other influential envvars: SYN_FILE_EXPAND_DEBUGVARS=1 SYN_FILE_EXPAND_DEFAULTTRUE=1

Positional arguments:
  input_file                 Input Rust source file to start crawling from

Optional arguments:
  -h, --help
  -l, --loopify              Convert all blocks and expressions to `loop{}`s.
                      Note that inner items within blocks get lost in the process.
  -D, --undoc                Strip all documentation attributes. Note that inner items within blocks are not processed and may retain their attributes.
  -T, --cfg-true-by-default  Assume all `#[cfg]`s and `#[cfg_attr]`s are true. May lead to errors unless `-f` is also used.
  -f, --full-crate-tree      Allow duplicate modules, also preserve/transform some `cfg` attributes.
  -c, --cfg CFG              Set this cfg check result to true.
                                Note that `all` or `any` are not handled.
                                You need to set all needed expression results one by one.
                                Strings required for --cfg are not the same as for environment variables-
                                -based version of this feature.
  -u, --unset-cfg UNSET-CFG  In `--cfg-true-by-default` mode, explicitly unset given cfg expression outcome.
  -d, --debug-cfg            Print each encountered cfg check to stderr, in form suitable for `--cfg` parameter
                   Note that the format is different from the one used by `SYN_FILE_EXPAND_DEBUGVARS=1` environment variable.
  -o, --output OUTPUT        Use given file for output instead of stdout
  -p, --pretty               Use `prettyplease` to format the output

有一个GitHub发布版,包含为各种平台预先构建的工具。
您还可以使用 cargo install syn-file-expand-cli 安装工具。

依赖

~1.7–2.2MB
~51K SLoC