在适当的情况下自动转换所有标识符

Automatically convert all the identifiers in appropriate case

例如,

pub enum Format {
    Undefined,
    R4g4UnormPack8,
    R4g4b4a4UnormPack16,
    B4g4r4a4UnormPack16,
    R5g6b5UnormPack16,
    B5g6r5UnormPack16,

    R5G5B5A1_UNORM_PACK16,
    B5G5R5A1_UNORM_PACK16,
    A1R5G5B5_UNORM_PACK16,
    R8_UNORM,
    //...
    G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16,
    //...
}

前 6 行是我使用 rust-analyzer 代码操作手动转换的。

但是您可以猜到这个列表很长,我正在寻找一种方法来完成所有这些工作。 就像构建时它警告我转换它。

warning: variant `G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16` should have an upper camel case name
   --> src/core.rs:388:5
    |
388 |     G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16,
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to upper camel case: `G10x6B10x6R10x6_3plane420Unorm3pack16`

而且我也想转换一下。有没有办法做到这一点?感谢任何帮助。

可以使用 rustfix 修复它,但根据 github issue, this lint is filtered out, it can be included via setting env var(ref)。

对于 Windows:

set __CARGO_FIX_YOLO="yolo"
cargo fix

基于 Unix:

export __CARGO_FIX_YOLO="yolo"
cargo fix

重要:

请注意,这将尝试修复您源代码中的所有内容,这可能会带来不必要的副作用,您可能需要检查 cargo fix ref 或者您可以通过将您的结构复制到全新的结构来做一些小改动项目然后在修复后重新添加到您的项目中。

问了这个问题后,我一直在努力实现。我想它有效。

这是我的代码。

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CharClass {
    Alphabetic,
    Digit,
}

fn convert(text: &str) -> String {
    let mut previous = None;
    let mut current = CharClass::Alphabetic;

    let mut result = String::with_capacity(text.len());

    result.push_str(text.get(..1).unwrap());
    for ch in text.chars().skip(1) {
        match previous {
            // `previous` is only `Some` if encountered '_'.
            Some(t) => {
                match t {
                    CharClass::Alphabetic => {
                        if ch.is_ascii_alphabetic() {
                            current = CharClass::Alphabetic;
                        } else if ch.is_ascii_digit() {
                            current = CharClass::Digit;
                        }
                        result.push(ch);
                    }
                    CharClass::Digit => {
                        if ch.is_ascii_alphabetic() {
                            current = CharClass::Alphabetic;
                            result.push(ch);
                        } else if ch.is_ascii_digit() {
                            current = CharClass::Digit;
                            result.push('_');
                            result.push(ch);
                        }
                    }
                }
                previous = None;
            }
            None => {
                if ch.is_ascii_alphabetic() {
                    current = CharClass::Alphabetic;
                    result.push(ch.to_ascii_lowercase());
                } else if ch.is_ascii_digit() {
                    current = CharClass::Digit;
                    result.push(ch);
                } else if ch == '_' {
                    previous = Some(current);
                } else {
                    result.push(ch);
                }
            }
        }
    }

    result
}

fn main() {
    assert_eq!(
        "G10x6B10x6R10x6_3plane420Unorm3pack16",
        &convert("G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16")
    );

    assert_eq!(
        "Astc12x12SfloatBlockExt",
        &convert("ASTC_12x12_SFLOAT_BLOCK_EXT")
    );
}