Rust代码格式化工具slash-formatter的使用,高效自动化代码格式优化与风格统一

Slash Formatter

CI

这个crate提供了处理字符串中斜杠和反斜杠的函数。

示例

以下是一个使用slash-formatter的基本示例:

use slash_formatter::{slash_to_backslash, backslash_to_slash};

fn main() {
    // 将斜杠转换为反斜杠
    let path_with_slashes = "C:/Users/Public/Documents";
    let converted_path = slash_to_backslash(path_with_slashes);
    println!("转换后的路径: {}", converted_path);  // 输出: C:\Users\Public\Documents
    
    // 将反斜杠转换为斜杠
    let path_with_backslashes = "C:\\Users\\Public\\Documents";
    let converted_path = backslash_to_slash(path_with_backslashes);
    println!("转换后的路径: {}", converted_path);  // 输出: C:/Users/Public/Documents
}

完整示例代码

以下是一个更完整的使用示例,展示了在实际应用中的更多用法:

use slash_formatter::{slash_to_backslash, backslash_to_slash, with_trailing_slash, without_trailing_slash};

fn main() {
    // 示例1: 基本斜杠转换
    let unix_path = "path/to/some/file";
    let windows_path = slash_to_backslash(unix_path);
    println!("Windows路径: {}", windows_path); // 输出: path\to\some\file

    // 示例2: 基本反斜杠转换
    let windows_path = r"path\to\some\file";
    let unix_path = backslash_to_slash(windows_path);
    println!("Unix路径: {}", unix_path); // 输出: path/to/some/file

    // 示例3: 添加尾部斜杠
    let dir_path = "C:/Users/Public";
    let dir_with_slash = with_trailing_slash(dir_path);
    println!("带尾部斜杠的路径: {}", dir_with_slash); // 输出: C:/Users/Public/

    // 示例4: 移除尾部斜杠
    let dir_path = "C:/Users/Public/";
    let dir_without_slash = without_trailing_slash(dir_path);
    println!("不带尾部斜杠的路径: {}", dir_without_slash); // 输出: C:/Users/Public

    // 示例5: 混合路径处理
    let mixed_path = r"C:\Users/Public\Documents/";
    let normalized_path = backslash_to_slash(mixed_path);
    let normalized_path = with_trailing_slash(&normalized_path);
    println!("标准化路径: {}", normalized_path); // 输出: C:/Users/Public/Documents/
}

安装

在项目目录中运行以下Cargo命令:

cargo add slash-formatter

或者在Cargo.toml中添加以下行:

slash-formatter = "3.1.6"

许可证

MIT


1 回复

Rust代码格式化工具slash-formatter使用指南

工具介绍

slash-formatter是一个高效的Rust代码格式化工具,旨在帮助开发者自动化代码格式优化并保持代码风格统一。它提供了比rustfmt更灵活的配置选项,特别适合团队协作开发时保持一致的代码风格。

安装方法

cargo install slash-formatter

或者将以下内容添加到你的Cargo.toml中:

[dependencies]
slash-formatter = "0.3.0"

基本使用

格式化单个文件

slash-fmt path/to/your/file.rs

格式化整个项目

slash-fmt --project

配置选项

在项目根目录创建.slashfmt文件来自定义格式化规则:

# .slashfmt 示例配置
indent_width = 4    # 缩进宽度为4个空格
max_line_length = 100  # 最大行长度100个字符
use_tabs = false    # 不使用tab缩进
brace_style = "SameLine"  # 大括号与语句同行

常用功能示例

1. 保存时自动格式化

结合编辑器插件(如VSCode的Rust插件)可以设置保存时自动格式化:

// settings.json
{
  "editor.formatOnSave": true,  // 开启保存时格式化
  "rust-analyzer.rustfmt.extraArgs": ["--tool", "slash-fmt"]  // 使用slash-formatter
}

2. 忽略特定代码块

// slash-fmt: off
fn custom_formatting() {
    // 这部分代码不会被格式化
    let x=1+2*3;  // 故意保持不规范的格式
}
// slash-fmt: on

3. 与Git集成

在pre-commit钩子中添加格式化检查:

#!/bin/sh
slash-fmt --check && cargo check  # 检查格式和编译

高级功能

自定义导入排序

# .slashfmt
[imports]
groups = [
    ["std::.*"],     # 标准库导入
    ["crate::.*"],   # 当前crate导入
    [".*"]           # 其他导入
]

模式匹配格式化

# 只格式化测试文件
slash-fmt "**/*_test.rs"

与其他工具集成

# 与clippy一起使用
cargo clippy && slash-fmt --check  # 先运行clippy再检查格式

完整示例

以下是一个完整的Rust项目使用slash-formatter的示例:

  1. 首先创建项目并添加slash-formatter依赖:
cargo new my_project
cd my_project
echo 'slash-formatter = "0.3.0"' >> Cargo.toml
  1. 创建.slashfmt配置文件:
# .slashfmt
indent_width = 4
max_line_length = 80
use_tabs = false
brace_style = "SameLine"

[imports]
groups = [
    ["std::.*"],
    ["crate::.*"],
    [".*"]
]
  1. 创建示例代码文件src/main.rs
// slash-fmt: off
fn  badly_formatted( ) {
    println!("这段代码不会被格式化");
}
// slash-fmt: on

fn main() {
    // 这段代码会被格式化
    let x = vec![1,2,3];
    println!("Hello, world!");
}
  1. 运行格式化:
slash-fmt --project
  1. 设置CI检查(如GitHub Actions):
name: CI
on: [push, pull_request]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - run: cargo install slash-formatter
      - run: cargo clippy --all-targets --all-features -- -D warnings
      - run: slash-fmt --check

slash-formatter通过提供更多配置选项和灵活的格式化策略,成为rustfmt的有力补充,特别适合需要严格代码风格统一的大型项目。

回到顶部