askibin / perpetuals

Solana Perpetuals Exchange
Other
34 stars 18 forks source link

Configure rust fmt #24

Closed adrena-orex closed 1 year ago

adrena-orex commented 1 year ago

Setup new rustfmt.toml file to specify custom rules:

imports_granularity = "One"
group_imports = "One"
use_field_init_shorthand = true

See:

To run:

cargo +nightly fmt

What changes:

adrena-orex commented 1 year ago

Requires to use +nightly because imports_granularity and group_imports rules are not Stable and requires nightly mode.

If ran without +nightly:

orex> cargo fmt                              
Warning: can't set `imports_granularity = One`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = One`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = One`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = One`, unstable features are only available in nightly channel.
Adrena-Corto commented 1 year ago

Ngl grouping imports has been a pain, need Might want to add this as pre-commit-hook? (not adding clippy there cause it will download deps and compile project at each commit)

#!/bin/bash

diff=$(cargo +nightly fmt -- --check)
result=$?

if [[ ${result} -ne 0 ]] ; then
    cat <<\EOF
There are some code style issues, run `cargo fmt` first.
EOF
    exit 1
fi

exit 0
askibin commented 1 year ago

it doesn't seem to be working with Vs Code out of the box: https://github.com/rust-lang/rust-analyzer/issues/6973 I.e. rustfmt.toml is ignored, providing all args to rust-analyzer.rustfmt.overrideCommand might be the only viable solution, which is not the end of the world, but still. At least README.md will need to be updated. Could you check if you can make it work?

adrena-orex commented 1 year ago

It's possible to add +nightly as extra args for rustanalyser.

    "rust-analyzer.rustfmt.extraArgs": [
        "+nightly"
    ],
Screenshot 2023-03-09 at 09 38 36

Why it doesn't work right away is because toolchain is configured as 1.66.0 and that is what is used by default with rustfmt.

adrena-orex commented 1 year ago

@askibin I have updated the README.md and added .vscode/settings.json to automatically apply the +nightly.

Not sure if we should include the .vscode there or if users should manually configure it in theirs vscode at install. I let you tell me, I'm ok both ways.

adrena-orex commented 1 year ago

Added pre-commit hook. Needs to execute the command git config core.hooksPath .githooks. Have updated README.md.

Adrena-Corto commented 1 year ago

@askibin I have updated the README.md and added .vscode/settings.json to automatically apply the +nightly.

Not sure if we should include the .vscode there or if users should manually configure it in theirs vscode at install. I let you tell me, I'm ok both ways.

I think the .vscode should be added to have a self contained working repo?

askibin commented 1 year ago

could you resolve conflicts please

adrena-orex commented 1 year ago

could you resolve conflicts please

Done!