rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.06k stars 892 forks source link

Draft: Add #[rustfmt::sort] and implement for enum variants and struct structs #6313

Open Code0x58 opened 2 months ago

Code0x58 commented 2 months ago

3422: add explicit rustfmt::sort attribute which causes case sensitive sorting

This does not raise if rustfmt::sort is applied in an unhandled place (e.g. struct, member, etc.), although that might be reasonable to pursue; at a glance, rustfmt::skip does not appear to do that, which may be more reasonable given how it can reasonably apply to anything.

TODO:

Code0x58 commented 2 months ago

I've found this useful as is, for formatting a large enumeration with attributes on the members. One issue I've bumped into was that this enumeration in particular uses Serde's untagged feature e.g.

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum StringyThingy {
    Zoo,
    #[serde(untagged)]
    Other(String)
}

where Other must be at the end. That makes me think that it would be best to try working out some way of maintaining this. The most practical solution I can think of right now is to have some a way to stop the index from being changed, e.g. rustfmt::skip::sort

pub enum StringyThingy {
    Zoo,
    #[rustfmt::skip::sort]
    #[serde(untagged)]
    Other(String)
}