#derive #cargo-subcommand #cargo #subcommand #plugin

app cargo-sort-derives

Cargo 子命令,用于排序 derive 属性

4 个版本 (重大更新)

新版本 0.4.0 2024年8月21日
0.3.0 2024年8月20日
0.2.0 2024年8月18日
0.1.0 2024年8月17日

#104 in Cargo 插件

Download history 266/week @ 2024-08-12

每月266次下载

MIT 许可证

22KB
466

cargo-sort-derives

Crate Status

Cargo 子命令,用于排序 derive 属性

关于

这是一个 Cargo 子命令,帮助你一致地排序 Rust 代码中的 derive 属性。

该工具确保你的结构体和枚举中的 derive 属性按一致的方式排序。

安装

$ cargo install --locked cargo-sort-derives

用法

Usage: cargo sort-derives [OPTIONS]

Options:
      --order <VALUE>  Define the custom order of derive attributes, separated by commas (e.g. "Debug, Clone, Copy")
                       Any derives not listed will appear at the end in alphabetical order by default
      --preserve       Preserve the original order for unspecified derive attributes (only applies when --order is used)
      --check          Check if the derive attributes are sorted
      --color <TYPE>   Use colored output [default: auto] [possible values: auto, always, never]
  -h, --help           Print help
  -V, --version        Print version

基本

以下命令会排序当前目录下 derive 属性的 .rs 文件

$ cargo sort-derives

这将按以下顺序重新排序 derive 属性

// Before:
#[derive(Debug, Clone, Copy, Default, Eq)]
struct Example;

// After: sorted alphabetically
#[derive(Clone, Copy, Debug, Default, Eq)]
struct Example;

默认情况下,它是按字母顺序排序的。

指定顺序

$ cargo sort-derives --order "Eq, Clone, Default"

这将按以下顺序重新排序 derive 属性

// Before:
#[derive(Debug, Clone, Copy, Default, Eq)]
struct Example;

// After: "Eq, Clone, Default" are sorted in that order, the rest are sorted alphabetically
#[derive(Eq, Clone, Default, Copy, Debug)]
struct Example;

未列出的任何 derive 将会按字母顺序出现在末尾。

使用 --preserve 选项可以保持未指定在 --order 选项中的 derive 属性的原始顺序。

$ cargo sort-derives --order "Eq, Clone, Default" --preserve

这将按以下顺序重新排序 derive 属性

// Before:
#[derive(Debug, Clone, Copy, Default, Eq)]
struct Example;

// After: "Eq, Clone, Default" are sorted in that order, the rest keep the original order
#[derive(Eq, Clone, Default, Debug, Copy)]
struct Example;

不更新进行检查

$ cargo sort-derives --check

这将检查你的 .rs 文件中的 derive 属性是否已正确排序。

如果属性顺序错误,命令将以非零状态码退出,表示文件需要更新。

配置

如果当前目录中存在 .sort-derives.toml,则会加载配置。

格式

.sort-derives.toml 文件使用以下格式

# Define the custom order of derive attributes, separated by commas (e.g. "Debug, Clone, Copy")
# The command line option `--order` will override this setting if specified.
# type: string
order = "Eq, Clone, Default"
# Preserve the original order for unspecified derive attributes (only applies when custom order is used)
# The command line option `--preserve` will override this setting if specified.
# type: boolean
preserve = true

许可证

MIT

依赖

~9–19MB
~303K SLoC