#rustfmt #formatting #block #best #mangled #macro #pls

nofmt

This crate provides nofmt::pls, a macro that does its best at preventing a code block from being mangled

1 stable release

1.0.0 May 14, 2021

#1262 in Rust patterns

25 downloads per month
Used in 2 crates

MIT license

5KB

This crate provides nofmt::pls, a macro that does its best at preventing a code block from being mangled.

[dependencies]
nofmt = "1.0"
nofmt::pls! {
    //                                      R      G      B      A
    pub const MAIN_WINDOW_BG:  [f32; 4] = [0.187, 0.187, 0.187, 1.  ];
    pub const DEBUG_WINDOW_BG: [f32; 4] = [0.   , 0.   , 0.   , 0.85];
    pub const INPUT_BOX_BG:    [f32; 4] = [0.011, 0.022, 0.055, 1.  ];
}

How to bypass rustfmt 101

Executing cargo fmt on a crate is likely to violently alter every file, irreversibly. So our only option is to just go with it.

  • To skip a single item declaration, use #[rustfmt::skip]
  • To skip a block, use nofmt::pls!
  • To skip an entire file, use #![cfg_attr(rustfmt, rustfmt_skip)]

Keep in mind that opting out of rustfmt can not be done through rustfmt.toml.
Since both disable_all_formatting = true and ignore = ["/"] are not stable features.

Known limitations

By itself, this method is not perfect, inside of a nofmt::pls block:

  • each line will get trimmed.
  • indenting tabs get replaced by spaces.

Note that format macros for rustfmt is an open issue. So this crate might stop working at any point in time.

Going all out

To bypass the limitations, you can use the rustfmt::skip attribute in conjunction with the the macro.

#[rustfmt::skip]	  	nofmt::pls!	  	{	  	
	  	let	absolute_preservation_of_whitespace	=	"achieved";	 	
  	}

No runtime deps