Skip to content

Commit

Permalink
update formatting docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Lubinets committed Mar 23, 2019
1 parent 98045ea commit 3e86815
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions FORMATTING.md
Expand Up @@ -3,9 +3,19 @@ This repository incorporates a certain formatting style derived from the [rustfm
- Maximum line width is 120 characters
- Newline separator style is always Unix (`\n` as opposed to Windows `\n\r`)
- `try!` macro would automatically convert into a `?` question-mark operator expression

Any other configuration is the **stable** `rustfmt` default.

#### Running the formatter
`evm-rs` is a complex project with a lot of sub-crates, so `cargo fmt` should be invoked with an `--all` argument.
```bash
cargo fmt --all
```
More info can be found at the [project page](https://github.com/rust-lang/rustfmt)

#### Manual formatting overrides
The formatting constraint is checked within Travis CI and Jenkins continuous integration pipelines, hence any pull-request should be formatted before it may be merged.

Though, as most of the tooling generally is, `rustfmt` isn’t perfect and sometimes one would requite to force the manual formatting, as, for instance, it’s required for the `Patch` interface in `evm-rs`
##### Original code
```rust
Expand All @@ -31,7 +41,8 @@ impl AccountPatch for EmbeddedAccountPatch {
}
}
```
Clearly, the readability of the code have decreased, as the expanded function body brings none value in this case. While this would be possible to fix with **nightly** `rustfmt`, nightly version is still too unstable, so it’s encouraged to use manual formatting overrides where it is justified.
Depending on the case, the readability of the code may decrease, like here (IMHO) since the expanded function body brings none value.
While this would be possible to fix with **nightly** `rustfmt`, nightly version is still too unstable, so it’s encouraged to use manual formatting overrides where it is justified.
##### Manual override
```rust
pub struct EmbeddedAccountPatch;
Expand All @@ -43,15 +54,16 @@ impl AccountPatch for EmbeddedAccountPatch {
}
```
#### Automation
To ensure none commits are misformatted, it’s required to manually run rustfmt before commiting the code though that might be irritating.
To ensure none commits are misformatted, it’s required to manually run rustfmt before commiting the code, though that might be irritating.
Fortunately, formatting (and formatting checks) may be automated, and here are ways to do that:
##### 1. Formatting on save
Most of the editors have a feature of pre-save hooks that can execute arbitrary commands before persisting the file contents.
* [Setup for JetBrains IDEs (Clion, Intellij Idea, …)](https://codurance.com/2017/11/26/rusting-IntelliJ/)
* [Setup for VSCode](https://github.com/editor-rs/vscode-rust/blob/master/doc/format.md)

If the editor doesn’t support the on-save hook, one could automate formatting through [cargo watch](https://github.com/passcod/cargo-watch):
```bash
cargo watch -s “cargo fmt all”
cargo watch -s “cargo fmt --all”
```
##### 2. Git pre-commit hook
Create a file `.git/hooks/pre-commit` with the following contents:
Expand All @@ -71,6 +83,6 @@ done
```
This hook will reject ill-formatted code before the commit.

Synergy of these two automation techniques should allow one to ensure formatting correctness and it would not require running `rustfmt` manually.
Synergy of these two automation techniques should allow one to ensure formatting correctness while not being forced to run `rustfmt` manually.


0 comments on commit 3e86815

Please sign in to comment.